Skip to content

Commit bac3951

Browse files
committed
JS: Convert boolean to a newtype
1 parent 4941ed2 commit bac3951

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

javascript/ql/lib/semmle/javascript/ApiGraphs.qll

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ module API {
10431043
// property reads
10441044
exists(DataFlow::SourceNode src, DataFlow::SourceNode pred, string propDesc |
10451045
use(base, src) and
1046-
pred = trackUseNode(src, false, 0, propDesc) and
1046+
pred = trackUseNode(src, Promisification::notPromisified(), 0, propDesc) and
10471047
propertyRead(pred, propDesc, lbl, ref) and
10481048
// `module.exports` is special: it is a use of a def-node, not a use-node,
10491049
// so we want to exclude it here
@@ -1242,6 +1242,26 @@ module API {
12421242

12431243
private import semmle.javascript.dataflow.TypeTracking
12441244

1245+
private module Promisification {
1246+
private newtype TState =
1247+
/** Default statue; the tracked value has not been through any steps related to promisification. */
1248+
TNotPromisified() or
1249+
/** The tracked value is a function that has been through promisification. */
1250+
TPromisifiedFunction()
1251+
1252+
class State extends TState {
1253+
string toString() {
1254+
this = TNotPromisified() and result = "not-promisified"
1255+
or
1256+
this = TPromisifiedFunction() and result = "promisified-function"
1257+
}
1258+
}
1259+
1260+
State notPromisified() { result = TNotPromisified() }
1261+
1262+
State promisifiedFunction() { result = TPromisifiedFunction() }
1263+
}
1264+
12451265
/**
12461266
* Gets a data-flow node to which `nd`, which is a use of an API-graph node, flows.
12471267
*
@@ -1256,19 +1276,20 @@ module API {
12561276
* and not necessarily the entire object.
12571277
*/
12581278
private DataFlow::SourceNode trackUseNode(
1259-
DataFlow::SourceNode nd, boolean promisified, int boundArgs, string prop,
1279+
DataFlow::SourceNode nd, Promisification::State promisified, int boundArgs, string prop,
12601280
DataFlow::TypeTracker t
12611281
) {
12621282
t.start() and
12631283
use(_, nd) and
12641284
result = nd and
1265-
promisified = false and
1285+
promisified = Promisification::notPromisified() and
12661286
boundArgs = 0 and
12671287
prop = ""
12681288
or
12691289
exists(Promisify::PromisifyCall promisify |
1270-
trackUseNode(nd, false, boundArgs, prop, t.continue()).flowsTo(promisify.getArgument(0)) and
1271-
promisified = true and
1290+
trackUseNode(nd, Promisification::notPromisified(), boundArgs, prop, t.continue())
1291+
.flowsTo(promisify.getArgument(0)) and
1292+
promisified = Promisification::promisifiedFunction() and
12721293
prop = "" and
12731294
result = promisify
12741295
)
@@ -1287,7 +1308,7 @@ module API {
12871308
or
12881309
exists(DataFlow::Node pred, string preprop |
12891310
trackUseNode(nd, promisified, boundArgs, preprop, t.continue()).flowsTo(pred) and
1290-
promisified = false and
1311+
promisified = Promisification::notPromisified() and
12911312
boundArgs = 0 and
12921313
SharedTypeTrackingStep::loadStoreStep(pred, result, prop)
12931314
|
@@ -1308,7 +1329,8 @@ module API {
13081329
*/
13091330
pragma[noopt]
13101331
private DataFlow::TypeTracker useStep(
1311-
DataFlow::Node nd, boolean promisified, int boundArgs, string prop, DataFlow::Node res
1332+
DataFlow::Node nd, Promisification::State promisified, int boundArgs, string prop,
1333+
DataFlow::Node res
13121334
) {
13131335
exists(DataFlow::TypeTracker t, StepSummary summary, DataFlow::SourceNode prev |
13141336
prev = trackUseNode(nd, promisified, boundArgs, prop, t) and
@@ -1318,7 +1340,7 @@ module API {
13181340
}
13191341

13201342
private DataFlow::SourceNode trackUseNode(
1321-
DataFlow::SourceNode nd, boolean promisified, int boundArgs, string prop
1343+
DataFlow::SourceNode nd, Promisification::State promisified, int boundArgs, string prop
13221344
) {
13231345
result = trackUseNode(nd, promisified, boundArgs, prop, DataFlow::TypeTracker::end())
13241346
}
@@ -1328,7 +1350,7 @@ module API {
13281350
*/
13291351
cached
13301352
DataFlow::SourceNode trackUseNode(DataFlow::SourceNode nd) {
1331-
result = trackUseNode(nd, false, 0, "")
1353+
result = trackUseNode(nd, Promisification::notPromisified(), 0, "")
13321354
}
13331355

13341356
private DataFlow::SourceNode trackDefNode(DataFlow::Node nd, DataFlow::TypeBackTracker t) {

0 commit comments

Comments
 (0)