Skip to content

Commit b3e8103

Browse files
author
Max Schaefer
committed
JavaScript: Track flow through property getter functions.
1 parent 1c175cb commit b3e8103

File tree

6 files changed

+112
-5
lines changed

6 files changed

+112
-5
lines changed

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,11 +697,60 @@ private predicate storeStep(
697697
)
698698
}
699699

700+
/**
701+
* Holds if `f` may `read` property `prop` of parameter `parm`.
702+
*/
703+
private predicate parameterPropRead(
704+
Function f, DataFlow::Node invk, DataFlow::Node arg, string prop, DataFlow::PropRead read,
705+
DataFlow::Configuration cfg
706+
) {
707+
exists(DataFlow::SourceNode parm |
708+
callInputStep(f, invk, arg, parm, cfg) and
709+
read = parm.getAPropertyRead(prop)
710+
)
711+
}
712+
713+
/**
714+
* Holds if `nd` may flow into a return statement of `f` under configuration `cfg`
715+
* (possibly through callees) along a path summarized by `summary`.
716+
*/
717+
private predicate reachesReturn(
718+
Function f, DataFlow::Node nd, DataFlow::Configuration cfg, PathSummary summary
719+
) {
720+
isRelevant(nd, cfg) and
721+
returnExpr(f, nd, _) and
722+
summary = PathSummary::level()
723+
or
724+
exists(DataFlow::Node mid, PathSummary oldSummary, PathSummary newSummary |
725+
flowStep(nd, cfg, mid, oldSummary) and
726+
reachesReturn(f, mid, cfg, newSummary) and
727+
summary = oldSummary.append(newSummary)
728+
)
729+
}
730+
731+
/**
732+
* Holds if property `prop` of `pred` may flow into `succ` along a path summarized by
733+
* `summary`.
734+
*/
735+
private predicate loadStep(
736+
DataFlow::Node pred, DataFlow::Node succ, string prop, DataFlow::Configuration cfg,
737+
PathSummary summary
738+
) {
739+
basicLoadStep(pred, succ, prop) and
740+
summary = PathSummary::level()
741+
or
742+
exists(Function f, DataFlow::PropRead read |
743+
parameterPropRead(f, succ, pred, prop, read, cfg) and
744+
reachesReturn(f, read, cfg, summary)
745+
)
746+
}
747+
700748
/**
701749
* Holds if `rhs` is the right-hand side of a write to property `prop`, and `nd` is reachable
702750
* from the base of that write under configuration `cfg` (possibly through callees) along a
703751
* path summarized by `summary`.
704752
*/
753+
pragma[nomagic]
705754
private predicate reachableFromStoreBase(
706755
string prop, DataFlow::Node rhs, DataFlow::Node nd, DataFlow::Configuration cfg,
707756
PathSummary summary
@@ -723,11 +772,14 @@ private predicate reachableFromStoreBase(
723772
*
724773
* In other words, `pred` may flow to `succ` through a property.
725774
*/
775+
pragma[noinline]
726776
private predicate flowThroughProperty(
727777
DataFlow::Node pred, DataFlow::Node succ, DataFlow::Configuration cfg, PathSummary summary
728778
) {
729-
exists(string prop, DataFlow::Node base | reachableFromStoreBase(prop, pred, base, cfg, summary) |
730-
basicLoadStep(base, succ, prop)
779+
exists(string prop, DataFlow::Node base, PathSummary oldSummary, PathSummary newSummary |
780+
reachableFromStoreBase(prop, pred, base, cfg, oldSummary) and
781+
loadStep(base, succ, prop, cfg, newSummary) and
782+
summary = oldSummary.append(newSummary)
731783
)
732784
}
733785

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ private module NodeTracking {
150150
)
151151
}
152152

153+
/**
154+
* Holds if `nd` may flow into a return statement of `f`
155+
* (possibly through callees) along a path summarized by `summary`.
156+
*/
157+
private predicate reachesReturn(Function f, DataFlow::Node nd, PathSummary summary) {
158+
returnExpr(f, nd, _) and
159+
summary = PathSummary::level()
160+
or
161+
exists (DataFlow::Node mid, PathSummary oldSummary, PathSummary newSummary |
162+
flowStep(nd, mid, oldSummary) and
163+
reachesReturn(f, mid, newSummary) and
164+
summary = oldSummary.append(newSummary)
165+
)
166+
}
167+
153168
/**
154169
* Holds if a function invoked at `invk` may return an expression into which `input`,
155170
* which is either an argument or a definition captured by the function, flows,
@@ -191,6 +206,21 @@ private module NodeTracking {
191206
)
192207
}
193208

209+
/**
210+
* Holds if property `prop` of `pred` may flow into `succ` along a path summarized by
211+
* `summary`.
212+
*/
213+
private predicate loadStep(DataFlow::Node pred, DataFlow::Node succ, string prop,
214+
PathSummary summary) {
215+
basicLoadStep(pred, succ, prop) and
216+
summary = PathSummary::level()
217+
or
218+
exists (Function f, DataFlow::ParameterNode parm |
219+
argumentPassing(succ, pred, f, parm) and
220+
reachesReturn(f, parm.getAPropertyRead(prop), summary)
221+
)
222+
}
223+
194224
/**
195225
* Holds if `rhs` is the right-hand side of a write to property `prop`, and `nd` is reachable
196226
* from the base of that write (possibly through callees) along a path summarized by `summary`.
@@ -216,9 +246,10 @@ private module NodeTracking {
216246
private predicate flowThroughProperty(
217247
DataFlow::Node pred, DataFlow::Node succ, PathSummary summary
218248
) {
219-
exists(string prop, DataFlow::Node base |
220-
reachableFromStoreBase(prop, pred, base, summary) and
221-
basicLoadStep(base, succ, prop)
249+
exists (string prop, DataFlow::Node base, PathSummary oldSummary, PathSummary newSummary |
250+
reachableFromStoreBase(prop, pred, base, oldSummary) and
251+
loadStep(base, succ, prop, newSummary) and
252+
summary = oldSummary.append(newSummary)
222253
)
223254
}
224255

javascript/ql/test/library-tests/InterProceduralFlow/DataFlow.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
| promises.js:12:22:12:31 | "rejected" | promises.js:24:20:24:20 | v |
3030
| promises.js:12:22:12:31 | "rejected" | promises.js:27:16:27:16 | v |
3131
| properties2.js:7:14:7:21 | "source" | properties2.js:8:12:8:24 | foo(source).p |
32+
| properties2.js:7:14:7:21 | "source" | properties2.js:33:13:33:20 | getP(o3) |
3233
| properties.js:2:16:2:24 | "tainted" | properties.js:5:14:5:23 | a.someProp |
3334
| properties.js:2:16:2:24 | "tainted" | properties.js:12:15:12:24 | x.someProp |
3435
| properties.js:2:16:2:24 | "tainted" | properties.js:14:15:14:27 | tmp1.someProp |

javascript/ql/test/library-tests/InterProceduralFlow/GermanFlow.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
| promises.js:12:22:12:31 | "rejected" | promises.js:24:20:24:20 | v |
3131
| promises.js:12:22:12:31 | "rejected" | promises.js:27:16:27:16 | v |
3232
| properties2.js:7:14:7:21 | "source" | properties2.js:8:12:8:24 | foo(source).p |
33+
| properties2.js:7:14:7:21 | "source" | properties2.js:33:13:33:20 | getP(o3) |
3334
| properties.js:2:16:2:24 | "tainted" | properties.js:5:14:5:23 | a.someProp |
3435
| properties.js:2:16:2:24 | "tainted" | properties.js:12:15:12:24 | x.someProp |
3536
| properties.js:2:16:2:24 | "tainted" | properties.js:14:15:14:27 | tmp1.someProp |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
| promises.js:12:22:12:31 | "rejected" | promises.js:27:16:27:16 | v |
3737
| promises.js:32:24:32:37 | "also tainted" | promises.js:38:32:38:32 | v |
3838
| properties2.js:7:14:7:21 | "source" | properties2.js:8:12:8:24 | foo(source).p |
39+
| properties2.js:7:14:7:21 | "source" | properties2.js:33:13:33:20 | getP(o3) |
3940
| properties.js:2:16:2:24 | "tainted" | properties.js:5:14:5:23 | a.someProp |
4041
| properties.js:2:16:2:24 | "tainted" | properties.js:12:15:12:24 | x.someProp |
4142
| properties.js:2:16:2:24 | "tainted" | properties.js:14:15:14:27 | tmp1.someProp |

javascript/ql/test/library-tests/InterProceduralFlow/properties2.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,25 @@ var o2 = {};
2121
setP(o2, "not a source");
2222
var sink5 = o2.p;
2323

24+
function getP(base) {
25+
return base.p;
26+
}
27+
28+
function getQ(base) {
29+
return base.q;
30+
}
31+
32+
var o3 = { p: source };
33+
var sink6 = getP(o3);
34+
var sink7 = getQ(o3);
35+
36+
var o4 = {};
37+
setP(o4, source);
38+
var sink8 = getP(o4);
39+
var sink9 = getQ(o4);
40+
41+
var o5 = {};
42+
setP(o5, "not a source");
43+
var sink10 = getP(o5);
44+
2445
// semmle-extractor-options: --source-type module

0 commit comments

Comments
 (0)