|
| 1 | +/** |
| 2 | + * @name Useless regular-expression character escape |
| 3 | + * @description Prepending a backslash to an ordinary character in a string |
| 4 | + * does not have any effect, and may make regular expressions constructed from this string |
| 5 | + * behave unexpectedly. |
| 6 | + * @kind problem |
| 7 | + * @problem.severity error |
| 8 | + * @precision high |
| 9 | + * @id js/useless-regexp-character-escape |
| 10 | + * @tags correctness |
| 11 | + * security |
| 12 | + * external/cwe/cwe-20 |
| 13 | + */ |
| 14 | + |
| 15 | +import javascript |
| 16 | +import semmle.javascript.CharacterEscapes::CharacterEscapes |
| 17 | + |
| 18 | +newtype TRegExpPatternMistake = |
| 19 | + /** |
| 20 | + * A character escape mistake in regular expression string `src` |
| 21 | + * for the character `char` at `index` in `rawStringNode`, explained |
| 22 | + * by `mistake`. |
| 23 | + */ |
| 24 | + TIdentityEscapeInStringMistake( |
| 25 | + RegExpPatternSource src, string char, string mistake, ASTNode rawStringNode, int index |
| 26 | + ) { |
| 27 | + char = getALikelyRegExpPatternMistake(src, mistake, rawStringNode, index) |
| 28 | + } or |
| 29 | + /** |
| 30 | + * A backslash-escaped 'b' at `index` of `rawStringNode` in the |
| 31 | + * regular expression string `src`, indicating intent to use the |
| 32 | + * word-boundary assertion '\b'. |
| 33 | + */ |
| 34 | + TBackspaceInStringMistake(RegExpPatternSource src, ASTNode rawStringNode, int index) { |
| 35 | + exists(string raw, string cooked | |
| 36 | + exists(StringLiteral lit | lit = rawStringNode | |
| 37 | + rawStringNode = src.asExpr() and |
| 38 | + raw = lit.getRawValue() and |
| 39 | + cooked = lit.getStringValue() |
| 40 | + ) |
| 41 | + or |
| 42 | + exists(TemplateElement elem | elem = rawStringNode | |
| 43 | + rawStringNode = src.asExpr().(TemplateLiteral).getAnElement() and |
| 44 | + raw = elem.getRawValue() and |
| 45 | + cooked = elem.getStringValue() |
| 46 | + ) |
| 47 | + | |
| 48 | + "b" = getAnEscapedCharacter(raw, index) and |
| 49 | + // except if the string is exactly \b |
| 50 | + cooked.length() != 1 |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | +/** |
| 55 | + * A character escape mistake in a regular expression string. |
| 56 | + * |
| 57 | + * Implementation note: the main purpose of this class is to associate an |
| 58 | + * exact character location with an alert message, in the name of |
| 59 | + * user-friendly alerts. The implementation can be simplified |
| 60 | + * significantly by only using the enclosing string location as the alert |
| 61 | + * location. |
| 62 | + */ |
| 63 | +class RegExpPatternMistake extends TRegExpPatternMistake { |
| 64 | + /** |
| 65 | + * Holds if this element is at the specified location. |
| 66 | + * The location spans column `startcolumn` of line `startline` to |
| 67 | + * column `endcolumn` of line `endline` in file `filepath`. |
| 68 | + * For more information, see |
| 69 | + * [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html). |
| 70 | + */ |
| 71 | + predicate hasLocationInfo( |
| 72 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 73 | + ) { |
| 74 | + exists(int srcStartcolumn, int srcEndcolumn, int index | |
| 75 | + index = getIndex() and |
| 76 | + getRawStringNode() |
| 77 | + .getLocation() |
| 78 | + .hasLocationInfo(filepath, startline, srcStartcolumn, endline, srcEndcolumn) |
| 79 | + | |
| 80 | + ( |
| 81 | + if startline = endline |
| 82 | + then startcolumn = srcStartcolumn + index - 1 and endcolumn = srcStartcolumn + index |
| 83 | + else ( |
| 84 | + startcolumn = srcStartcolumn and endcolumn = srcEndcolumn |
| 85 | + ) |
| 86 | + ) |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + /** Gets a textual representation of this element. */ |
| 91 | + string toString() { result = getMessage() } |
| 92 | + |
| 93 | + abstract ASTNode getRawStringNode(); |
| 94 | + |
| 95 | + abstract RegExpPatternSource getSrc(); |
| 96 | + |
| 97 | + abstract int getIndex(); |
| 98 | + |
| 99 | + abstract string getMessage(); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * An identity-escaped character that indicates programmer intent to |
| 104 | + * do something special in a regular expression. |
| 105 | + */ |
| 106 | +class IdentityEscapeInStringMistake extends RegExpPatternMistake, TIdentityEscapeInStringMistake { |
| 107 | + RegExpPatternSource src; |
| 108 | + |
| 109 | + string char; |
| 110 | + |
| 111 | + string mistake; |
| 112 | + |
| 113 | + int index; |
| 114 | + |
| 115 | + ASTNode rawStringNode; |
| 116 | + |
| 117 | + IdentityEscapeInStringMistake() { |
| 118 | + this = TIdentityEscapeInStringMistake(src, char, mistake, rawStringNode, index) |
| 119 | + } |
| 120 | + |
| 121 | + override string getMessage() { |
| 122 | + result = "'\\" + char + "' is equivalent to just '" + char + "', so the sequence " + mistake |
| 123 | + } |
| 124 | + |
| 125 | + override int getIndex() { result = index } |
| 126 | + |
| 127 | + override RegExpPatternSource getSrc() { result = src } |
| 128 | + |
| 129 | + override ASTNode getRawStringNode() { result = rawStringNode } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * A backspace as '\b' in a regular expression string, indicating |
| 134 | + * programmer intent to use the word-boundary assertion '\b'. |
| 135 | + */ |
| 136 | +class BackspaceInStringMistake extends RegExpPatternMistake, TBackspaceInStringMistake { |
| 137 | + RegExpPatternSource src; |
| 138 | + |
| 139 | + int index; |
| 140 | + |
| 141 | + ASTNode rawStringNode; |
| 142 | + |
| 143 | + BackspaceInStringMistake() { this = TBackspaceInStringMistake(src, rawStringNode, index) } |
| 144 | + |
| 145 | + override string getMessage() { result = "'\\b' is a backspace, and not a word-boundary assertion" } |
| 146 | + |
| 147 | + override int getIndex() { result = index } |
| 148 | + |
| 149 | + override RegExpPatternSource getSrc() { result = src } |
| 150 | + |
| 151 | + override ASTNode getRawStringNode() { result = rawStringNode } |
| 152 | +} |
| 153 | + |
| 154 | +from RegExpPatternMistake mistake |
| 155 | +select mistake, "The escape sequence " + mistake.getMessage() + " when it is used in a $@.", |
| 156 | + mistake.getSrc().getAParse(), "regular expression" |
0 commit comments