Skip to content

Commit 45b09df

Browse files
committed
JS: Fix regression from global declare vars
1 parent 24af019 commit 45b09df

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

javascript/ql/lib/semmle/javascript/Variables.qll

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ class NamespaceScope extends Scope, @namespace_scope {
126126
override string toString() { result = "namespace scope" }
127127
}
128128

129+
/**
130+
* Holds if `v` is declared in the top-level of a module using a `declare` statement.
131+
*
132+
* For example:
133+
* ```js
134+
* declare var $: any;
135+
* ```
136+
*
137+
* This introduces a `$` variable in the module scope, but it is likely just a way to provide
138+
* a type for a global variable. We therefore treat it as a global variable.
139+
*/
140+
pragma[nomagic]
141+
private predicate isGlobalLike(Variable v) {
142+
v.getScope() instanceof ModuleScope and
143+
forex(VarDecl decl | decl = v.getADeclaration() | decl.isAmbient())
144+
}
145+
129146
/** A variable declared in a scope. */
130147
class Variable extends @variable, LexicalName {
131148
/** Gets the name of this variable. */
@@ -135,7 +152,7 @@ class Variable extends @variable, LexicalName {
135152
override Scope getScope() { variables(this, _, result) }
136153

137154
/** Holds if this is a global variable. */
138-
predicate isGlobal() { this.getScope() instanceof GlobalScope }
155+
predicate isGlobal() { this.getScope() instanceof GlobalScope or isGlobalLike(this) }
139156

140157
/**
141158
* Holds if this is a variable exported from a TypeScript namespace.

javascript/ql/test/query-tests/Security/CWE-079/DomBasedXss/Xss.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
| dragAndDrop.ts:73:29:73:39 | droppedHtml | dragAndDrop.ts:71:27:71:61 | e.dataT ... /html') | dragAndDrop.ts:73:29:73:39 | droppedHtml | Cross-site scripting vulnerability due to $@. | dragAndDrop.ts:71:27:71:61 | e.dataT ... /html') | user-provided value |
6363
| event-handler-receiver.js:2:31:2:83 | '<h2><a ... ></h2>' | event-handler-receiver.js:2:49:2:61 | location.href | event-handler-receiver.js:2:31:2:83 | '<h2><a ... ></h2>' | Cross-site scripting vulnerability due to $@. | event-handler-receiver.js:2:49:2:61 | location.href | user-provided value |
6464
| express.js:6:15:6:33 | req.param("wobble") | express.js:6:15:6:33 | req.param("wobble") | express.js:6:15:6:33 | req.param("wobble") | Cross-site scripting vulnerability due to $@. | express.js:6:15:6:33 | req.param("wobble") | user-provided value |
65+
| jquery-declare-any.ts:6:7:6:17 | window.name | jquery-declare-any.ts:6:7:6:17 | window.name | jquery-declare-any.ts:6:7:6:17 | window.name | Cross-site scripting vulnerability due to $@. | jquery-declare-any.ts:6:7:6:17 | window.name | user-provided value |
66+
| jquery-declare-type.ts:6:7:6:17 | window.name | jquery-declare-type.ts:6:7:6:17 | window.name | jquery-declare-type.ts:6:7:6:17 | window.name | Cross-site scripting vulnerability due to $@. | jquery-declare-type.ts:6:7:6:17 | window.name | user-provided value |
6567
| jquery.js:7:5:7:34 | "<div i ... + "\\">" | jquery.js:2:17:2:40 | documen ... .search | jquery.js:7:5:7:34 | "<div i ... + "\\">" | Cross-site scripting vulnerability due to $@. | jquery.js:2:17:2:40 | documen ... .search | user-provided value |
6668
| jquery.js:8:18:8:34 | "XSS: " + tainted | jquery.js:2:17:2:40 | documen ... .search | jquery.js:8:18:8:34 | "XSS: " + tainted | Cross-site scripting vulnerability due to $@. | jquery.js:2:17:2:40 | documen ... .search | user-provided value |
6769
| jquery.js:10:5:10:40 | "<b>" + ... "</b>" | jquery.js:10:13:10:20 | location | jquery.js:10:5:10:40 | "<b>" + ... "</b>" | Cross-site scripting vulnerability due to $@. | jquery.js:10:13:10:20 | location | user-provided value |
@@ -954,6 +956,8 @@ nodes
954956
| event-handler-receiver.js:2:31:2:83 | '<h2><a ... ></h2>' | semmle.label | '<h2><a ... ></h2>' |
955957
| event-handler-receiver.js:2:49:2:61 | location.href | semmle.label | location.href |
956958
| express.js:6:15:6:33 | req.param("wobble") | semmle.label | req.param("wobble") |
959+
| jquery-declare-any.ts:6:7:6:17 | window.name | semmle.label | window.name |
960+
| jquery-declare-type.ts:6:7:6:17 | window.name | semmle.label | window.name |
957961
| jquery.js:2:7:2:40 | tainted | semmle.label | tainted |
958962
| jquery.js:2:17:2:40 | documen ... .search | semmle.label | documen ... .search |
959963
| jquery.js:4:5:4:11 | tainted | semmle.label | tainted |

javascript/ql/test/query-tests/Security/CWE-079/DomBasedXss/XssWithAdditionalSources.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ nodes
182182
| hana.js:85:35:85:54 | tableRows[0].comment | semmle.label | tableRows[0].comment |
183183
| hana.js:90:33:90:34 | rs | semmle.label | rs |
184184
| hana.js:90:33:90:45 | rs[0].comment | semmle.label | rs[0].comment |
185+
| jquery-declare-any.ts:6:7:6:17 | window.name | semmle.label | window.name |
186+
| jquery-declare-type.ts:6:7:6:17 | window.name | semmle.label | window.name |
185187
| jquery.js:2:7:2:40 | tainted | semmle.label | tainted |
186188
| jquery.js:2:17:2:40 | documen ... .search | semmle.label | documen ... .search |
187189
| jquery.js:4:5:4:11 | tainted | semmle.label | tainted |

javascript/ql/test/query-tests/Security/CWE-079/DomBasedXss/jquery-declare-any.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import 'dummy';
33
declare var $: any;
44

55
function t() {
6-
$(window.name); // $ MISSING: Alert
6+
$(window.name); // $ Alert
77
}

javascript/ql/test/query-tests/Security/CWE-079/DomBasedXss/jquery-declare-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import 'dummy';
33
declare var $: JQueryStatic;
44

55
function t() {
6-
$(window.name); // $ MISSING: Alert
6+
$(window.name); // $ Alert
77
}

0 commit comments

Comments
 (0)