|
13 | 13 |
|
14 | 14 | import javascript |
15 | 15 | import semmle.javascript.HTML |
| 16 | +import semmle.javascript.dataflow.TaintTracking |
16 | 17 |
|
17 | | -bindingset[host] |
18 | | -predicate isLocalhostPrefix(string host) { |
19 | | - host.toLowerCase() |
20 | | - .regexpMatch([ |
21 | | - "localhost(:[0-9]+)?/.*", "127.0.0.1(:[0-9]+)?/.*", "::1/.*", "\\[::1\\]:[0-9]+/.*" |
22 | | - ]) |
23 | | -} |
| 18 | +module Generic { |
| 19 | + /** A `CallNode` that creates an element of kind `name`. */ |
| 20 | + predicate isCreateElementNode(DataFlow::CallNode call, string name) { |
| 21 | + call = DataFlow::globalVarRef("document").getAMethodCall("createElement") and |
| 22 | + call.getArgument(0).getStringValue().toLowerCase() = name |
| 23 | + } |
24 | 24 |
|
25 | | -/** A path that is vulnerable to a MITM attack. */ |
26 | | -bindingset[url] |
27 | | -predicate isUntrustedSourceUrl(string url) { |
28 | | - url.substring(0, 2) = "//" |
29 | | - or |
30 | | - exists(string hostPath | hostPath = url.regexpCapture("http://(.*)", 1) | |
31 | | - not isLocalhostPrefix(hostPath) |
32 | | - ) |
33 | | -} |
| 25 | + /** |
| 26 | + * True if `rhs` is being assigned to the `propName` property of an HTML |
| 27 | + * element created by `createElementCall`. |
| 28 | + */ |
| 29 | + predicate isPropWrite(DataFlow::CallNode createElementCall, string propName, DataFlow::Node rhs) { |
| 30 | + exists(DataFlow::PropWrite assignment | |
| 31 | + isCreateElementNode(createElementCall, _) and |
| 32 | + assignment.writes(createElementCall.getALocalUse(), propName, rhs) |
| 33 | + ) |
| 34 | + } |
34 | 35 |
|
35 | | -/** A path that needs an integrity check — even with https. */ |
36 | | -bindingset[url] |
37 | | -predicate isCdnUrlWithCheckingRequired(string url) { |
38 | | - // Some CDN URLs are required to have an integrity attribute. We only add CDNs to that list |
39 | | - // that recommend integrity-checking. |
40 | | - url.regexpMatch([ |
41 | | - "^https?://code\\.jquery\\.com/.*\\.js$", "^https?://cdnjs\\.cloudflare\\.com/.*\\.js$", |
42 | | - "^https?://cdnjs\\.com/.*\\.js$" |
43 | | - ]) |
44 | | -} |
| 36 | + /** |
| 37 | + * A `createElement` call that creates a `<script ../>` element which never |
| 38 | + * has its `integrity` attribute set locally. |
| 39 | + */ |
| 40 | + predicate isCreateScriptNodeWoIntegrityCheck(DataFlow::CallNode createCall) { |
| 41 | + isCreateElementNode(createCall, "script") and |
| 42 | + not exists(DataFlow::Node rhs | isPropWrite(createCall, "integrity", rhs)) |
| 43 | + } |
45 | 44 |
|
46 | | -abstract class IncludesUntrustedContent extends HTML::Element { |
47 | | - /** Gets an explanation why this source is untrusted. */ |
48 | | - abstract string getProblem(); |
| 45 | + /** A location that adds a reference to an untrusted source. */ |
| 46 | + abstract class AddsUntrustedUrl extends Locatable { |
| 47 | + /** Gets an explanation why this source is untrusted. */ |
| 48 | + abstract string getProblem(); |
| 49 | + } |
49 | 50 | } |
50 | 51 |
|
51 | | -/** A script element that refers to untrusted content. */ |
52 | | -class ScriptElementWithUntrustedContent extends IncludesUntrustedContent, HTML::ScriptElement { |
53 | | - ScriptElementWithUntrustedContent() { |
54 | | - not exists(string digest | not digest = "" | this.getIntegrityDigest() = digest) and |
55 | | - isUntrustedSourceUrl(this.getSourcePath()) |
| 52 | +module StaticCreation { |
| 53 | + bindingset[host] |
| 54 | + predicate isLocalhostPrefix(string host) { |
| 55 | + host.toLowerCase() |
| 56 | + .regexpMatch([ |
| 57 | + "localhost(:[0-9]+)?/.*", "127.0.0.1(:[0-9]+)?/.*", "::1/.*", "\\[::1\\]:[0-9]+/.*" |
| 58 | + ]) |
56 | 59 | } |
57 | 60 |
|
58 | | - override string getProblem() { |
59 | | - result = "script elements should use an HTTPS url and/or use the integrity attribute" |
| 61 | + /** A path that is vulnerable to a MITM attack. */ |
| 62 | + bindingset[url] |
| 63 | + predicate isUntrustedSourceUrl(string url) { |
| 64 | + exists(string hostPath | hostPath = url.regexpCapture("http://(.*)", 1) | |
| 65 | + not isLocalhostPrefix(hostPath) |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + /** A path that needs an integrity check — even with https. */ |
| 70 | + bindingset[url] |
| 71 | + predicate isCdnUrlWithCheckingRequired(string url) { |
| 72 | + // Some CDN URLs are required to have an integrity attribute. We only add CDNs to that list |
| 73 | + // that recommend integrity-checking. |
| 74 | + url.regexpMatch([ |
| 75 | + "^https?://code\\.jquery\\.com/.*\\.js$", "^https?://cdnjs\\.cloudflare\\.com/.*\\.js$", |
| 76 | + "^https?://cdnjs\\.com/.*\\.js$" |
| 77 | + ]) |
| 78 | + } |
| 79 | + |
| 80 | + /** A script element that refers to untrusted content. */ |
| 81 | + class ScriptElementWithUntrustedContent extends Generic::AddsUntrustedUrl, HTML::ScriptElement { |
| 82 | + ScriptElementWithUntrustedContent() { |
| 83 | + not exists(string digest | not digest = "" | this.getIntegrityDigest() = digest) and |
| 84 | + isUntrustedSourceUrl(this.getSourcePath()) |
| 85 | + } |
| 86 | + |
| 87 | + override string getProblem() { |
| 88 | + result = "script elements should use an HTTPS url and/or use the integrity attribute" |
| 89 | + } |
60 | 90 | } |
61 | | -} |
62 | 91 |
|
63 | | -/** A script element that refers to untrusted content. */ |
64 | | -class CDNScriptElementWithUntrustedContent extends IncludesUntrustedContent, HTML::ScriptElement { |
65 | | - CDNScriptElementWithUntrustedContent() { |
66 | | - not exists(string digest | not digest = "" | this.getIntegrityDigest() = digest) and |
67 | | - isCdnUrlWithCheckingRequired(this.getSourcePath()) |
| 92 | + /** A script element that refers to untrusted content. */ |
| 93 | + class CDNScriptElementWithUntrustedContent extends Generic::AddsUntrustedUrl, HTML::ScriptElement { |
| 94 | + CDNScriptElementWithUntrustedContent() { |
| 95 | + not exists(string digest | not digest = "" | this.getIntegrityDigest() = digest) and |
| 96 | + isCdnUrlWithCheckingRequired(this.getSourcePath()) |
| 97 | + } |
| 98 | + |
| 99 | + override string getProblem() { |
| 100 | + result = |
| 101 | + "script elements that depend on this CDN should use an HTTPS url and use the integrity attribute" |
| 102 | + } |
68 | 103 | } |
69 | 104 |
|
70 | | - override string getProblem() { |
71 | | - result = |
72 | | - "script elements that depend on this CDN should use an HTTPS url and use the integrity attribute" |
| 105 | + /** An iframe element that includes untrusted content. */ |
| 106 | + class IframeElementWithUntrustedContent extends HTML::IframeElement, Generic::AddsUntrustedUrl { |
| 107 | + IframeElementWithUntrustedContent() { isUntrustedSourceUrl(this.getSourcePath()) } |
| 108 | + |
| 109 | + override string getProblem() { result = "iframe elements should use an HTTPS url" } |
73 | 110 | } |
74 | 111 | } |
75 | 112 |
|
76 | | -/** An iframe element that includes untrusted content. */ |
77 | | -class IframeElementWithUntrustedContent extends HTML::IframeElement, IncludesUntrustedContent { |
78 | | - IframeElementWithUntrustedContent() { isUntrustedSourceUrl(this.getSourcePath()) } |
| 113 | +module DynamicCreation { |
| 114 | + import DataFlow::TypeTracker |
| 115 | + |
| 116 | + predicate isUnsafeSourceLiteral(DataFlow::Node source) { |
| 117 | + exists(StringLiteral s | source = s.flow() | |
| 118 | + s.getValue().toLowerCase() = "http:" + any(string rest) |
| 119 | + ) |
| 120 | + } |
79 | 121 |
|
80 | | - override string getProblem() { result = "iframe elements should use an HTTPS url" } |
| 122 | + DataFlow::Node urlTrackedFromUnsafeSourceLiteral(DataFlow::TypeTracker t) { |
| 123 | + t.start() and isUnsafeSourceLiteral(result) |
| 124 | + or |
| 125 | + exists(DataFlow::TypeTracker t2, DataFlow::Node prev | |
| 126 | + prev = urlTrackedFromUnsafeSourceLiteral(t2) |
| 127 | + | |
| 128 | + not exists(string httpsUrl | httpsUrl.toLowerCase() = "https:" + any(string rest) | |
| 129 | + // when the result may have a string value starting with https, |
| 130 | + // we're most likely with an assignment like: |
| 131 | + // e.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js' |
| 132 | + // these assignments, we don't want to fix — once the browser is using http, |
| 133 | + // MITM attacks are possible anyway. |
| 134 | + result.mayHaveStringValue(httpsUrl) |
| 135 | + ) and |
| 136 | + ( |
| 137 | + t2 = t.smallstep(prev, result) |
| 138 | + or |
| 139 | + TaintTracking::sharedTaintStep(prev, result) and |
| 140 | + t = t2 |
| 141 | + ) |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + DataFlow::Node urlTrackedFromUnsafeSourceLiteral() { |
| 146 | + result = urlTrackedFromUnsafeSourceLiteral(DataFlow::TypeTracker::end()) |
| 147 | + } |
| 148 | + |
| 149 | + predicate isAssignedToSrcAttribute(string name, DataFlow::Node sink) { |
| 150 | + exists(DataFlow::CallNode createElementCall | |
| 151 | + name = "script" and |
| 152 | + Generic::isCreateScriptNodeWoIntegrityCheck(createElementCall) and |
| 153 | + Generic::isPropWrite(createElementCall, "src", sink) |
| 154 | + or |
| 155 | + name = "iframe" and |
| 156 | + Generic::isCreateElementNode(createElementCall, "iframe") and |
| 157 | + Generic::isPropWrite(createElementCall, "src", sink) |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + class IframeOrScriptSrcAssignment extends Expr, Generic::AddsUntrustedUrl { |
| 162 | + string name; |
| 163 | + |
| 164 | + IframeOrScriptSrcAssignment() { |
| 165 | + exists(DataFlow::Node n | n.asExpr() = this | |
| 166 | + DynamicCreation::isAssignedToSrcAttribute(name, n) and |
| 167 | + n = DynamicCreation::urlTrackedFromUnsafeSourceLiteral() |
| 168 | + ) |
| 169 | + } |
| 170 | + |
| 171 | + override string getProblem() { |
| 172 | + name = "script" and |
| 173 | + result = "script elements should use an HTTPS url and/or use the integrity attribute" |
| 174 | + or |
| 175 | + name = "iframe" and result = "iframe elements should use an HTTPS url" |
| 176 | + } |
| 177 | + } |
81 | 178 | } |
82 | 179 |
|
83 | | -from IncludesUntrustedContent s, string problem |
84 | | -where problem = s.getProblem() |
85 | | -select s, "HTML-element uses untrusted content (" + problem + ")" |
| 180 | +from Generic::AddsUntrustedUrl s |
| 181 | +select s, "HTML-element uses untrusted content (" + s.getProblem() + ")" |
0 commit comments