Skip to content

Commit d2335b6

Browse files
author
Stephan Brandauer
committed
stylistic improvements after review
1 parent 9aec443 commit d2335b6

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

javascript/ql/src/Security/CWE-830/FunctionalityFromUntrustedSource.qhelp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<overview>
66
<p>
77

8-
Including functionality from an external source via an http URL may
8+
Including functionality from an external source via an <code>http</code> URL may
99
allow an attacker to inject malicious code via a MITM (man-in-the-middle) attack.
1010

1111
</p>
@@ -15,24 +15,25 @@
1515
<recommendation>
1616
<p>
1717

18-
When including external pages or behaviour, use <em>https</em> URLs to make sure you're
19-
getting the intended data, or users will be vulnerable to MITM attacks.
18+
When including external pages or behaviour, use <code>https</code> URLs
19+
to make sure you're getting the intended data, or users will be vulnerable
20+
to MITM attacks.
2021

2122
</p>
2223

2324
<p>
2425

25-
When including external behaviour in iframe or script elements, using http URLs is
26-
unsafe because the request sent may be intercepted by an attacker, and malicious data
27-
may be sent back in reply.
26+
When including external behaviour in iframe or script elements, using
27+
<code>http</code> URLs is unsafe because the request sent may be intercepted
28+
by an attacker, and malicious data may be sent back in reply.
2829

2930
</p>
3031

3132

3233
<p>
3334

34-
Even when https is used, an attacker might still compromise the server the page is
35-
receving data from.
35+
Even when <code>https</code> is used, an attacker might still compromise the
36+
server the page is receving data from.
3637

3738
When including scripts from a CDN (content-delivery network), it is therefore recommended
3839
to set the integrity-attribute on the script tag to the hash of the script you're expecting

javascript/ql/src/Security/CWE-830/FunctionalityFromUntrustedSource.ql

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,13 @@ module Generic {
2222
call.getArgument(0).getStringValue().toLowerCase() = name
2323
}
2424

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-
}
35-
3625
/**
3726
* A `createElement` call that creates a `<script ../>` element which never
3827
* has its `integrity` attribute set locally.
3928
*/
4029
predicate isCreateScriptNodeWoIntegrityCheck(DataFlow::CallNode createCall) {
4130
isCreateElementNode(createCall, "script") and
42-
not exists(DataFlow::Node rhs | isPropWrite(createCall, "integrity", rhs))
31+
not exists(createCall.getAPropertyWrite("integrity"))
4332
}
4433

4534
/** A location that adds a reference to an untrusted source. */
@@ -54,14 +43,14 @@ module StaticCreation {
5443
predicate isLocalhostPrefix(string host) {
5544
host.toLowerCase()
5645
.regexpMatch([
57-
"localhost(:[0-9]+)?/.*", "127.0.0.1(:[0-9]+)?/.*", "::1/.*", "\\[::1\\]:[0-9]+/.*"
46+
"(?i)localhost(:[0-9]+)?/.*", "127.0.0.1(:[0-9]+)?/.*", "::1/.*", "\\[::1\\]:[0-9]+/.*"
5847
])
5948
}
6049

6150
/** A path that is vulnerable to a MITM attack. */
6251
bindingset[url]
6352
predicate isUntrustedSourceUrl(string url) {
64-
exists(string hostPath | hostPath = url.regexpCapture("http://(.*)", 1) |
53+
exists(string hostPath | hostPath = url.regexpCapture("(?i)http://(.*)", 1) |
6554
not isLocalhostPrefix(hostPath)
6655
)
6756
}
@@ -71,10 +60,11 @@ module StaticCreation {
7160
predicate isCdnUrlWithCheckingRequired(string url) {
7261
// Some CDN URLs are required to have an integrity attribute. We only add CDNs to that list
7362
// that recommend integrity-checking.
74-
url.regexpMatch([
75-
"^https?://code\\.jquery\\.com/.*\\.js$", "^https?://cdnjs\\.cloudflare\\.com/.*\\.js$",
76-
"^https?://cdnjs\\.com/.*\\.js$"
77-
])
63+
url.regexpMatch("(?i)" +
64+
[
65+
"^https?://code\\.jquery\\.com/.*\\.js$", "^https?://cdnjs\\.cloudflare\\.com/.*\\.js$",
66+
"^https?://cdnjs\\.com/.*\\.js$"
67+
])
7868
}
7969

8070
/** A script element that refers to untrusted content. */
@@ -85,7 +75,7 @@ module StaticCreation {
8575
}
8676

8777
override string getProblem() {
88-
result = "script elements should use an HTTPS url and/or use the integrity attribute"
78+
result = "script elements should use an 'https:' URL and/or use the integrity attribute"
8979
}
9080
}
9181

@@ -98,25 +88,23 @@ module StaticCreation {
9888

9989
override string getProblem() {
10090
result =
101-
"script elements that depend on this CDN should use an HTTPS url and use the integrity attribute"
91+
"script elements that depend on this CDN should use an 'https:' URL and use the integrity attribute"
10292
}
10393
}
10494

10595
/** An iframe element that includes untrusted content. */
10696
class IframeElementWithUntrustedContent extends HTML::IframeElement, Generic::AddsUntrustedUrl {
10797
IframeElementWithUntrustedContent() { isUntrustedSourceUrl(this.getSourcePath()) }
10898

109-
override string getProblem() { result = "iframe elements should use an HTTPS url" }
99+
override string getProblem() { result = "iframe elements should use an 'https:' URL" }
110100
}
111101
}
112102

113103
module DynamicCreation {
114104
import DataFlow::TypeTracker
115105

116106
predicate isUnsafeSourceLiteral(DataFlow::Node source) {
117-
exists(StringLiteral s | source = s.flow() |
118-
s.getValue().toLowerCase() = "http:" + any(string rest)
119-
)
107+
exists(StringLiteral s | source = s.flow() | s.getValue().regexpMatch("(?i)http:.*"))
120108
}
121109

122110
DataFlow::Node urlTrackedFromUnsafeSourceLiteral(DataFlow::TypeTracker t) {
@@ -150,11 +138,11 @@ module DynamicCreation {
150138
exists(DataFlow::CallNode createElementCall |
151139
name = "script" and
152140
Generic::isCreateScriptNodeWoIntegrityCheck(createElementCall) and
153-
Generic::isPropWrite(createElementCall, "src", sink)
141+
sink = createElementCall.getAPropertyWrite("src").getRhs()
154142
or
155143
name = "iframe" and
156144
Generic::isCreateElementNode(createElementCall, "iframe") and
157-
Generic::isPropWrite(createElementCall, "src", sink)
145+
sink = createElementCall.getAPropertyWrite("src").getRhs()
158146
)
159147
}
160148

@@ -170,9 +158,9 @@ module DynamicCreation {
170158

171159
override string getProblem() {
172160
name = "script" and
173-
result = "script elements should use an HTTPS url and/or use the integrity attribute"
161+
result = "script elements should use an 'https:' URL and/or use the integrity attribute"
174162
or
175-
name = "iframe" and result = "iframe elements should use an HTTPS url"
163+
name = "iframe" and result = "iframe elements should use an 'https:' URL"
176164
}
177165
}
178166
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| DynamicCreationOfUntrustedSourceUse.html:18:26:18:50 | 'http:/ ... e.com/' | HTML-element uses untrusted content (iframe elements should use an HTTPS url) |
2-
| DynamicCreationOfUntrustedSourceUse.html:29:27:29:40 | getUrl('v123') | HTML-element uses untrusted content (iframe elements should use an HTTPS url) |
3-
| StaticCreationOfUntrustedSourceUse.html:6:9:6:56 | <script>...</> | HTML-element uses untrusted content (script elements should use an HTTPS url and/or use the integrity attribute) |
4-
| StaticCreationOfUntrustedSourceUse.html:9:9:9:58 | <iframe>...</> | HTML-element uses untrusted content (iframe elements should use an HTTPS url) |
5-
| StaticCreationOfUntrustedSourceUse.html:21:9:21:155 | <script>...</> | HTML-element uses untrusted content (script elements that depend on this CDN should use an HTTPS url and use the integrity attribute) |
1+
| DynamicCreationOfUntrustedSourceUse.html:18:26:18:50 | 'http:/ ... e.com/' | HTML-element uses untrusted content (iframe elements should use an 'https:' URL) |
2+
| DynamicCreationOfUntrustedSourceUse.html:29:27:29:40 | getUrl('v123') | HTML-element uses untrusted content (iframe elements should use an 'https:' URL) |
3+
| StaticCreationOfUntrustedSourceUse.html:6:9:6:56 | <script>...</> | HTML-element uses untrusted content (script elements should use an 'https:' URL and/or use the integrity attribute) |
4+
| StaticCreationOfUntrustedSourceUse.html:9:9:9:58 | <iframe>...</> | HTML-element uses untrusted content (iframe elements should use an 'https:' URL) |
5+
| StaticCreationOfUntrustedSourceUse.html:21:9:21:155 | <script>...</> | HTML-element uses untrusted content (script elements that depend on this CDN should use an 'https:' URL and use the integrity attribute) |

0 commit comments

Comments
 (0)