Skip to content

Commit 418f841

Browse files
committed
JS: Handle imports through lazy-cache
1 parent 180e9d4 commit 418f841

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

javascript/ql/src/javascript.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import semmle.javascript.frameworks.Files
7878
import semmle.javascript.frameworks.Firebase
7979
import semmle.javascript.frameworks.jQuery
8080
import semmle.javascript.frameworks.Handlebars
81+
import semmle.javascript.frameworks.LazyCache
8182
import semmle.javascript.frameworks.LodashUnderscore
8283
import semmle.javascript.frameworks.Logging
8384
import semmle.javascript.frameworks.HttpFrameworks
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Models imports through the NPM `lazy-cache` package.
3+
*/
4+
5+
import javascript
6+
7+
module LazyCache {
8+
/**
9+
* A lazy-cache object, usually created through an expression of form `require('lazy-cache')(require)`.
10+
*/
11+
class LazyCacheObject extends DataFlow::SourceNode {
12+
LazyCacheObject() {
13+
// Use `require` directly instead of `moduleImport` to avoid recursion.
14+
// For the same reason, avoid `Import.getImportedPath`.
15+
exists(Require req |
16+
req.getArgument(0).getStringValue() = "lazy-cache" and
17+
this = req.flow().(DataFlow::SourceNode).getAnInvocation()
18+
)
19+
}
20+
}
21+
22+
/**
23+
* An import through `lazy-cache`.
24+
*/
25+
class LazyCacheImport extends CallExpr, Import {
26+
LazyCacheObject cache;
27+
28+
LazyCacheImport() { this = cache.getACall().asExpr() }
29+
30+
/** Gets the name of the package as it's exposed on the lazy-cache object. */
31+
string getLocalAlias() {
32+
result = getArgument(1).getStringValue()
33+
or
34+
not exists(getArgument(1)) and
35+
result = getArgument(0).getStringValue()
36+
}
37+
38+
override Module getEnclosingModule() { result = getTopLevel() }
39+
40+
override PathExpr getImportedPath() { result = getArgument(0) }
41+
42+
override DataFlow::Node getImportedModuleNode() {
43+
result = this.flow()
44+
or
45+
result = cache.getAPropertyRead(getLocalAlias())
46+
}
47+
}
48+
49+
/** A constant path element appearing in a call to a lazy-cache object. */
50+
private class LazyCachePathExpr extends PathExprInModule, ConstantString {
51+
LazyCachePathExpr() { this = any(LazyCacheImport rp).getArgument(0) }
52+
53+
override string getValue() { result = getStringValue() }
54+
}
55+
}

0 commit comments

Comments
 (0)