|
| 1 | +/** |
| 2 | + * Provides precicates for reasoning about bad tag filter vulnerabilities. |
| 3 | + */ |
| 4 | + |
| 5 | +import performance.ReDoSUtil |
| 6 | + |
| 7 | +/** |
| 8 | + * A module for determining if a regexp matches a given string. |
| 9 | + */ |
| 10 | +private module RegexpMatching { |
| 11 | + /** |
| 12 | + * A class to test whether a regular expression matches a string. |
| 13 | + * Override this class and extend `toTest` to configure which strings should be tested for acceptance by this regular expression. |
| 14 | + * The result can afterwards be read from the `matches` predicate. |
| 15 | + */ |
| 16 | + abstract class MatchedRegExp extends RegExpTerm { |
| 17 | + MatchedRegExp() { this.isRootTerm() } |
| 18 | + |
| 19 | + /** |
| 20 | + * Holds if it should be tested whether this regular expression matches `str`. |
| 21 | + * |
| 22 | + * If `ignorePrefix` is true, then a regexp without a start anchor will be treated as if it had a start anchor. |
| 23 | + * E.g. a regular expression `/foo$/` will match any string that ends with "foo", |
| 24 | + * but if `ignorePrefix` is true, it will only match "foo". |
| 25 | + */ |
| 26 | + abstract predicate toTest(string str, boolean ignorePrefix); |
| 27 | + |
| 28 | + /** |
| 29 | + * Gets a state a regular expression is in after matching the `i`th char in `str`. |
| 30 | + * The regular expression is modelled as a non-determistic finite automaton, |
| 31 | + * the regular expression can therefore be in multiple states after matching a character. |
| 32 | + */ |
| 33 | + private State getAState(int i, string str, boolean ignorePrefix) { |
| 34 | + i = -1 and |
| 35 | + this.toTest(str, ignorePrefix) and |
| 36 | + result.getRepr().getRootTerm() = this and |
| 37 | + isStartState(result) |
| 38 | + or |
| 39 | + exists(State prev | |
| 40 | + prev = getAState(i - 1, str, ignorePrefix) and |
| 41 | + deltaClosed(prev, getAnInputSymbolMatching(str.charAt(i)), result) and |
| 42 | + not ( |
| 43 | + ignorePrefix = true and |
| 44 | + isStartState(prev) and |
| 45 | + isStartState(result) |
| 46 | + ) |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Holds if `regexp` matches `str`. |
| 52 | + */ |
| 53 | + predicate matches(string str) { |
| 54 | + exists(State state | state = getAState(str.length() - 1, str, _) | |
| 55 | + epsilonSucc*(state) = Accept(_) |
| 56 | + ) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Holds if `state` is a start state. |
| 62 | + */ |
| 63 | + private predicate isStartState(State state) { |
| 64 | + state = mkMatch(any(RegExpRoot r)) and |
| 65 | + not exists(RegExpCaret car | car.getRootTerm() = state.getRepr().getRootTerm()) |
| 66 | + or |
| 67 | + exists(RegExpCaret car | state = after(car)) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * A class to test whether a regular expression matches certain HTML tags. |
| 73 | + */ |
| 74 | +class HTMLMatchingRegExp extends RegexpMatching::MatchedRegExp { |
| 75 | + HTMLMatchingRegExp() { |
| 76 | + // the regexp must mention "<" and ">" explicitly. |
| 77 | + forall(string angleBracket | angleBracket = ["<", ">"] | |
| 78 | + any(RegExpConstant term | term.getValue().regexpMatch(".*" + angleBracket + ".*")) |
| 79 | + .getRootTerm() = this |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + override predicate toTest(string str, boolean ignorePrefix) { |
| 84 | + ignorePrefix = true and |
| 85 | + str = |
| 86 | + [ |
| 87 | + "<!-- foo -->", "<!- foo ->", "<!-- foo --!>", "<!-- foo\n -->", "<script>foo</script>", |
| 88 | + "<script \n>foo</script>", "<script >foo\n</script>", "<foo ></foo>", "<foo>", |
| 89 | + "<foo src=\"foo\"></foo>", "<script>", "<script src=\"foo\"></script>", |
| 90 | + "<script src='foo'></script>", "<SCRIPT>foo</SCRIPT>", "<script\tsrc=\"foo\"/>", |
| 91 | + "<script\tsrc='foo'></script>", "<sCrIpT>foo</ScRiPt>", "<script src=\"foo\">foo</script >", |
| 92 | + "<script src=\"foo\">foo</script foo=\"bar\">", "<script src=\"foo\">foo</script\t\n bar>" |
| 93 | + ] |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Holds if `regexp` matches some HTML tags, but misses some HTML tags that it should match. |
| 99 | + * |
| 100 | + * When adding a new case to this predicate, make sure the test string used in `matches(..)` calls are present in `HTMLMatchingRegExp::toTest`. |
| 101 | + */ |
| 102 | +predicate isBadRegexpFilter(HTMLMatchingRegExp regexp, string msg) { |
| 103 | + regexp.matches("<!-- foo -->") and |
| 104 | + not regexp.matches("<!-- foo --!>") and |
| 105 | + not regexp.matches("<!- foo ->") and |
| 106 | + not regexp.matches("<foo>") and |
| 107 | + not regexp.matches("<script>") and |
| 108 | + msg = "This regular expression only matches --> and not --!> as a HTML comment end tag." |
| 109 | + or |
| 110 | + regexp.matches("<!-- foo -->") and |
| 111 | + not regexp.matches("<!-- foo\n -->") and |
| 112 | + not regexp.matches("<!- foo ->") and |
| 113 | + not regexp.matches("<foo>") and |
| 114 | + not regexp.matches("<script>") and |
| 115 | + msg = "This regular expression does not match comments containing newlines." |
| 116 | + or |
| 117 | + regexp.matches("<script>foo</script>") and |
| 118 | + regexp.matches("<script src=\"foo\"></script>") and |
| 119 | + not regexp.matches("<foo ></foo>") and |
| 120 | + ( |
| 121 | + not regexp.matches("<script \n>foo</script>") and |
| 122 | + msg = "This regular expression matches <script></script>, but not <script \\n></script>" |
| 123 | + or |
| 124 | + not regexp.matches("<script >foo\n</script>") and |
| 125 | + msg = "This regular expression matches <script>foo</script>, but not <script >foo\\n</script>" |
| 126 | + ) |
| 127 | + or |
| 128 | + regexp.matches("<script src=\"foo\"></script>") and |
| 129 | + not regexp.matches("<script src='foo'></script>") and |
| 130 | + not regexp.matches("<foo>") and |
| 131 | + msg = "This regular expression does not match script tags where the attribute uses single-quotes." |
| 132 | + or |
| 133 | + regexp.matches("<script src='foo'></script>") and |
| 134 | + not regexp.matches("<script src=\"foo\"></script>") and |
| 135 | + not regexp.matches("<foo>") and |
| 136 | + msg = "This regular expression does not match script tags where the attribute uses double-quotes." |
| 137 | + or |
| 138 | + regexp.matches("<script src='foo'></script>") and |
| 139 | + not regexp.matches("<script\tsrc='foo'></script>") and |
| 140 | + not regexp.matches("<foo>") and |
| 141 | + not regexp.matches("<foo src=\"foo\"></foo>") and |
| 142 | + msg = "This regular expression does not match script tags tabs are used between attributes." |
| 143 | + or |
| 144 | + regexp.matches("<script>foo</script>") and |
| 145 | + not RegExpFlags::isIgnoreCase(regexp) and |
| 146 | + not regexp.matches("<foo>") and |
| 147 | + not regexp.matches("<foo ></foo>") and |
| 148 | + ( |
| 149 | + not regexp.matches("<SCRIPT>foo</SCRIPT>") and |
| 150 | + msg = "This regular expression does not match upper case <SCRIPT> tags." |
| 151 | + or |
| 152 | + not regexp.matches("<sCrIpT>foo</ScRiPt>") and |
| 153 | + regexp.matches("<SCRIPT>foo</SCRIPT>") and |
| 154 | + msg = "This regular expression does not match mixed case <sCrIpT> tags." |
| 155 | + ) |
| 156 | + or |
| 157 | + regexp.matches("<script src=\"foo\"></script>") and |
| 158 | + not regexp.matches("<foo>") and |
| 159 | + not regexp.matches("<foo ></foo>") and |
| 160 | + ( |
| 161 | + not regexp.matches("<script src=\"foo\">foo</script >") or |
| 162 | + not regexp.matches("<script src=\"foo\">foo</script foo=\"bar\">") or |
| 163 | + not regexp.matches("<script src=\"foo\">foo</script\t\n bar>") |
| 164 | + ) and |
| 165 | + msg = |
| 166 | + "This regular expression does not match script end tags containing spaces, tabs or newlines." |
| 167 | +} |
0 commit comments