Skip to content

Commit 4060c68

Browse files
committed
JS: Convert boolean to a newtype
1 parent 331c2a9 commit 4060c68

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

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
@@ -1253,6 +1253,26 @@ module API {
12531253

12541254
private import semmle.javascript.dataflow.TypeTracking
12551255

1256+
private module Promisification {
1257+
private newtype TState =
1258+
/** Default statue; the tracked value has not been through any steps related to promisification. */
1259+
TNotPromisified() or
1260+
/** The tracked value is a function that has been through promisification. */
1261+
TPromisifiedFunction()
1262+
1263+
class State extends TState {
1264+
string toString() {
1265+
this = TNotPromisified() and result = "not-promisified"
1266+
or
1267+
this = TPromisifiedFunction() and result = "promisified-function"
1268+
}
1269+
}
1270+
1271+
State notPromisified() { result = TNotPromisified() }
1272+
1273+
State promisifiedFunction() { result = TPromisifiedFunction() }
1274+
}
1275+
12561276
/**
12571277
* Gets a data-flow node to which `nd`, which is a use of an API-graph node, flows.
12581278
*
@@ -1267,19 +1287,20 @@ module API {
12671287
* and not necessarily the entire object.
12681288
*/
12691289
private DataFlow::SourceNode trackUseNode(
1270-
DataFlow::SourceNode nd, boolean promisified, int boundArgs, string prop,
1290+
DataFlow::SourceNode nd, Promisification::State promisified, int boundArgs, string prop,
12711291
DataFlow::TypeTracker t
12721292
) {
12731293
t.start() and
12741294
use(_, nd) and
12751295
result = nd and
1276-
promisified = false and
1296+
promisified = Promisification::notPromisified() and
12771297
boundArgs = 0 and
12781298
prop = ""
12791299
or
12801300
exists(Promisify::PromisifyCall promisify |
1281-
trackUseNode(nd, false, boundArgs, prop, t.continue()).flowsTo(promisify.getArgument(0)) and
1282-
promisified = true and
1301+
trackUseNode(nd, Promisification::notPromisified(), boundArgs, prop, t.continue())
1302+
.flowsTo(promisify.getArgument(0)) and
1303+
promisified = Promisification::promisifiedFunction() and
12831304
prop = "" and
12841305
result = promisify
12851306
)
@@ -1298,7 +1319,7 @@ module API {
12981319
or
12991320
exists(DataFlow::Node pred, string preprop |
13001321
trackUseNode(nd, promisified, boundArgs, preprop, t.continue()).flowsTo(pred) and
1301-
promisified = false and
1322+
promisified = Promisification::notPromisified() and
13021323
boundArgs = 0 and
13031324
SharedTypeTrackingStep::loadStoreStep(pred, result, prop)
13041325
|
@@ -1319,7 +1340,8 @@ module API {
13191340
*/
13201341
pragma[noopt]
13211342
private DataFlow::TypeTracker useStep(
1322-
DataFlow::Node nd, boolean promisified, int boundArgs, string prop, DataFlow::Node res
1343+
DataFlow::Node nd, Promisification::State promisified, int boundArgs, string prop,
1344+
DataFlow::Node res
13231345
) {
13241346
exists(DataFlow::TypeTracker t, StepSummary summary, DataFlow::SourceNode prev |
13251347
prev = trackUseNode(nd, promisified, boundArgs, prop, t) and
@@ -1331,7 +1353,7 @@ module API {
13311353
}
13321354

13331355
private DataFlow::SourceNode trackUseNode(
1334-
DataFlow::SourceNode nd, boolean promisified, int boundArgs, string prop
1356+
DataFlow::SourceNode nd, Promisification::State promisified, int boundArgs, string prop
13351357
) {
13361358
result = trackUseNode(nd, promisified, boundArgs, prop, DataFlow::TypeTracker::end())
13371359
}
@@ -1341,7 +1363,7 @@ module API {
13411363
*/
13421364
cached
13431365
DataFlow::SourceNode trackUseNode(DataFlow::SourceNode nd) {
1344-
result = trackUseNode(nd, false, 0, "")
1366+
result = trackUseNode(nd, Promisification::notPromisified(), 0, "")
13451367
}
13461368

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

0 commit comments

Comments
 (0)