|
| 1 | +/** |
| 2 | + * @name Double escaping or unescaping |
| 3 | + * @description When escaping special characters using a meta-character like backslash or |
| 4 | + * ampersand, the meta-character has to be escaped first to avoid double-escaping, |
| 5 | + * and conversely it has to be unescaped last to avoid double-unescaping. |
| 6 | + * @kind problem |
| 7 | + * @problem.severity warning |
| 8 | + * @precision high |
| 9 | + * @id js/double-escaping |
| 10 | + * @tags correctness |
| 11 | + * security |
| 12 | + * external/cwe/cwe-116 |
| 13 | + * external/cwe/cwe-20 |
| 14 | + */ |
| 15 | + |
| 16 | +import javascript |
| 17 | + |
| 18 | +/** |
| 19 | + * Holds if `rl` is a simple constant, which is bound to the result of the predicate. |
| 20 | + * |
| 21 | + * For example, `/a/g` has string value `"a"` and `/abc/` has string value `"abc"`, |
| 22 | + * while `/ab?/` and `/a(?=b)/` do not have a string value. |
| 23 | + * |
| 24 | + * Flags are ignored, so `/a/i` is still considered to have string value `"a"`, |
| 25 | + * even though it also matches `"A"`. |
| 26 | + * |
| 27 | + * Note the somewhat subtle use of monotonic aggregate semantics, which makes the |
| 28 | + * `strictconcat` fail if one of the children of the root is not a constant (legacy |
| 29 | + * semantics would simply skip such children). |
| 30 | + */ |
| 31 | +language[monotonicAggregates] |
| 32 | +string getStringValue(RegExpLiteral rl) { |
| 33 | + exists (RegExpTerm root | root = rl.getRoot() | |
| 34 | + result = root.(RegExpConstant).getValue() |
| 35 | + or |
| 36 | + result = strictconcat(RegExpTerm ch, int i | |
| 37 | + ch = root.(RegExpSequence).getChild(i) | |
| 38 | + ch.(RegExpConstant).getValue() order by i |
| 39 | + ) |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Gets a predecessor of `nd` that is not an SSA phi node. |
| 45 | + */ |
| 46 | +DataFlow::Node getASimplePredecessor(DataFlow::Node nd) { |
| 47 | + result = nd.getAPredecessor() and |
| 48 | + not nd.(DataFlow::SsaDefinitionNode).getSsaVariable().getDefinition() instanceof SsaPhiNode |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Holds if `metachar` is a meta-character that is used to escape special characters |
| 53 | + * into a form described by regular expression `regex`. |
| 54 | + */ |
| 55 | +predicate escapingScheme(string metachar, string regex) { |
| 56 | + metachar = "&" and regex = "&.*;" |
| 57 | + or |
| 58 | + metachar = "%" and regex = "%.*" |
| 59 | + or |
| 60 | + metachar = "\\" and regex = "\\\\.*" |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * A call to `String.prototype.replace` that replaces all instances of a pattern. |
| 65 | + */ |
| 66 | +class Replacement extends DataFlow::Node { |
| 67 | + RegExpLiteral pattern; |
| 68 | + |
| 69 | + Replacement() { |
| 70 | + exists (DataFlow::MethodCallNode mcn | this = mcn | |
| 71 | + mcn.getMethodName() = "replace" and |
| 72 | + mcn.getArgument(0).asExpr() = pattern and |
| 73 | + mcn.getNumArgument() = 2 and |
| 74 | + pattern.isGlobal() |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Holds if this replacement replaces the string `input` with `output`. |
| 80 | + */ |
| 81 | + predicate replaces(string input, string output) { |
| 82 | + exists (DataFlow::MethodCallNode mcn | |
| 83 | + mcn = this and |
| 84 | + input = getStringValue(pattern) and |
| 85 | + output = mcn.getArgument(1).asExpr().getStringValue() |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Holds if this replacement escapes `char` using `metachar`. |
| 91 | + * |
| 92 | + * For example, during HTML entity escaping `<` is escaped (to `<`) |
| 93 | + * using `&`. |
| 94 | + */ |
| 95 | + predicate escapes(string char, string metachar) { |
| 96 | + exists (string regexp, string repl | |
| 97 | + escapingScheme(metachar, regexp) and |
| 98 | + replaces(char, repl) and |
| 99 | + repl.regexpMatch(regexp) |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Holds if this replacement unescapes `char` using `metachar`. |
| 105 | + * |
| 106 | + * For example, during HTML entity unescaping `<` is unescaped (from |
| 107 | + * `<`) using `<`. |
| 108 | + */ |
| 109 | + predicate unescapes(string metachar, string char) { |
| 110 | + exists (string regexp, string orig | |
| 111 | + escapingScheme(metachar, regexp) and |
| 112 | + replaces(orig, char) and |
| 113 | + orig.regexpMatch(regexp) |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Gets the previous replacement in this chain of replacements. |
| 119 | + */ |
| 120 | + Replacement getPreviousReplacement() { |
| 121 | + result = getASimplePredecessor*(this.(DataFlow::MethodCallNode).getReceiver()) |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Gets an earlier replacement in this chain of replacements that |
| 126 | + * performs an escaping. |
| 127 | + */ |
| 128 | + Replacement getAnEarlierEscaping(string metachar) { |
| 129 | + exists (Replacement pred | pred = this.getPreviousReplacement() | |
| 130 | + if pred.escapes(_, metachar) then |
| 131 | + result = pred |
| 132 | + else |
| 133 | + result = pred.getAnEarlierEscaping(metachar) |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Gets an earlier replacement in this chain of replacements that |
| 139 | + * performs a unescaping. |
| 140 | + */ |
| 141 | + Replacement getALaterUnescaping(string metachar) { |
| 142 | + exists (Replacement succ | this = succ.getPreviousReplacement() | |
| 143 | + if succ.unescapes(metachar, _) then |
| 144 | + result = succ |
| 145 | + else |
| 146 | + result = succ.getALaterUnescaping(metachar) |
| 147 | + ) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +from Replacement primary, Replacement supplementary, string message, string metachar |
| 152 | +where primary.escapes(metachar, _) and |
| 153 | + supplementary = primary.getAnEarlierEscaping(metachar) and |
| 154 | + message = "may double-escape '" + metachar + "' characters from $@" |
| 155 | + or |
| 156 | + primary.unescapes(_, metachar) and |
| 157 | + supplementary = primary.getALaterUnescaping(metachar) and |
| 158 | + message = "may produce '" + metachar + "' characters that are double-unescaped $@" |
| 159 | +select primary, "This replacement " + message + ".", supplementary, "here" |
0 commit comments