Skip to content

Commit 22640f8

Browse files
author
Max Schaefer
committed
JavaScript: Make lodash/underscore recognition extensible.
1 parent 1da873e commit 22640f8

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,41 @@ import javascript
55

66
/** Provides a unified model of [lodash](https://lodash.com/) and [underscore](http://underscorejs.org/). */
77
module LodashUnderscore {
8+
/**
9+
* A data flow node that accesses a given member of `lodash` or `underscore`.
10+
*/
11+
abstract class Member extends DataFlow::SourceNode {
12+
/** Gets the name of the accessed member. */
13+
abstract string getName();
14+
}
15+
16+
/**
17+
* An import of `lodash` or `underscore` accessing a given member of that package.
18+
*/
19+
private class DefaultMember extends Member {
20+
string name;
21+
22+
DefaultMember() {
23+
this = DataFlow::moduleMember("underscore", name) or
24+
this = DataFlow::moduleMember("lodash", name) or
25+
this = DataFlow::moduleImport("lodash/" + name) or
26+
this = DataFlow::moduleImport("lodash." + name) or
27+
this = DataFlow::globalVarRef("_").getAPropertyRead(name)
28+
}
29+
30+
override string getName() {
31+
result = name
32+
}
33+
}
34+
835
/**
936
* Gets a data flow node that accesses the given member of `lodash` or `underscore`.
1037
*
1138
* In addition to normal imports, this supports per-method imports such as `require("lodash.map")` and `require("lodash/map")`.
1239
* In addition, the global variable `_` is assumed to refer to `lodash` or `underscore`.
1340
*/
1441
DataFlow::SourceNode member(string name) {
15-
result = DataFlow::moduleMember("underscore", name) or
16-
result = DataFlow::moduleMember("lodash", name) or
17-
result = DataFlow::moduleImport("lodash/" + name) or
18-
result = DataFlow::moduleImport("lodash." + name) or
19-
result = DataFlow::globalVarRef("_").getAPropertyRead(name)
42+
result.(Member).getName() = name
2043
}
2144
}
2245

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Provides classes that heuristically identify uses of common frameworks.
3+
*
4+
* Note: This module should not be a permanent part of the standard library imports.
5+
*/
6+
7+
import javascript
8+
9+
/**
10+
* An import of a module whose name ends in `-lodash` or `-underscore`, interpreted
11+
* as a likely import of the lodash or underscore library.
12+
*/
13+
private class ImpreciseLodashMember extends LodashUnderscore::Member {
14+
string name;
15+
16+
ImpreciseLodashMember() {
17+
exists (string lodash |
18+
this = DataFlow::moduleMember(lodash, name) |
19+
lodash.matches("%-lodash") or lodash.matches("%-underscore")
20+
)
21+
}
22+
23+
override string getName() { result = name }
24+
}

javascript/ql/src/semmle/javascript/heuristics/all.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Note: This module should not be a permanent part of the standard library imports.
77
*/
88

9+
import AdditionalFrameworks
910
import AdditionalPromises
1011
import AdditionalRouteHandlers
1112
import AdditionalSources

0 commit comments

Comments
 (0)