Skip to content

Commit ae0aeb9

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

File tree

7 files changed

+48
-8
lines changed

7 files changed

+48
-8
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,26 @@ class Variable extends @variable, LexicalName {
134134
/** Gets the scope this variable is declared in. */
135135
override Scope getScope() { variables(this, _, result) }
136136

137+
/**
138+
* Holds if this variable is declared in the top-level of a module using a `declare` statement.
139+
*
140+
* For example:
141+
* ```js
142+
* declare var $: any;
143+
* ```
144+
*
145+
* Such variables are generally treated as a global variables, except for type-checking related purposes.
146+
*/
147+
pragma[nomagic]
148+
predicate isTopLevelWithAmbientDeclaration() {
149+
this.getScope() instanceof ModuleScope and
150+
forex(VarDecl decl | decl = this.getADeclaration() | decl.isAmbient())
151+
}
152+
137153
/** Holds if this is a global variable. */
138-
predicate isGlobal() { this.getScope() instanceof GlobalScope }
154+
predicate isGlobal() {
155+
this.getScope() instanceof GlobalScope or this.isTopLevelWithAmbientDeclaration()
156+
}
139157

140158
/**
141159
* Holds if this is a variable exported from a TypeScript namespace.

javascript/ql/lib/semmle/javascript/internal/NameResolution.qll

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module NameResolution {
2828
Location getLocation() {
2929
result = this.(AstNode).getLocation()
3030
or
31-
result = this.(LocalVariable).getLocation()
31+
result = this.(LocalVariableLike).getLocation()
3232
or
3333
result = this.(JSDocTypeExpr).getLocation()
3434
}
@@ -47,6 +47,22 @@ module NameResolution {
4747
}
4848
}
4949

50+
/**
51+
* A local variable, or a top-level variable that acts as a global variable due to an ambient declaration.
52+
*/
53+
class LocalVariableLike extends Variable {
54+
LocalVariableLike() { this.isLocal() or this.isTopLevelWithAmbientDeclaration() }
55+
56+
Location getLocation() {
57+
result =
58+
min(Location loc |
59+
loc = this.getADeclaration().getLocation()
60+
|
61+
loc order by loc.getStartLine(), loc.getStartColumn()
62+
)
63+
}
64+
}
65+
5066
/**
5167
* Holds if values/namespaces/types in `node1` can flow to values/namespaces/types in `node2`.
5268
*/
@@ -224,7 +240,7 @@ module NameResolution {
224240
/**
225241
* A local variable with exactly one definition, not counting implicit initialization.
226242
*/
227-
private class EffectivelyConstantVariable extends LocalVariable {
243+
private class EffectivelyConstantVariable extends LocalVariableLike {
228244
EffectivelyConstantVariable() {
229245
count(SsaExplicitDefinition ssa | ssa.getSourceVariable() = this) <= 1 // count may be zero if ambient
230246
}
@@ -294,7 +310,7 @@ module NameResolution {
294310
* Holds if `value` is stored in `target.prop`. Only needs to recognise assignments
295311
* that are also recognised by JSDoc tooling such as the Closure compiler.
296312
*/
297-
private predicate storeToVariable(Expr value, string prop, LocalVariable target) {
313+
private predicate storeToVariable(Expr value, string prop, LocalVariableLike target) {
298314
exists(AssignExpr assign |
299315
// exports.name = value
300316
assign.getLhs().(PropAccess).accesses(target.getAnAccess(), prop) and

javascript/ql/lib/semmle/javascript/internal/TypeResolution.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ module TypeResolution {
190190
}
191191

192192
predicate contextualType(Node value, Node type) {
193-
exists(LocalVariable v |
193+
exists(LocalVariableLike v |
194194
type = v.getADeclaration().getTypeAnnotation() and
195195
value = v.getAnAssignedExpr()
196196
)
@@ -239,7 +239,7 @@ module TypeResolution {
239239
// ValueFlow::step is restricted to variables with at most one assignment. Allow the type annotation
240240
// of a variable to propagate to its uses, even if the variable has multiple assignments.
241241
type = decl.getTypeAnnotation() and
242-
value = decl.getVariable().(LocalVariable).getAnAccess()
242+
value = decl.getVariable().(LocalVariableLike).getAnAccess()
243243
)
244244
or
245245
exists(MemberDeclaration member |

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)