|
| 1 | +/** |
| 2 | + * Provides classes for working with HTML sanitizers. |
| 3 | + */ |
| 4 | +import javascript |
| 5 | + |
| 6 | +/** |
| 7 | + * A call that sanitizes HTML in a string, either by replacing |
| 8 | + * meta characters with their HTML entities, or by removing |
| 9 | + * certain HTML tags entirely. |
| 10 | + */ |
| 11 | +abstract class HtmlSanitizerCall extends DataFlow::CallNode { |
| 12 | + /** |
| 13 | + * Gets the data flow node referring to the input that gets sanitized. |
| 14 | + */ |
| 15 | + abstract DataFlow::Node getInput(); |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Matches HTML sanitizers from known NPM packages as well as home-made sanitizers (matched by name). |
| 20 | + */ |
| 21 | +private class DefaultHtmlSanitizerCall extends HtmlSanitizerCall { |
| 22 | + DefaultHtmlSanitizerCall() { |
| 23 | + exists (DataFlow::SourceNode callee | this = callee.getACall() | |
| 24 | + callee = DataFlow::moduleMember("ent", "encode") or |
| 25 | + callee = DataFlow::moduleMember("entities", "encodeHTML") or |
| 26 | + callee = DataFlow::moduleMember("entities", "encodeXML") or |
| 27 | + callee = DataFlow::moduleMember("escape-goat", "escape") or |
| 28 | + callee = DataFlow::moduleMember("he", "encode") or |
| 29 | + callee = DataFlow::moduleMember("he", "escape") or |
| 30 | + callee = DataFlow::moduleImport("sanitize-html") or |
| 31 | + callee = DataFlow::moduleMember("sanitizer", "escape") or |
| 32 | + callee = DataFlow::moduleMember("sanitizer", "sanitize") or |
| 33 | + callee = DataFlow::moduleMember("validator", "escape") or |
| 34 | + callee = DataFlow::moduleImport("xss") or |
| 35 | + callee = DataFlow::moduleMember("xss-filters", _) or |
| 36 | + callee = LodashUnderscore::member("escape") or |
| 37 | + exists (string name | name = "encode" or name = "encodeNonUTF" | |
| 38 | + callee = DataFlow::moduleMember("html-entities", _).getAnInstantiation().getAPropertyRead(name) or |
| 39 | + callee = DataFlow::moduleMember("html-entities", _).getAPropertyRead(name)) |
| 40 | + ) |
| 41 | + or |
| 42 | + // Match home-made sanitizers by name. |
| 43 | + exists (string calleeName | calleeName = getCalleeName() | |
| 44 | + calleeName.regexpMatch("(?i).*html.*") and |
| 45 | + calleeName.regexpMatch("(?i).*(?<!un)(saniti[sz]|escape|strip).*")) |
| 46 | + } |
| 47 | + |
| 48 | + override DataFlow::Node getInput() { result = getArgument(0) } |
| 49 | +} |
0 commit comments