-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathFunctionalityFromUntrustedSource.ql
More file actions
57 lines (47 loc) · 1.88 KB
/
FunctionalityFromUntrustedSource.ql
File metadata and controls
57 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @name Inclusion of untrusted functionality by a HTML element.
* @description Including untrusted functionality by a HTML element
* opens up for potential man-in-the-middle attacks.
* @kind problem
* @problem.severity warning
* @security-severity 8.1
* @precision high
* @id js/functionality-from-untrusted-source
* @tags security
* external/cwe/cwe-830
*/
import javascript
import semmle.javascript.HTML
bindingset[host]
predicate isAllowedHost(string host) { host.toLowerCase().regexpMatch("localhost(:[0-9]+)?/.*") }
bindingset[path]
predicate isUntrustedSourcePath(string path) {
path.substring(0, 2) = "//"
or
exists(string hostPath | hostPath = path.regexpCapture("http://(.*)", 1) |
not isAllowedHost(hostPath)
)
}
abstract class IncludesUntrustedContent extends HTML::Element {
IncludesUntrustedContent() { this = this }
/** Gets an explanation why this source is untrusted. */
abstract string getProblem();
}
/** A script element that refers to untrusted content. */
class ScriptElementWithUntrustedContent extends IncludesUntrustedContent, HTML::ScriptElement {
ScriptElementWithUntrustedContent() {
isUntrustedSourcePath(this.getSourcePath()) and
not exists(string digest | not digest = "" | this.getIntegrityDigest() = digest)
}
override string getProblem() {
result = "script elements should use an https link and/or use the integrity attribute"
}
}
/** An iframe element that includes untrusted content. */
class IframeElementWithUntrustedContent extends HTML::IframeElement, IncludesUntrustedContent {
IframeElementWithUntrustedContent() { isUntrustedSourcePath(this.getSourcePath()) }
override string getProblem() { result = "iframe elements should use an https link" }
}
from IncludesUntrustedContent s, string problem
where problem = s.getProblem()
select s, "HTML-element imports untrusted content (" + problem + ")"