|
| 1 | +/** |
| 2 | + * Provides classes and predicates for working with incomplete blacklist sanitizers. |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | + |
| 7 | +/** |
| 8 | + * An incomplete black-list sanitizer. |
| 9 | + */ |
| 10 | +abstract class IncompleteBlacklistSanitizer extends DataFlow::Node { |
| 11 | + /** |
| 12 | + * Gets a relevant character that is not sanitized by this sanitizer. |
| 13 | + */ |
| 14 | + abstract string getAnUnsanitizedCharacter(); |
| 15 | + |
| 16 | + /** |
| 17 | + * Gets the kind of sanitization this sanitizer performs. |
| 18 | + */ |
| 19 | + abstract string getKind(); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Describes the characters represented by `rep`. |
| 24 | + */ |
| 25 | +string describeCharacters(string rep) { |
| 26 | + rep = "\"" and result = "quotes" |
| 27 | + or |
| 28 | + rep = "&" and result = "ampersands" |
| 29 | + or |
| 30 | + rep = "<" and result = "less-thans" |
| 31 | + or |
| 32 | + rep = ">" and result = "greater-thans" |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * A local sequence of calls to `String.prototype.replace`, |
| 37 | + * represented by the last call. |
| 38 | + */ |
| 39 | +class StringReplaceCallSequence extends DataFlow::CallNode { |
| 40 | + StringReplaceCallSequence() { |
| 41 | + this instanceof StringReplaceCall and |
| 42 | + not exists(getAStringReplaceMethodCall(this)) // terminal |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets a member of this sequence. |
| 47 | + */ |
| 48 | + StringReplaceCall getAMember() { this = getAStringReplaceMethodCall*(result) } |
| 49 | + |
| 50 | + /** Gets a string that is the replacement of this call. */ |
| 51 | + string getAReplacementString() { |
| 52 | + // this is more restrictive than `StringReplaceCall::replaces/2`, in the name of precision |
| 53 | + getAMember().getRawReplacement().getStringValue() = result |
| 54 | + } |
| 55 | + |
| 56 | + /** Gets a string that is being replaced by this call. */ |
| 57 | + string getAReplacedString() { getAMember().getAReplacedString() = result } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * A specialized version of `DataFlow::Node::getAMethodCall` that is |
| 62 | + * restricted to `StringReplaceCall`-nodes. |
| 63 | + */ |
| 64 | +private StringReplaceCall getAStringReplaceMethodCall(StringReplaceCall n) { |
| 65 | + n.getAMethodCall() = result |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Provides predicates and classes for reasoning about HTML sanitization. |
| 70 | + */ |
| 71 | +module HtmlSanitization { |
| 72 | + private predicate fixedGlobalReplacement(StringReplaceCallSequence chain) { |
| 73 | + forall(StringReplaceCall member | member = chain.getAMember() | |
| 74 | + member.isGlobal() and member.getArgument(0) instanceof DataFlow::RegExpLiteralNode |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Gets a HTML-relevant character that is replaced by `chain`. |
| 80 | + */ |
| 81 | + private string getALikelyReplacedCharacter(StringReplaceCallSequence chain) { |
| 82 | + result = "\"" and |
| 83 | + ( |
| 84 | + chain.getAReplacedString() = result or |
| 85 | + chain.getAReplacementString() = """ or |
| 86 | + chain.getAReplacementString() = """ |
| 87 | + ) |
| 88 | + or |
| 89 | + result = "&" and |
| 90 | + ( |
| 91 | + chain.getAReplacedString() = result or |
| 92 | + chain.getAReplacementString() = "&" or |
| 93 | + chain.getAReplacementString() = "(" |
| 94 | + ) |
| 95 | + or |
| 96 | + result = "<" and |
| 97 | + ( |
| 98 | + chain.getAReplacedString() = result or |
| 99 | + chain.getAReplacementString() = "<" or |
| 100 | + chain.getAReplacementString() = "<" |
| 101 | + ) |
| 102 | + or |
| 103 | + result = ">" and |
| 104 | + ( |
| 105 | + chain.getAReplacedString() = result or |
| 106 | + chain.getAReplacementString() = ">" or |
| 107 | + chain.getAReplacementString() = ">" |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * An incomplete sanitizer for HTML-relevant characters. |
| 113 | + */ |
| 114 | + class IncompleteSanitizer extends IncompleteBlacklistSanitizer { |
| 115 | + StringReplaceCallSequence chain; |
| 116 | + string unsanitized; |
| 117 | + |
| 118 | + IncompleteSanitizer() { |
| 119 | + this = chain and |
| 120 | + fixedGlobalReplacement(chain) and |
| 121 | + not getALikelyReplacedCharacter(chain) = unsanitized and |
| 122 | + ( |
| 123 | + // replaces `<` and `>` |
| 124 | + getALikelyReplacedCharacter(chain) = "<" and |
| 125 | + getALikelyReplacedCharacter(chain) = ">" and |
| 126 | + ( |
| 127 | + unsanitized = "\"" |
| 128 | + or |
| 129 | + unsanitized = "&" |
| 130 | + ) |
| 131 | + or |
| 132 | + // replaces '&' and either `<` or `>` |
| 133 | + getALikelyReplacedCharacter(chain) = "&" and |
| 134 | + ( |
| 135 | + getALikelyReplacedCharacter(chain) = ">" and |
| 136 | + unsanitized = ">" |
| 137 | + or |
| 138 | + getALikelyReplacedCharacter(chain) = "<" and |
| 139 | + unsanitized = "<" |
| 140 | + ) |
| 141 | + ) and |
| 142 | + // does not replace special characters that the browser doesn't care for |
| 143 | + not chain.getAReplacedString() = ["!", "#", "*", "?", "@", "|", "~"] and |
| 144 | + /// only replaces explicit characters: exclude character ranges and negated character classes |
| 145 | + not exists(RegExpTerm t | t = chain.getAMember().getRegExp().getRoot().getAChild*() | |
| 146 | + t.(RegExpCharacterClass).isInverted() or |
| 147 | + t instanceof RegExpCharacterRange |
| 148 | + ) and |
| 149 | + // the replacements are either empty or HTML entities |
| 150 | + chain.getAReplacementString().regexpMatch("(?i)(|(&[#a-z0-9]+;))") |
| 151 | + } |
| 152 | + |
| 153 | + override string getKind() { result = "HTML" } |
| 154 | + |
| 155 | + override string getAnUnsanitizedCharacter() { result = unsanitized } |
| 156 | + } |
| 157 | +} |
0 commit comments