Skip to content

Commit b015c73

Browse files
authored
Merge pull request github#3809 from max-schaefer/util-deprecate
Approved by asgerf
2 parents 1b4df57 + 640c194 commit b015c73

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javascript
66
import semmle.javascript.frameworks.HTTP
77
import semmle.javascript.security.SensitiveActions
8+
private import semmle.javascript.dataflow.internal.PreCallGraphStep
89

910
module NodeJSLib {
1011
private GlobalVariable processVariable() { variables(result, "process", any(GlobalScope sc)) }
@@ -610,6 +611,22 @@ module NodeJSLib {
610611
)
611612
}
612613

614+
/**
615+
* A call to `util.deprecate`, considered to introduce data flow from its first argument
616+
* to its result.
617+
*/
618+
private class UtilDeprecateStep extends PreCallGraphStep {
619+
override predicate step(DataFlow::Node pred, DataFlow::Node succ) {
620+
exists(DataFlow::CallNode deprecate |
621+
deprecate = DataFlow::moduleMember("util", "deprecate").getACall() or
622+
deprecate = DataFlow::moduleImport("util-deprecate").getACall()
623+
|
624+
pred = deprecate.getArgument(0) and
625+
succ = deprecate
626+
)
627+
}
628+
}
629+
613630
/**
614631
* A call to a method from module `child_process`.
615632
*/

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

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import javascript
2+
3+
/** Gets a node to which the source node annotated with `name` is tracked under state `t`. */
4+
DataFlow::SourceNode trackNamedNode(DataFlow::TypeTracker t, string name) {
5+
t.start() and
6+
exists(Comment c, string f, int l |
7+
f = c.getFile().getAbsolutePath() and
8+
l = c.getLocation().getStartLine() and
9+
result.hasLocationInfo(f, l, _, _, _) and
10+
name = c.getText().regexpFind("(?<=name: )\\S+", _, _)
11+
)
12+
or
13+
exists(DataFlow::TypeTracker t2 | result = trackNamedNode(t2, name).track(t2, t))
14+
}
15+
16+
/** Holds if `name` is tracked to expression `e` starting on line `l` of file `f`. */
17+
predicate actual(Expr e, File f, int l, string name) {
18+
trackNamedNode(DataFlow::TypeTracker::end(), name).flowsToExpr(e) and
19+
f = e.getFile() and
20+
l = e.getLocation().getStartLine()
21+
}
22+
23+
/**
24+
* Holds if there is an annotation comment expecting `name` to be tracked to an expression
25+
* on line `l` of file `f`.
26+
*/
27+
predicate expected(Comment c, File f, int l, string name) {
28+
f = c.getFile() and
29+
l = c.getLocation().getStartLine() and
30+
name = c.getText().regexpFind("(?<=track: )\\S+", _, _)
31+
}
32+
33+
from Locatable loc, File f, int l, string name, string msg
34+
where
35+
expected(loc, f, l, name) and
36+
not actual(_, f, l, name) and
37+
msg = "Failed to track " + name + " here."
38+
or
39+
actual(loc, f, l, name) and
40+
not expected(_, f, l, name) and
41+
expected(_, f, l, _) and
42+
msg = "Unexpectedly tracked " + name + " here."
43+
select loc, msg
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const g = require("./deprecated");
2+
3+
g(); // track: f
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const util = require("util");
2+
3+
function f() {} // name: f
4+
5+
module.exports = util.deprecate(f, "don't use this function");

0 commit comments

Comments
 (0)