Skip to content

Commit c5a02da

Browse files
authored
Merge pull request #15768 from asgerf/js/amd-pseudo-deps
JS: Do not treat AMD pseudo-dependencies as imports
2 parents adefdfd + 8533973 commit c5a02da

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

javascript/ql/lib/semmle/javascript/AMD.qll

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ class AmdModuleDefinition extends CallExpr instanceof AmdModuleDefinition::Range
6161
}
6262

6363
/** Gets the `i`th dependency of this module definition. */
64-
PathExpr getDependency(int i) { result = this.getDependencies().getElement(i) }
64+
PathExpr getDependency(int i) {
65+
exists(Expr expr |
66+
expr = this.getDependencies().getElement(i) and
67+
not isPseudoDependency(expr.getStringValue()) and
68+
result = expr
69+
)
70+
}
6571

6672
/** Gets a dependency of this module definition. */
6773
PathExpr getADependency() {
@@ -102,9 +108,10 @@ class AmdModuleDefinition extends CallExpr instanceof AmdModuleDefinition::Range
102108
/**
103109
* Holds if `p` is the parameter corresponding to dependency `dep`.
104110
*/
105-
predicate dependencyParameter(PathExpr dep, Parameter p) {
111+
predicate dependencyParameter(Expr dep, Parameter p) {
106112
exists(int i |
107-
dep = this.getDependency(i) and
113+
// Note: to avoid spurious recursion, do not depend on PathExpr here
114+
dep = this.getDependencies().getElement(i) and
108115
p = this.getFactoryParameter(i)
109116
)
110117
}
@@ -122,9 +129,9 @@ class AmdModuleDefinition extends CallExpr instanceof AmdModuleDefinition::Range
122129
* `dep1` and `dep2`.
123130
*/
124131
Parameter getDependencyParameter(string name) {
125-
exists(PathExpr dep |
132+
exists(Expr dep |
126133
this.dependencyParameter(dep, result) and
127-
dep.getValue() = name
134+
name = dep.getStringValue()
128135
)
129136
}
130137

@@ -202,11 +209,15 @@ class AmdModuleDefinition extends CallExpr instanceof AmdModuleDefinition::Range
202209
}
203210
}
204211

212+
private predicate isPseudoDependency(string s) { s = ["exports", "require", "module"] }
213+
205214
/** An AMD dependency, considered as a path expression. */
206215
private class AmdDependencyPath extends PathExprCandidate {
207216
AmdDependencyPath() {
208217
exists(AmdModuleDefinition amd |
209-
this = amd.getDependencies().getAnElement() or
218+
this = amd.getDependencies().getAnElement() and
219+
not isPseudoDependency(this.getStringValue())
220+
or
210221
this = amd.getARequireCall().getAnArgument()
211222
)
212223
}

javascript/ql/test/library-tests/AMD/tests.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ amdModuleDefinition
6161
| umd.js:4:9:4:43 | define( ... actory) | umd.js:1:18:1:24 | factory |
6262
| umd.js:4:9:4:43 | define( ... actory) | umd.js:9:9:14:1 | functio ... };\\n} |
6363
amdModuleDependencies
64-
| tst2.js:1:1:3:2 | define( ... 42;\\n}) | tst2.js:1:9:1:17 | 'exports' |
6564
| tst3.js:1:1:3:2 | define( ... 42;\\n}) | tst3.js:2:21:2:25 | './a' |
6665
| tst4.js:1:1:11:2 | define( ... };\\n}) | tst4.js:2:9:2:14 | 'a.js' |
6766
| tst4.js:1:1:11:2 | define( ... };\\n}) | tst4.js:3:9:3:13 | 'foo' |

0 commit comments

Comments
 (0)