Skip to content

Commit 3804d3f

Browse files
committed
JS: Remove Import->SourceNode dependency from lazy cache
1 parent 8f930fc commit 3804d3f

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

javascript/ql/src/semmle/javascript/frameworks/LazyCache.qll

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import javascript
66

77
module LazyCache {
88
/**
9+
* DEPRECATED. DO NOT USE.
10+
*
911
* A lazy-cache object, usually created through an expression of form `require('lazy-cache')(require)`.
1012
*/
13+
deprecated
1114
class LazyCacheObject extends DataFlow::SourceNode {
1215
LazyCacheObject() {
1316
// Use `require` directly instead of `moduleImport` to avoid recursion.
@@ -19,13 +22,26 @@ module LazyCache {
1922
}
2023
}
2124

25+
/**
26+
* A variable containing a lazy-cache object.
27+
*/
28+
class LazyCacheVariable extends LocalVariable {
29+
LazyCacheVariable() {
30+
// To avoid recursion, this should not depend on `SourceNode`.
31+
exists(Require req |
32+
req.getArgument(0).getStringValue() = "lazy-cache" and
33+
getAnAssignedExpr().(CallExpr).getCallee() = req
34+
)
35+
}
36+
}
37+
2238
/**
2339
* An import through `lazy-cache`.
2440
*/
2541
class LazyCacheImport extends CallExpr, Import {
26-
LazyCacheObject cache;
42+
LazyCacheVariable cache;
2743

28-
LazyCacheImport() { this = cache.getACall().asExpr() }
44+
LazyCacheImport() { getCallee() = cache.getAnAccess() }
2945

3046
/** Gets the name of the package as it's exposed on the lazy-cache object. */
3147
string getLocalAlias() {
@@ -39,10 +55,23 @@ module LazyCache {
3955

4056
override PathExpr getImportedPath() { result = getArgument(0) }
4157

58+
private LazyCacheVariable getVariable() {
59+
result = cache
60+
}
61+
62+
pragma[noopt]
4263
override DataFlow::Node getImportedModuleNode() {
64+
this instanceof LazyCacheImport and
4365
result = this.flow()
4466
or
45-
result = cache.getAPropertyRead(getLocalAlias())
67+
exists(LazyCacheVariable variable, Expr base, PropAccess access, string localName |
68+
variable = getVariable() and
69+
base = variable.getAnAccess() and
70+
access.getBase() = base and
71+
localName = getLocalAlias() and
72+
access.getPropertyName() = localName and
73+
result = access.flow()
74+
)
4675
}
4776
}
4877

0 commit comments

Comments
 (0)