Skip to content

Commit 6d1eab8

Browse files
committed
JS: support flow out of "this" in constructor call
1 parent 370a9e4 commit 6d1eab8

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

javascript/ql/src/semmle/javascript/dataflow/Configuration.qll

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,16 @@ private predicate storeStep(
606606
basicStoreStep(pred, succ, prop) and
607607
summary = PathSummary::level()
608608
or
609-
exists(Function f, DataFlow::Node mid, DataFlow::Node base |
609+
exists(Function f, DataFlow::Node mid |
610610
// `f` stores its parameter `pred` in property `prop` of a value that it returns,
611611
// and `succ` is an invocation of `f`
612612
reachableFromInput(f, succ, pred, mid, cfg, summary) and
613-
returnedPropWrite(f, base, prop, mid)
613+
(
614+
returnedPropWrite(f, _, prop, mid)
615+
or
616+
succ instanceof DataFlow::NewNode and
617+
receiverPropWrite(f, prop, mid)
618+
)
614619
)
615620
}
616621

@@ -622,6 +627,13 @@ predicate returnedPropWrite(Function f, DataFlow::SourceNode base, string prop,
622627
base.flowsToExpr(f.getAReturnedExpr())
623628
}
624629

630+
/**
631+
* Holds if `f` may return `base`, which has a write of property `prop` with right-hand side `rhs`.
632+
*/
633+
predicate receiverPropWrite(Function f, string prop, DataFlow::Node rhs) {
634+
DataFlow::thisNode(f).hasPropertyWrite(prop, rhs)
635+
}
636+
625637
/**
626638
* Holds if `rhs` is the right-hand side of a write to property `prop`, and `nd` is reachable
627639
* from the base of that write under configuration `cfg` (possibly through callees) along a

javascript/ql/src/semmle/javascript/dataflow/DataFlow.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,13 @@ module DataFlow {
802802
*/
803803
predicate thisNode(DataFlow::Node node, StmtContainer container) { node = TThisNode(container) }
804804

805+
/**
806+
* Gets the node representing the receiver of the given function, or `this` in the given top-level.
807+
*
808+
* Has no result if `container` is an arrow function.
809+
*/
810+
DataFlow::ThisNode thisNode(StmtContainer container) { result = TThisNode(container) }
811+
805812
/**
806813
* A classification of flows that are not modeled, or only modeled incompletely, by
807814
* `DataFlowNode`:

javascript/ql/src/semmle/javascript/dataflow/internal/FlowSteps.qll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ predicate callStep(DataFlow::Node pred, DataFlow::Node succ) {
118118
* from a function call.
119119
*/
120120
predicate returnStep(DataFlow::Node pred, DataFlow::Node succ) {
121-
exists(Function f |
122-
returnExpr(f, pred, _) and
123-
calls(succ, f)
121+
exists(Function f | calls(succ, f) |
122+
returnExpr(f, pred, _)
123+
or
124+
succ instanceof DataFlow::NewNode and
125+
DataFlow::thisNode(pred, f)
124126
)
125127
}
126128

javascript/ql/test/library-tests/TaintTracking/BasicTaintTracking.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
| advanced-callgraph.js:2:13:2:20 | source() | advanced-callgraph.js:6:22:6:22 | v |
2+
| constructor-calls.js:4:18:4:25 | source() | constructor-calls.js:18:8:18:14 | c.taint |
3+
| constructor-calls.js:4:18:4:25 | source() | constructor-calls.js:22:8:22:19 | c_safe.taint |
4+
| constructor-calls.js:10:16:10:23 | source() | constructor-calls.js:26:8:26:14 | d.taint |
5+
| constructor-calls.js:10:16:10:23 | source() | constructor-calls.js:30:8:30:19 | d_safe.taint |
6+
| constructor-calls.js:14:15:14:22 | source() | constructor-calls.js:17:8:17:14 | c.param |
7+
| constructor-calls.js:14:15:14:22 | source() | constructor-calls.js:25:8:25:14 | d.param |
28
| partialCalls.js:4:17:4:24 | source() | partialCalls.js:17:14:17:14 | x |
39
| partialCalls.js:4:17:4:24 | source() | partialCalls.js:20:14:20:14 | y |
410
| partialCalls.js:4:17:4:24 | source() | partialCalls.js:30:14:30:20 | x.value |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class EcmaClass {
2+
constructor(param) {
3+
this.param = param;
4+
this.taint = source();
5+
}
6+
}
7+
8+
function JsClass(param) {
9+
this.param = param;
10+
this.taint = source();
11+
}
12+
13+
function test() {
14+
let taint = source();
15+
16+
let c = new EcmaClass(taint);
17+
sink(c.param); // NOT OK
18+
sink(c.taint); // NOT OK
19+
20+
let c_safe = new EcmaClass("safe");
21+
sink(c_safe.param); // OK
22+
sink(c_safe.taint); // NOT OK
23+
24+
let d = new JsClass(taint);
25+
sink(d.param); // NOT OK
26+
sink(d.taint); // NOT OK
27+
28+
let d_safe = new JsClass("safe");
29+
sink(d_safe.param); // OK
30+
sink(d_safe.taint); // NOT OK
31+
}

0 commit comments

Comments
 (0)