Skip to content

Commit 69d6df7

Browse files
committed
make globalVarRef non recursive
1 parent 06091e5 commit 69d6df7

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

javascript/ql/src/semmle/javascript/dataflow/Nodes.qll

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,22 +365,38 @@ DataFlow::SourceNode globalObjectRef() {
365365
)
366366
or
367367
// DOM
368-
result = globalVarRef("window")
368+
result = globalVariable("window")
369369
or
370370
// Node.js
371-
result = globalVarRef("global")
371+
result = globalVariable("global")
372372
or
373373
// DOM and service workers
374-
result = globalVarRef("self")
374+
result = globalVariable("self")
375375
or
376376
// ECMAScript 2020
377-
result = globalVarRef("globalThis")
377+
result = globalVariable("globalThis")
378378
or
379379
// `require("global")`
380380
result = moduleImport("global")
381381
or
382382
// Closure library - based on AST to avoid recursion with Closure library model
383-
result = globalVarRef("goog").getAPropertyRead("global")
383+
result = globalVariable("goog").getAPropertyRead("global")
384+
}
385+
386+
/**
387+
* Gets a reference to a global variable `name`.
388+
* For example, if `name` is "foo":
389+
* ```js
390+
* foo
391+
* require('global/foo')
392+
* ```
393+
*/
394+
private DataFlow::SourceNode globalVariable(string name) {
395+
result.(GlobalVarRefNode).getName() = name
396+
or
397+
// `require("global/document")` or `require("global/window")`
398+
(name = "document" or name = "window") and
399+
result = moduleImport("global/" + name)
384400
}
385401

386402
/**
@@ -398,13 +414,9 @@ DataFlow::SourceNode globalObjectRef() {
398414
*/
399415
pragma[nomagic]
400416
DataFlow::SourceNode globalVarRef(string name) {
401-
result.(GlobalVarRefNode).getName() = name
417+
result = globalVariable(name)
402418
or
403419
result = globalObjectRef().getAPropertyReference(name)
404-
or
405-
// `require("global/document")` or `require("global/window")`
406-
(name = "document" or name = "window") and
407-
result = moduleImport("global/" + name)
408420
}
409421

410422
/**

javascript/ql/test/library-tests/Nodes/globalObjectRef.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@
99
| tst.js:1:1:1:6 | window |
1010
| tst.js:3:1:3:6 | window |
1111
| tst.js:4:1:4:6 | window |
12-
| tst.js:4:1:4:13 | window.window |
1312
| tst.js:5:1:5:4 | self |
1413
| tst.js:6:1:6:10 | globalThis |

javascript/ql/test/library-tests/Nodes/globalVarRef.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
| String | tst2.js:9:1:9:11 | this.String |
33
| document | tst2.js:2:1:2:26 | require ... ument") |
44
| document | tst.js:3:1:3:15 | window.document |
5-
| document | tst.js:4:1:4:22 | window. ... ocument |
65
| document | tst.js:5:1:5:13 | self.document |
76
| document | tst.js:6:1:6:19 | globalThis.document |
87
| foo | tst3.js:4:1:4:5 | w.foo |

0 commit comments

Comments
 (0)