|
| 1 | +/** |
| 2 | + * @name Unsafe expansion of shorthand HTML tag |
| 3 | + * @description Using regular expressions to expand shorthand HTML |
| 4 | + * tags may lead to cross-site scripting vulnerabilities. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @precision very-high |
| 8 | + * @id js/unsafe-html-expansion |
| 9 | + * @tags correctness |
| 10 | + * security |
| 11 | + * external/cwe/cwe-079 |
| 12 | + * external/cwe/cwe-116 |
| 13 | + */ |
| 14 | + |
| 15 | +import javascript |
| 16 | + |
| 17 | +/** |
| 18 | + * A regular expression that captures the name and content of a shorthand HTML tag such as `<div id='foo'/>`. |
| 19 | + */ |
| 20 | +class ShorthandTagRecognizer extends RegExpLiteral { |
| 21 | + ShorthandTagRecognizer() { |
| 22 | + exists(RegExpSequence seq, RegExpGroup name, RegExpGroup content | |
| 23 | + // `/.../g` |
| 24 | + this.isGlobal() and |
| 25 | + this = seq.getLiteral() and |
| 26 | + // `/<.../` |
| 27 | + seq.getChild(0).getConstantValue() = "<" and |
| 28 | + // `/...\/>/` |
| 29 | + seq.getLastChild().getPredecessor().getConstantValue() = "/" and |
| 30 | + seq.getLastChild().getConstantValue() = ">" and |
| 31 | + // `/...((...)...).../` |
| 32 | + seq.getAChild() = content and |
| 33 | + content.getNumber() = 1 and |
| 34 | + name.getNumber() = 2 and |
| 35 | + name = content.getChild(0).(RegExpSequence).getChild(0) and |
| 36 | + // `/...(([a-z]+)...).../` or `/...(([a-z][...]*)...).../` |
| 37 | + exists(RegExpQuantifier quant | name.getAChild*() = quant | |
| 38 | + quant instanceof RegExpStar or |
| 39 | + quant instanceof RegExpPlus |
| 40 | + ) and |
| 41 | + // `/...((...)[^>]*).../` |
| 42 | + exists(RegExpCharacterClass lazy | |
| 43 | + name.getSuccessor().(RegExpStar).getChild(0) = lazy and |
| 44 | + lazy.isInverted() and |
| 45 | + lazy.getAChild().getConstantValue() = ">" |
| 46 | + ) |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Gets a data flow node that may refer to this regular expression. |
| 52 | + */ |
| 53 | + DataFlow::SourceNode ref(DataFlow::TypeTracker t) { |
| 54 | + t.start() and |
| 55 | + result = this.flow() |
| 56 | + or |
| 57 | + exists(DataFlow::TypeTracker t2 | result = ref(t2).track(t2, t)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +from ShorthandTagRecognizer regexp, StringReplaceCall replace |
| 62 | +where |
| 63 | + regexp.ref(DataFlow::TypeTracker::end()).flowsTo(replace.getArgument(0)) and |
| 64 | + replace.getRawReplacement().mayHaveStringValue("<$1></$2>") |
| 65 | +select replace, |
| 66 | + "This HTML tag expansion may disable earlier sanitizations as $@ may match unintended strings.", |
| 67 | + regexp, "this regular expression" |
0 commit comments