Skip to content

Commit 2fac743

Browse files
committed
JS: infer this to be module.exports in node modules
1 parent ae8d382 commit 2fac743

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

javascript/ql/src/semmle/javascript/dataflow/internal/InterProceduralTypeInference.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ private class AnalyzedThisInBoundFunction extends AnalyzedThisExpr {
4545
}
4646
}
4747

48+
/**
49+
* Flow analysis for `this` expressions in node modules.
50+
*
51+
* These expressions are assumed to refer to the `module.exports` object.
52+
*/
53+
private class AnalyzedThisAsModuleExports extends DataFlow::AnalyzedNode, DataFlow::ThisNode {
54+
NodeModule m;
55+
56+
AnalyzedThisAsModuleExports() { m = getBindingContainer() }
57+
58+
override AbstractValue getALocalValue() { result = TAbstractExportsObject(m) }
59+
}
60+
4861
/**
4962
* Flow analysis for `this` expressions inside a function that is instantiated.
5063
*

javascript/ql/test/library-tests/ThisExpr/ThisExpr_analyzed.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
| module-exports.js:3:1:3:4 | this | file://:0:0:0:0 | indefinite value (call) |
2+
| module-exports.js:3:1:3:4 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
23
| module-exports.js:4:28:4:31 | this | file://:0:0:0:0 | indefinite value (call) |
34
| module-exports.js:4:28:4:31 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
45
| module-exports.js:4:28:4:31 | this | module-exports.js:4:15:4:34 | instance of anonymous function |
@@ -7,7 +8,9 @@
78
| module-exports.js:5:35:5:38 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
89
| module-exports.js:5:35:5:38 | this | module-exports.js:5:22:5:41 | instance of anonymous function |
910
| module-exports.js:6:21:6:24 | this | file://:0:0:0:0 | indefinite value (call) |
11+
| module-exports.js:6:21:6:24 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
1012
| module-exports.js:7:28:7:31 | this | file://:0:0:0:0 | indefinite value (call) |
13+
| module-exports.js:7:28:7:31 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
1114
| tst.js:4:9:4:12 | this | file://:0:0:0:0 | indefinite value (call) |
1215
| tst.js:4:9:4:12 | this | tst.js:1:1:11:1 | instance of class C_normal |
1316
| tst.js:8:15:8:18 | this | file://:0:0:0:0 | indefinite value (call) |

0 commit comments

Comments
 (0)