Skip to content

Commit 2f14a62

Browse files
committed
generalize RxJS pipes
1 parent 310baab commit 2f14a62

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

javascript/ql/src/semmle/javascript/frameworks/RxJS.qll

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private class RxJsSubscribeStep extends TaintTracking::SharedTaintStep {
2323
* created by the `map` call.
2424
*/
2525
private DataFlow::Node pipeInput(DataFlow::CallNode pipe) {
26-
pipe = DataFlow::moduleMember("rxjs/operators", ["map", "filter"]).getACall() and
26+
pipe = DataFlow::moduleMember("rxjs/operators", any(string s | not s = "catchError")).getACall() and
2727
result = pipe.getCallback(0).getParameter(0)
2828
}
2929

@@ -48,30 +48,56 @@ private DataFlow::Node pipeOutput(DataFlow::CallNode pipe) {
4848
* be special-cased.
4949
*/
5050
private predicate isIdentityPipe(DataFlow::CallNode pipe) {
51-
pipe = DataFlow::moduleMember("rxjs/operators", "catchError").getACall()
51+
pipe = DataFlow::moduleMember("rxjs/operators", ["catchError", "tap"]).getACall()
52+
}
53+
54+
/**
55+
* A call to `pipe`, which is assumed to be an `rxjs/operators` pipe.
56+
*
57+
* Has utility methods `getInput`/`getOutput` to get the input/output of each
58+
* element of the pipe.
59+
* These utility methods automatically handle itentity pipes, and the
60+
* first/last elements of the pipe.
61+
*/
62+
private class RxJSPipe extends DataFlow::MethodCallNode {
63+
RxJSPipe() { this.getMethodName() = "pipe" }
64+
65+
/**
66+
* Gets an input to pipe element `i`.
67+
* Or if `i` is equal to the number of elements, gets the output of the pipe (the call itself)
68+
*/
69+
DataFlow::Node getInput(int i) {
70+
result = pipeInput(this.getArgument(i).getALocalSource())
71+
or
72+
i = this.getNumArgument() and
73+
result = this
74+
}
75+
76+
/**
77+
* Gets an output from pipe element `i`.
78+
* Handles identity pipes by getting the output from the previous element.
79+
* If `i` is -1, gets the receiver to the call, which started the pipe.
80+
*/
81+
DataFlow::Node getOutput(int i) {
82+
isIdentityPipe(this.getArgument(i).getALocalSource()) and
83+
result = getOutput(i - 1)
84+
or
85+
not isIdentityPipe(this.getArgument(i).getALocalSource()) and
86+
result = pipeOutput(this.getArgument(i).getALocalSource())
87+
or
88+
i = -1 and
89+
result = this.getReceiver()
90+
}
5291
}
5392

5493
/**
5594
* A step in or out of the map callback in a call of form `x.pipe(map(y => ...))`.
5695
*/
5796
private class RxJsPipeMapStep extends TaintTracking::SharedTaintStep {
5897
override predicate heapStep(DataFlow::Node pred, DataFlow::Node succ) {
59-
exists(DataFlow::MethodCallNode call | call.getMethodName() = "pipe" |
60-
pred = call.getReceiver() and
61-
succ = pipeInput(call.getArgument(0).getALocalSource())
62-
or
63-
exists(int i |
64-
pred = pipeOutput(call.getArgument(i).getALocalSource()) and
65-
succ = pipeInput(call.getArgument(i + 1).getALocalSource())
66-
)
67-
or
68-
pred = pipeOutput(call.getLastArgument().getALocalSource()) and
69-
succ = call
70-
or
71-
// Handle a common case where the last step is `catchError`.
72-
isIdentityPipe(call.getLastArgument().getALocalSource()) and
73-
pred = pipeOutput(call.getArgument(call.getNumArgument() - 2)) and
74-
succ = call
98+
exists(RxJSPipe pipe, int i |
99+
pred = pipe.getOutput(i) and
100+
succ = pipe.getInput(i + 1)
75101
)
76102
}
77103
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ typeInferenceMismatch
109109
| promise.js:10:24:10:31 | source() | promise.js:10:8:10:32 | Promise ... urce()) |
110110
| promise.js:12:20:12:27 | source() | promise.js:13:8:13:23 | resolver.promise |
111111
| rxjs.js:3:1:3:8 | source() | rxjs.js:10:14:10:17 | data |
112+
| rxjs.js:13:1:13:8 | source() | rxjs.js:17:23:17:23 | x |
113+
| rxjs.js:13:1:13:8 | source() | rxjs.js:18:23:18:23 | x |
114+
| rxjs.js:13:1:13:8 | source() | rxjs.js:22:14:22:17 | data |
112115
| sanitizer-function.js:12:17:12:24 | source() | sanitizer-function.js:14:10:14:14 | taint |
113116
| sanitizer-function.js:12:17:12:24 | source() | sanitizer-function.js:33:14:33:18 | taint |
114117
| sanitizer-guards.js:2:11:2:18 | source() | sanitizer-guards.js:4:8:4:8 | x |

javascript/ql/test/library-tests/TaintTracking/rxjs.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { map, catchError } from 'rxjs/operators';
1+
import { map, tap, catchError } from 'rxjs/operators';
22

33
source()
44
.pipe(
@@ -9,3 +9,15 @@ source()
99
.subscribe(data => {
1010
sink(data)
1111
});
12+
13+
source()
14+
.pipe(
15+
map(x => x + 'foo'),
16+
// `tap` taps into the source observable, so like `subscribe` but inside the pipe.
17+
tap(x => sink(x)),
18+
tap(x => sink(x)),
19+
catchError(err => {})
20+
)
21+
.subscribe(data => {
22+
sink(data)
23+
});

0 commit comments

Comments
 (0)