Skip to content

Commit 9497199

Browse files
committed
JS: add localFieldStep
1 parent b6648de commit 9497199

File tree

8 files changed

+57
-0
lines changed

8 files changed

+57
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,16 @@ module DataFlow {
10781078
)
10791079
}
10801080

1081+
/**
1082+
* Holds if there is a step from `pred` to `succ` through a field accessed through `this` in a class.
1083+
*/
1084+
predicate localFieldStep(DataFlow::Node pred, DataFlow::Node succ) {
1085+
exists (ClassNode cls, string prop |
1086+
pred = cls.getAReceiverNode().getAPropertyWrite(prop).getRhs() and
1087+
succ = cls.getAReceiverNode().getAPropertyRead(prop)
1088+
)
1089+
}
1090+
10811091
/**
10821092
* Gets the data flow node representing the source of definition `def`, taking
10831093
* flow through IIFE calls into account.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| tst2.js:3:14:3:14 | x | tst2.js:7:5:7:10 | this.x |
2+
| tst2.js:3:14:3:14 | x | tst2.js:8:25:8:30 | this.x |
3+
| tst2.js:3:14:3:14 | x | tst2.js:12:12:12:17 | this.x |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import javascript
2+
3+
from DataFlow::Node pred, DataFlow::Node succ
4+
where DataFlow::localFieldStep(pred, succ)
5+
select pred, succ

javascript/ql/test/library-tests/ClassNode/InstanceMember.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
| namespace.js:5:32:5:44 | function() {} | Baz.method | method |
2+
| tst2.js:6:9:9:3 | () {\\n ... .x;\\n } | C.method | method |
3+
| tst2.js:11:13:13:3 | () {\\n ... .x;\\n } | C.getter | getter |
24
| tst.js:4:17:4:21 | () {} | A.instanceMethod | method |
35
| tst.js:7:6:7:10 | () {} | A.bar | method |
46
| tst.js:9:10:9:14 | () {} | A.baz | getter |

javascript/ql/test/library-tests/ClassNode/InstanceMethod.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
| namespace.js:5:32:5:44 | function() {} | Baz.method |
2+
| tst2.js:6:9:9:3 | () {\\n ... .x;\\n } | C.method |
23
| tst.js:4:17:4:21 | () {} | A.instanceMethod |
34
| tst.js:7:6:7:10 | () {} | A.bar |
45
| tst.js:17:19:17:31 | function() {} | B.foo |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
| namespace.js:3:15:3:31 | function Baz() {} | namespace.js:3:15:3:14 | this |
2+
| namespace.js:3:15:3:31 | function Baz() {} | namespace.js:5:32:5:31 | this |
3+
| tst2.js:1:1:14:1 | class C ... ;\\n }\\n} | tst2.js:2:14:2:13 | this |
4+
| tst2.js:1:1:14:1 | class C ... ;\\n }\\n} | tst2.js:6:9:6:8 | this |
5+
| tst2.js:1:1:14:1 | class C ... ;\\n }\\n} | tst2.js:11:13:11:12 | this |
6+
| tst.js:3:1:10:1 | class A ... () {}\\n} | tst.js:3:9:3:8 | this |
7+
| tst.js:3:1:10:1 | class A ... () {}\\n} | tst.js:4:17:4:16 | this |
8+
| tst.js:3:1:10:1 | class A ... () {}\\n} | tst.js:7:6:7:5 | this |
9+
| tst.js:3:1:10:1 | class A ... () {}\\n} | tst.js:9:10:9:9 | this |
10+
| tst.js:13:1:13:21 | class A ... ds A {} | tst.js:13:20:13:19 | this |
11+
| tst.js:15:1:15:15 | function B() {} | tst.js:15:1:15:0 | this |
12+
| tst.js:15:1:15:15 | function B() {} | tst.js:17:19:17:18 | this |
13+
| tst.js:19:1:19:15 | function C() {} | tst.js:19:1:19:0 | this |
14+
| tst.js:19:1:19:15 | function C() {} | tst.js:21:19:21:18 | this |
15+
| tst.js:23:1:23:15 | function D() {} | tst.js:23:1:23:0 | this |
16+
| tst.js:23:1:23:15 | function D() {} | tst.js:25:13:25:12 | this |
17+
| tst.js:23:1:23:15 | function D() {} | tst.js:26:13:26:12 | this |
18+
| tst.js:23:1:23:15 | function D() {} | tst.js:27:4:27:3 | this |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import javascript
2+
3+
from DataFlow::ClassNode cls
4+
select cls, cls.getAReceiverNode()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class C {
2+
constructor(x) {
3+
this.x = x;
4+
}
5+
6+
method() {
7+
this.x;
8+
let closure = () => this.x;
9+
}
10+
11+
get getter() {
12+
return this.x;
13+
}
14+
}

0 commit comments

Comments
 (0)