forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromises.qll
More file actions
757 lines (693 loc) · 25.7 KB
/
Promises.qll
File metadata and controls
757 lines (693 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/**
* Provides classes for modeling promises and their data-flow.
*/
import javascript
private import dataflow.internal.StepSummary
private import semmle.javascript.dataflow.internal.FlowSteps
/**
* A call to the `Promise` constructor, such as `new Promise((resolve, reject) => { ... })`.
*
* This includes calls to the built-in `Promise` constructor as well as promise implementations from known libraries, such as `bluebird`.
*/
abstract class PromiseDefinition extends DataFlow::SourceNode {
/** Gets the executor function of this promise object. */
abstract DataFlow::FunctionNode getExecutor();
/** Gets the `resolve` parameter of the executor function. */
DataFlow::ParameterNode getResolveParameter() { result = this.getExecutor().getParameter(0) }
/** Gets the `reject` parameter of the executor function. */
DataFlow::ParameterNode getRejectParameter() { result = this.getExecutor().getParameter(1) }
/** Gets the `i`th callback handler installed by method `m`. */
private DataFlow::FunctionNode getAHandler(string m, int i) {
result = this.getAMethodCall(m).getCallback(i)
}
/**
* Gets a function that handles promise resolution, including both
* `then` handlers and `finally` handlers.
*/
DataFlow::FunctionNode getAResolveHandler() {
result = this.getAHandler("then", 0) or
result = this.getAFinallyHandler()
}
/**
* Gets a function that handles promise rejection, including
* `then` handlers, `catch` handlers and `finally` handlers.
*/
DataFlow::FunctionNode getARejectHandler() {
result = this.getAHandler("then", 1) or
result = this.getACatchHandler() or
result = this.getAFinallyHandler()
}
/**
* Gets a `catch` handler of this promise.
*/
DataFlow::FunctionNode getACatchHandler() { result = this.getAHandler("catch", 0) }
/**
* Gets a `finally` handler of this promise.
*/
DataFlow::FunctionNode getAFinallyHandler() { result = this.getAHandler("finally", 0) }
}
/** Holds if the `i`th callback handler is installed by method `m`. */
private predicate hasHandler(DataFlow::InvokeNode promise, string m, int i) {
exists(promise.getAMethodCall(m).getCallback(i))
}
/**
* Gets a reference to the `Promise` object.
* Either from the standard library, a polyfill import, or a polyfill that defines the global `Promise` variable.
*/
private DataFlow::SourceNode getAPromiseObject() {
// Standard library, or polyfills like [es6-shim](https://npmjs.org/package/es6-shim).
result = DataFlow::globalVarRef("Promise")
or
// polyfills from the [`promise`](https://npmjs.org/package/promise) library.
result =
DataFlow::moduleImport([
"promise", "promise/domains", "promise/setimmediate", "promise/lib/es6-extensions",
"promise/domains/es6-extensions", "promise/setimmediate/es6-extensions"
])
or
// polyfill from the [`promise-polyfill`](https://npmjs.org/package/promise-polyfill) library.
result = DataFlow::moduleMember(["promise-polyfill", "promise-polyfill/src/polyfill"], "default")
or
result = DataFlow::moduleImport(["promise-polyfill", "promise-polyfill/src/polyfill"])
or
result = DataFlow::moduleMember(["es6-promise", "rsvp"], "Promise")
or
result = DataFlow::moduleImport("native-promise-only")
or
result = DataFlow::moduleImport("when")
or
result = DataFlow::moduleImport("pinkie-promise")
or
result = DataFlow::moduleImport("pinkie")
or
result = DataFlow::moduleMember("synchronous-promise", "SynchronousPromise")
or
result = DataFlow::moduleImport("any-promise")
or
result = DataFlow::moduleImport("lie")
}
/**
* A call that looks like a Promise.
*
* For example, this could be the call `promise(f).then(function(v){...})`
*/
class PromiseCandidate extends DataFlow::InvokeNode {
PromiseCandidate() {
hasHandler(this, "then", [0 .. 1]) or
hasHandler(this, "catch", 0) or
hasHandler(this, "finally", 0)
}
}
/**
* A promise object created by the standard ECMAScript 2015 `Promise` constructor,
* or a polyfill implementing a superset of the ECMAScript 2015 `Promise` API.
*/
private class ES2015PromiseDefinition extends PromiseDefinition, DataFlow::InvokeNode {
ES2015PromiseDefinition() { this = getAPromiseObject().getAnInvocation() }
override DataFlow::FunctionNode getExecutor() { result = this.getCallback(0) }
}
/**
* A promise that is created and resolved with one or more value.
*/
abstract class PromiseCreationCall extends DataFlow::CallNode {
/**
* Gets a value this promise is resolved with.
*/
abstract DataFlow::Node getValue();
}
/**
* A promise that is created using a `.resolve()` call.
*/
abstract class ResolvedPromiseDefinition extends PromiseCreationCall { }
/**
* A promise that is created using a `Promise.all(array)` call.
*/
abstract class PromiseAllCreation extends PromiseCreationCall {
/**
* Gets a node for the array of values given to the `Promise.all(array)` call.
*/
abstract DataFlow::Node getArrayNode();
}
/**
* A resolved promise created by the standard ECMAScript 2015 `Promise.resolve` function.
*/
class ResolvedES2015PromiseDefinition extends ResolvedPromiseDefinition {
ResolvedES2015PromiseDefinition() { this = getAPromiseObject().getAMemberCall("resolve") }
override DataFlow::Node getValue() { result = this.getArgument(0) }
}
/**
* An aggregated promise produced either by `Promise.all`, `Promise.race`, or `Promise.any`.
*/
class AggregateES2015PromiseDefinition extends PromiseCreationCall {
AggregateES2015PromiseDefinition() {
this = getAPromiseObject().getAMemberCall(["all", "race", "any", "allSettled"])
or
this = DataFlow::moduleImport("promise.allsettled").getACall()
}
override DataFlow::Node getValue() {
result = this.getArgument(0).getALocalSource().(DataFlow::ArrayCreationNode).getAnElement()
}
}
/**
* An aggregated promise created using `Promise.all()`.
*/
class ES2015PromiseAllDefinition extends AggregateES2015PromiseDefinition, PromiseAllCreation {
ES2015PromiseAllDefinition() { this.getCalleeName() = "all" }
override DataFlow::Node getArrayNode() { result = this.getArgument(0) }
}
/**
* Common predicates shared between type-tracking and data-flow for promises.
*/
module Promises {
/**
* Gets the pseudo-field used to describe resolved values in a promise.
*/
string valueProp() { result = "$PromiseResolveField$" }
/**
* Gets the pseudo-field used to describe rejected values in a promise.
*/
string errorProp() { result = "$PromiseRejectField$" }
/** A property set containing the pseudo-properites of a promise object. */
class PromiseProps extends DataFlow::PropertySet {
PromiseProps() { this = "PromiseProps" }
override string getAProperty() { result = [valueProp(), errorProp()] }
}
predicate promiseConstructorRef = getAPromiseObject/0;
}
/**
* A module for supporting promises in type-tracking predicates.
* The `PromiseTypeTracking::promiseStep` predicate is used for type tracking in and out of promises,
* and is included in the standard type-tracking steps (`SourceNode::track`).
* The `TypeTracker::startInPromise()` predicate can be used to initiate a type-tracker
* where the tracked value is a promise.
*
* The below is an example of a type-tracking predicate where the initial value is a promise:
* ```
* DataFlow::SourceNode myType(DataFlow::TypeTracker t) {
* t.startInPromise() and
* result = <the promise value> and
* or
* exists(DataFlow::TypeTracker t2 | result = myType(t2).track(t2, t))
* }
* ```
*
* The type-tracking predicate above will only end (`t = DataFlow::TypeTracker::end()`) after the tracked value has been
* extracted from the promise.
*
* The `PromiseTypeTracking::promiseStep` predicate can be used instead of `SourceNode::track`
* to get type-tracking only for promise steps.
*
* Replace `t.startInPromise()` in the above example with `t.start()` to create a type-tracking predicate
* where the value is not initially inside a promise.
*/
module PromiseTypeTracking {
/**
* Gets the result from a single step through a promise, from `pred` to `result` summarized by `summary`.
* This can be loading a resolved value from a promise, storing a value in a promise, or copying a resolved value from one promise to another.
*
* These type-tracking steps are already included in the default type-tracking steps (through `PreCallGraphStep`).
*/
pragma[inline]
DataFlow::Node promiseStep(DataFlow::Node pred, StepSummary summary) {
exists(string field | field = Promises::valueProp() |
summary = LoadStep(field) and
PromiseFlow::loadStep(pred, result, field)
or
summary = StoreStep(field) and
PromiseFlow::storeStep(pred, result, field)
or
summary = CopyStep(field) and
PromiseFlow::loadStoreStep(pred, result, field)
)
}
/**
* Gets the result from a single step through a promise, from `pred` with tracker `t2` to `result` with tracker `t`.
* This can be loading a resolved value from a promise, storing a value in a promise, or copying a resolved value from one promise to another.
*/
pragma[inline]
DataFlow::SourceNode promiseStep(
DataFlow::SourceNode pred, DataFlow::TypeTracker t, DataFlow::TypeTracker t2
) {
exists(DataFlow::Node mid, StepSummary summary | pred.flowsTo(mid) and t = t2.append(summary) |
result = PromiseTypeTracking::promiseStep(mid, summary)
)
}
}
private import semmle.javascript.dataflow.internal.PreCallGraphStep
/**
* A step related to promises.
*
* These steps are for `await p`, `new Promise()`, `Promise.resolve()`,
* `Promise.then()`, `Promise.catch()`, and `Promise.finally()`.
*/
private class PromiseStep extends LegacyPreCallGraphStep {
override predicate loadStep(DataFlow::Node obj, DataFlow::Node element, string prop) {
PromiseFlow::loadStep(obj, element, prop)
}
override predicate storeStep(DataFlow::Node element, DataFlow::SourceNode obj, string prop) {
PromiseFlow::storeStep(element, obj, prop)
}
override predicate loadStoreStep(DataFlow::Node pred, DataFlow::SourceNode succ, string prop) {
PromiseFlow::loadStoreStep(pred, succ, prop)
}
}
/**
* A step from `p -> await p` for the case where `p` is not a promise.
*
* In this case, `await p` just returns `p` itself. We block flow of the promise-related
* pseudo properties through this edge.
*/
private class RawAwaitStep extends DataFlow::SharedTypeTrackingStep {
override predicate withoutPropStep(
DataFlow::Node pred, DataFlow::Node succ, DataFlow::PropertySet props
) {
exists(AwaitExpr await |
pred = await.getOperand().flow() and
succ = await.flow() and
props instanceof Promises::PromiseProps
)
}
}
/**
* This module defines how data-flow propagates into and out of a Promise.
* The data-flow is based on pseudo-properties rather than tainting the Promise object (which is what `PromiseTaintStep` does).
*/
module PromiseFlow {
private predicate valueProp = Promises::valueProp/0;
private predicate errorProp = Promises::errorProp/0;
/**
* Holds if there is a step for loading a `value` from a `promise`.
* `prop` is either `valueProp()` if the value is a resolved value, or `errorProp()` if the promise has been rejected.
*/
predicate loadStep(DataFlow::Node promise, DataFlow::Node value, string prop) {
// await promise.
exists(AwaitExpr await | await.getOperand() = promise.asExpr() |
prop = valueProp() and
value.asExpr() = await
or
prop = errorProp() and
value = await.getExceptionTarget()
)
or
// promise.then()
exists(DataFlow::MethodCallNode call |
call.getMethodName() = "then" and promise = call.getReceiver()
|
prop = valueProp() and
value = call.getCallback(0).getParameter(0)
or
prop = errorProp() and
value = call.getCallback(1).getParameter(0)
)
or
// promise.catch()
exists(DataFlow::MethodCallNode call | call.getMethodName() = "catch" |
prop = errorProp() and
promise = call.getReceiver() and
value = call.getCallback(0).getParameter(0)
)
}
/**
* Holds if there is a step for storing a `value` into a promise `obj`.
* `prop` is either `valueProp()` if the value is a resolved value, or `errorProp()` if the promise has been rejected.
*/
predicate storeStep(DataFlow::Node value, DataFlow::SourceNode obj, string prop) {
// promise definition, e.g. `new Promise()`
exists(PromiseDefinition promise | obj = promise |
prop = valueProp() and
value = promise.getResolveParameter().getACall().getArgument(0)
or
prop = errorProp() and
value =
[
promise.getRejectParameter().getACall().getArgument(0),
promise.getExecutor().getExceptionalReturn()
]
)
or
// promise creation call, e.g. `Promise.resolve`.
exists(PromiseCreationCall promise | obj = promise |
not promise instanceof PromiseAllCreation and
prop = valueProp() and
value = promise.getValue()
or
prop = valueProp() and
value = promise.(PromiseAllCreation).getArrayNode()
)
or
// promise.then()
exists(DataFlow::MethodCallNode call | call.getMethodName() = "then" and obj = call |
prop = valueProp() and
value = call.getCallback([0 .. 1]).getAReturn()
or
prop = errorProp() and
value = call.getCallback([0 .. 1]).getExceptionalReturn()
)
or
// promise.catch()
exists(DataFlow::MethodCallNode call | call.getMethodName() = "catch" and obj = call |
prop = errorProp() and
value = call.getCallback(0).getExceptionalReturn()
or
prop = valueProp() and
value = call.getCallback(0).getAReturn()
)
or
// promise.finally()
exists(DataFlow::MethodCallNode call | call.getMethodName() = "finally" |
prop = errorProp() and
value = call.getCallback(0).getExceptionalReturn() and
obj = call
)
or
exists(DataFlow::FunctionNode f | f.getFunction().isAsync() |
// ordinary return
prop = valueProp() and
value = f.getAReturn() and
obj = f.getReturnNode()
or
// exceptional return
prop = errorProp() and
localExceptionStepWithAsyncFlag(value, obj, true)
)
}
/**
* Holds if there is a step copying a resolved/rejected promise value from promise `pred` to promise `succ`.
* `prop` is either `valueProp()` if the value is a resolved value, or `errorProp()` if the promise has been rejected.
*/
predicate loadStoreStep(DataFlow::Node pred, DataFlow::Node succ, string prop) {
// promise definition, e.g. `new Promise()`
exists(PromiseDefinition promise | succ = promise |
// Copy the value of a resolved promise to the value of this promise.
prop = valueProp() and
pred = promise.getResolveParameter().getACall().getArgument(0)
)
or
// promise creation call, e.g. `Promise.resolve`.
exists(PromiseCreationCall promise | succ = promise |
// Copy the value of a resolved promise to the value of this promise.
not promise instanceof PromiseAllCreation and
pred = promise.getValue() and
prop = valueProp()
or
pred = promise.(PromiseAllCreation).getArrayNode() and
prop = valueProp()
)
or
// promise.then()
exists(DataFlow::MethodCallNode call | call.getMethodName() = "then" and succ = call |
not exists(call.getArgument(1)) and
prop = errorProp() and
pred = call.getReceiver()
or
// read the value of a resolved/rejected promise that is returned
(prop = errorProp() or prop = valueProp()) and
pred = call.getCallback([0 .. 1]).getAReturn()
)
or
// promise.catch()
exists(DataFlow::MethodCallNode call | call.getMethodName() = "catch" and succ = call |
prop = valueProp() and
pred = call.getReceiver()
or
// read the value of a resolved/rejected promise that is returned
(prop = errorProp() or prop = valueProp()) and
pred = call.getCallback(0).getAReturn()
)
or
// promise.finally()
exists(DataFlow::MethodCallNode call | call.getMethodName() = "finally" and succ = call |
(prop = valueProp() or prop = errorProp()) and
pred = call.getReceiver()
or
// read the value of a rejected promise that is returned
prop = errorProp() and
pred = call.getCallback(0).getAReturn()
)
or
// return from `async` function
exists(DataFlow::FunctionNode f | f.getFunction().isAsync() |
// ordinary return
prop = valueProp() and
pred = f.getAReturn() and
succ = f.getReturnNode()
)
}
}
private class PromiseTaintStep extends TaintTracking::LegacyTaintStep {
override predicate promiseStep(DataFlow::Node pred, DataFlow::Node succ) {
// from `x` to `new Promise((res, rej) => res(x))`
pred = succ.(PromiseDefinition).getResolveParameter().getACall().getArgument(0)
or
// from `x` to `Promise.resolve(x)`
pred = succ.(PromiseCreationCall).getValue() and
not succ instanceof PromiseAllCreation
or
// from `arr` to `Promise.all(arr)`
pred = succ.(PromiseAllCreation).getArrayNode()
or
exists(DataFlow::MethodCallNode thn | thn.getMethodName() = "then" |
// from `p` to `x` in `p.then(x => ...)`
pred = thn.getReceiver() and
succ = thn.getCallback(0).getParameter(0)
or
// from `v` to `p.then(x => return v)`
pred = thn.getCallback([0 .. 1]).getAReturn() and
succ = thn
)
or
exists(DataFlow::MethodCallNode catch | catch.getMethodName() = "catch" |
// from `p` to `p.catch(..)`
pred = catch.getReceiver() and
succ = catch
or
// from `v` to `p.catch(x => return v)`
pred = catch.getCallback(0).getAReturn() and
succ = catch
)
or
// from `p` to `p.finally(..)`
exists(DataFlow::MethodCallNode finally | finally.getMethodName() = "finally" |
pred = finally.getReceiver() and
succ = finally
)
or
// from `x` to `await x`
exists(AwaitExpr await |
pred.getEnclosingExpr() = await.getOperand() and
succ.getEnclosingExpr() = await
)
or
exists(DataFlow::CallNode mapSeries |
mapSeries = DataFlow::moduleMember("bluebird", "mapSeries").getACall()
|
// from `xs` to `x` in `require("bluebird").mapSeries(xs, (x) => {...})`.
pred = mapSeries.getArgument(0) and
succ = mapSeries.getABoundCallbackParameter(1, 0)
or
// from `y` to `require("bluebird").mapSeries(x, x => y)`.
pred = mapSeries.getCallback(1).getAReturn() and
succ = mapSeries
)
}
}
/**
* Defines flow steps for return on async functions.
*/
private module AsyncReturnSteps {
/**
* A data-flow step for ordinary return from an async function in a taint configuration.
*/
private class AsyncTaintReturn extends TaintTracking::LegacyTaintStep {
override predicate step(DataFlow::Node pred, DataFlow::Node succ) {
exists(Function f |
f.isAsync() and
returnExpr(f, pred, _) and
succ.(DataFlow::FunctionReturnNode).getFunction() = f
)
}
}
}
/**
* Provides classes for working with the `bluebird` library (http://bluebirdjs.com).
*/
module Bluebird {
private DataFlow::SourceNode bluebird() {
result = DataFlow::globalVarRef("Promise") or // same as ES2015PromiseDefinition!
result = DataFlow::moduleImport("bluebird")
}
/**
* A promise object created by the bluebird `Promise` constructor.
*/
private class BluebirdPromiseDefinition extends PromiseDefinition, DataFlow::NewNode {
BluebirdPromiseDefinition() { this = bluebird().getAnInstantiation() }
override DataFlow::FunctionNode getExecutor() { result = this.getCallback(0) }
}
/**
* A resolved promise created by the bluebird `Promise.resolve` function.
*/
class ResolvedBluebidPromiseDefinition extends ResolvedPromiseDefinition {
ResolvedBluebidPromiseDefinition() { this = bluebird().getAMemberCall("resolve") }
override DataFlow::Node getValue() { result = this.getArgument(0) }
}
/**
* An aggregated promise produced either by `Promise.all`, `Promise.race` or `Promise.map`.
*/
class AggregateBluebirdPromiseDefinition extends PromiseCreationCall {
AggregateBluebirdPromiseDefinition() {
exists(string m | m = "all" or m = "race" or m = "map" | this = bluebird().getAMemberCall(m))
}
override DataFlow::Node getValue() {
result = this.getArgument(0).getALocalSource().(DataFlow::ArrayCreationNode).getAnElement()
}
}
/**
* A promise created using `Promise.all`:
*/
class BluebirdPromiseAllDefinition extends AggregateBluebirdPromiseDefinition, PromiseAllCreation {
BluebirdPromiseAllDefinition() { this.getCalleeName() = "all" }
override DataFlow::Node getArrayNode() { result = this.getArgument(0) }
}
/**
* An async function created using a call to `bluebird.coroutine`.
*/
class BluebirdCoroutineDefinition extends DataFlow::CallNode {
BluebirdCoroutineDefinition() { this = bluebird().getAMemberCall("coroutine") }
}
private class BluebirdCoroutineDefinitionAsPartialInvoke extends DataFlow::PartialInvokeNode::Range,
BluebirdCoroutineDefinition
{
override DataFlow::SourceNode getBoundFunction(DataFlow::Node callback, int boundArgs) {
boundArgs = 0 and
callback = this.getArgument(0) and
result = this
}
}
}
/**
* Provides classes for working with the `q` library (https://github.com/kriskowal/q) and the compatible `kew` library (https://github.com/Medium/kew).
*/
module Q {
/**
* A promise object created by the q `Promise` constructor.
*/
private class QPromiseDefinition extends PromiseDefinition, DataFlow::CallNode {
QPromiseDefinition() { this = DataFlow::moduleMember(["q", "kew"], "Promise").getACall() }
override DataFlow::FunctionNode getExecutor() { result = this.getCallback(0) }
}
}
private module ClosurePromise {
/**
* A promise created by a call `new goog.Promise(executor)`.
*/
private class ClosurePromiseDefinition extends PromiseDefinition, DataFlow::NewNode {
ClosurePromiseDefinition() { this = Closure::moduleImport("goog.Promise").getACall() }
override DataFlow::FunctionNode getExecutor() { result = this.getCallback(0) }
}
/**
* A promise created by a call `goog.Promise.resolve(value)`.
*/
private class ResolvedClosurePromiseDefinition extends ResolvedPromiseDefinition {
pragma[noinline]
ResolvedClosurePromiseDefinition() {
this = Closure::moduleImport("goog.Promise.resolve").getACall()
}
override DataFlow::Node getValue() { result = this.getArgument(0) }
}
/**
* Taint steps through closure promise methods.
*/
private class ClosurePromiseTaintStep extends TaintTracking::LegacyTaintStep {
override predicate step(DataFlow::Node pred, DataFlow::Node succ) {
// static methods in goog.Promise
exists(DataFlow::CallNode call, string name |
call = Closure::moduleImport("goog.Promise." + name).getACall() and
succ = call and
pred = call.getAnArgument()
|
name = ["all", "allSettled", "firstFulfilled", "race"]
)
or
// promise created through goog.promise.withResolver()
exists(DataFlow::CallNode resolver |
resolver = Closure::moduleImport("goog.Promise.withResolver").getACall() and
succ = resolver.getAPropertyRead("promise") and
pred = resolver.getAMethodCall("resolve").getArgument(0)
)
}
}
}
private module DynamicImportSteps {
/**
* A step from an export value to its uses via dynamic imports.
*
* For example:
* ```js
* // foo.js
* export default Foo
*
* // bar.js
* let Foo = await import('./foo');
* ```
*/
class DynamicImportStep extends LegacyPreCallGraphStep {
override predicate storeStep(DataFlow::Node pred, DataFlow::SourceNode succ, string prop) {
exists(DynamicImportExpr imprt |
pred = imprt.getImportedModule().getAnExportedValue("default") and
succ = imprt.flow() and
prop = Promises::valueProp()
)
}
override predicate step(DataFlow::Node pred, DataFlow::Node succ) {
// Special-case code like `(await import(x)).Foo` to boost type tracking depth.
exists(
DynamicImportExpr imprt, string name, DataFlow::Node mid, DataFlow::SourceNode awaited
|
pred = imprt.getImportedModule().getAnExportedValue(name) and
mid.getALocalSource() = imprt.flow() and
PromiseFlow::loadStep(mid, awaited, Promises::valueProp()) and
succ = awaited.getAPropertyRead(name)
)
}
}
}
/**
* Provides classes modeling libraries implementing `promisify` functions.
* That is, functions that convert callback style functions to functions that return a promise.
*/
module Promisify {
/**
* A call to a `promisifyAll` function.
* E.g. `require("bluebird").promisifyAll(...)`.
*/
class PromisifyAllCall extends DataFlow::CallNode {
PromisifyAllCall() {
this =
[
DataFlow::moduleMember(["bluebird", "@google-cloud/promisify", "es6-promisify"],
"promisifyAll"),
DataFlow::moduleMember("thenify-all", "withCallback"),
DataFlow::moduleImport([
"util-promisifyall", "pify", "thenify-all", "@gar/promisify", "util.promisify-all"
])
].getACall()
}
}
/**
* A call to a `promisify` function.
* E.g. `require("util").promisify(...)`.
*/
class PromisifyCall extends DataFlow::CallNode {
PromisifyCall() {
this = DataFlow::moduleImport(["util", "bluebird"]).getAMemberCall("promisify")
or
this = DataFlow::moduleImport(["pify", "util.promisify", "util-promisify"]).getACall()
or
this = DataFlow::moduleImport(["thenify", "@gar/promisify", "es6-promisify"]).getACall()
or
this = DataFlow::moduleMember("thenify", "withCallback").getACall()
or
this = DataFlow::moduleMember("@google-cloud/promisify", "promisify").getACall()
}
}
}