Skip to content

Commit 25db666

Browse files
author
Stephan Brandauer
committed
ATM: new feature to list all imports in an endpoint's file
1 parent d0faf71 commit 25db666

2 files changed

Lines changed: 79 additions & 44 deletions

File tree

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/EndpointFeatures.qll

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ private newtype TEndpointFeature =
230230
TCalleeAccessPath() or
231231
TCalleeAccessPathWithStructuralInfo() or
232232
TEnclosingFunctionBody() or
233-
TCallee_AccessPath() or
234-
TInput_AccessPathFromCallee() or
235-
TInput_ArgumentIndex()
233+
TFileImports() or
234+
TCalleeFlexibleAccessPath() or
235+
TInputAccessPathFromCallee() or
236+
TInputArgumentIndex()
236237

237238
/**
238239
* An implementation of an endpoint feature: produces feature names and values for used in ML.
@@ -410,10 +411,29 @@ class EnclosingFunctionBody extends EndpointFeature, TEnclosingFunctionBody {
410411
}
411412
}
412413

414+
/** The feature for the imports defined in the file containing an endpoint. */
415+
class FileImports extends EndpointFeature, TFileImports {
416+
override string getName() { result = "fileImports" }
417+
418+
override string getValue(DataFlow::Node endpoint) {
419+
result =
420+
concat(string importPath |
421+
importPath = SyntacticUtilities::getImportPathForFile(endpoint.getFile())
422+
|
423+
importPath, " " order by importPath
424+
)
425+
}
426+
}
427+
413428
/**
414429
* Syntactic utilities for feature value computation.
415430
*/
416431
private module SyntacticUtilities {
432+
/** Gets an import located in `file`. */
433+
string getImportPathForFile(File file) {
434+
result = any(Import imp | imp.getFile() = file).getImportedPath().getValue()
435+
}
436+
417437
/**
418438
* Gets a property initializer value in a an object literal or one of its nested object literals.
419439
*/
@@ -542,8 +562,8 @@ private module SyntacticUtilities {
542562
* foo[complex()].bar(endpoint); // -> foo.?.bar
543563
* ```
544564
*/
545-
class Callee_AccessPath extends EndpointFeature, TCallee_AccessPath {
546-
override string getName() { result = "Callee_AccessPath" }
565+
class CalleeFlexibleAccessPath extends EndpointFeature, TCalleeFlexibleAccessPath {
566+
override string getName() { result = "CalleeFlexibleAccessPath" }
547567

548568
override string getValue(DataFlow::Node endpoint) {
549569
exists(DataFlow::InvokeNode invk |
@@ -565,16 +585,16 @@ class Callee_AccessPath extends EndpointFeature, TCallee_AccessPath {
565585
*
566586
* "Containment" is syntactic, and currently means that the endpoint is an argument to the call, or that the endpoint is a (nested) property value of an argument.
567587
*
568-
* This feature, together with `Input_ArgumentIndex` is intended as a far superior version of the `ArgumentIndexFeature`.
588+
* This feature, together with `InputArgumentIndex` is intended as a far superior version of the `ArgumentIndexFeature`.
569589
*
570590
* Examples:
571591
* ```
572592
* foo({ bar: endpoint }); // -> bar
573593
* foo(x, { bar: { baz: endpoint } }); // -> bar.baz
574594
* ```
575595
*/
576-
class Input_AccessPathFromCallee extends EndpointFeature, TInput_AccessPathFromCallee {
577-
override string getName() { result = "Input_AccessPathFromCallee" }
596+
class InputAccessPathFromCallee extends EndpointFeature, TInputAccessPathFromCallee {
597+
override string getName() { result = "InputAccessPathFromCallee" }
578598

579599
override string getValue(DataFlow::Node endpoint) {
580600
exists(DataFlow::InvokeNode invk |
@@ -600,8 +620,8 @@ class Input_AccessPathFromCallee extends EndpointFeature, TInput_AccessPathFromC
600620
* foo(x, { bar: { baz: endpoint } }); // -> 1
601621
* ```
602622
*/
603-
class Input_ArgumentIndex extends EndpointFeature, TInput_ArgumentIndex {
604-
override string getName() { result = "Input_ArgumentIndex" }
623+
class InputArgumentIndex extends EndpointFeature, TInputArgumentIndex {
624+
override string getName() { result = "InputArgumentIndex" }
605625

606626
override string getValue(DataFlow::Node endpoint) {
607627
exists(DataFlow::InvokeNode invk, DataFlow::Node arg, int i | arg = invk.getArgument(i) |
Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,127 @@
1-
| test.html:2:61:2:68 | endpoint | Callee_AccessPath | $event.target.files.item |
2-
| test.html:2:61:2:68 | endpoint | Input_ArgumentIndex | 0 |
1+
| test.html:2:61:2:68 | endpoint | CalleeFlexibleAccessPath | $event.target.files.item |
2+
| test.html:2:61:2:68 | endpoint | InputArgumentIndex | 0 |
33
| test.html:2:61:2:68 | endpoint | argumentIndex | 0 |
44
| test.html:2:61:2:68 | endpoint | calleeAccessPath | |
55
| test.html:2:61:2:68 | endpoint | calleeAccessPathWithStructuralInfo | |
66
| test.html:2:61:2:68 | endpoint | calleeName | item |
7-
| test.js:2:7:2:14 | endpoint | Callee_AccessPath | f |
8-
| test.js:2:7:2:14 | endpoint | Input_ArgumentIndex | 0 |
7+
| test.html:2:61:2:68 | endpoint | fileImports | |
8+
| test.js:2:7:2:14 | endpoint | CalleeFlexibleAccessPath | f |
9+
| test.js:2:7:2:14 | endpoint | InputArgumentIndex | 0 |
910
| test.js:2:7:2:14 | endpoint | argumentIndex | 0 |
1011
| test.js:2:7:2:14 | endpoint | calleeAccessPath | |
1112
| test.js:2:7:2:14 | endpoint | calleeAccessPathWithStructuralInfo | |
1213
| test.js:2:7:2:14 | endpoint | calleeName | f |
1314
| test.js:2:7:2:14 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
1415
| test.js:2:7:2:14 | endpoint | enclosingFunctionName | |
15-
| test.js:3:11:3:18 | endpoint | Callee_AccessPath | f |
16-
| test.js:3:11:3:18 | endpoint | Input_AccessPathFromCallee | 0.p |
17-
| test.js:3:11:3:18 | endpoint | Input_ArgumentIndex | 0 |
16+
| test.js:2:7:2:14 | endpoint | fileImports | foo |
17+
| test.js:3:11:3:18 | endpoint | CalleeFlexibleAccessPath | f |
18+
| test.js:3:11:3:18 | endpoint | InputAccessPathFromCallee | 0.p |
19+
| test.js:3:11:3:18 | endpoint | InputArgumentIndex | 0 |
1820
| test.js:3:11:3:18 | endpoint | calleeAccessPath | |
1921
| test.js:3:11:3:18 | endpoint | calleeAccessPathWithStructuralInfo | |
2022
| test.js:3:11:3:18 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
2123
| test.js:3:11:3:18 | endpoint | enclosingFunctionName | |
22-
| test.js:4:15:4:22 | endpoint | Callee_AccessPath | f |
23-
| test.js:4:15:4:22 | endpoint | Input_AccessPathFromCallee | 0.p.q |
24-
| test.js:4:15:4:22 | endpoint | Input_ArgumentIndex | 0 |
24+
| test.js:3:11:3:18 | endpoint | fileImports | foo |
25+
| test.js:4:15:4:22 | endpoint | CalleeFlexibleAccessPath | f |
26+
| test.js:4:15:4:22 | endpoint | InputAccessPathFromCallee | 0.p.q |
27+
| test.js:4:15:4:22 | endpoint | InputArgumentIndex | 0 |
2528
| test.js:4:15:4:22 | endpoint | calleeAccessPath | |
2629
| test.js:4:15:4:22 | endpoint | calleeAccessPathWithStructuralInfo | |
2730
| test.js:4:15:4:22 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
2831
| test.js:4:15:4:22 | endpoint | enclosingFunctionName | |
29-
| test.js:5:9:5:16 | endpoint | Callee_AccessPath | o.m |
30-
| test.js:5:9:5:16 | endpoint | Input_ArgumentIndex | 0 |
32+
| test.js:4:15:4:22 | endpoint | fileImports | foo |
33+
| test.js:5:9:5:16 | endpoint | CalleeFlexibleAccessPath | o.m |
34+
| test.js:5:9:5:16 | endpoint | InputArgumentIndex | 0 |
3135
| test.js:5:9:5:16 | endpoint | argumentIndex | 0 |
3236
| test.js:5:9:5:16 | endpoint | calleeAccessPath | |
3337
| test.js:5:9:5:16 | endpoint | calleeAccessPathWithStructuralInfo | |
3438
| test.js:5:9:5:16 | endpoint | calleeName | m |
3539
| test.js:5:9:5:16 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
3640
| test.js:5:9:5:16 | endpoint | enclosingFunctionName | |
41+
| test.js:5:9:5:16 | endpoint | fileImports | foo |
3742
| test.js:5:9:5:16 | endpoint | receiverName | o |
38-
| test.js:6:13:6:20 | endpoint | Callee_AccessPath | o.m |
39-
| test.js:6:13:6:20 | endpoint | Input_AccessPathFromCallee | 0.p |
40-
| test.js:6:13:6:20 | endpoint | Input_ArgumentIndex | 0 |
43+
| test.js:6:13:6:20 | endpoint | CalleeFlexibleAccessPath | o.m |
44+
| test.js:6:13:6:20 | endpoint | InputAccessPathFromCallee | 0.p |
45+
| test.js:6:13:6:20 | endpoint | InputArgumentIndex | 0 |
4146
| test.js:6:13:6:20 | endpoint | calleeAccessPath | |
4247
| test.js:6:13:6:20 | endpoint | calleeAccessPathWithStructuralInfo | |
4348
| test.js:6:13:6:20 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
4449
| test.js:6:13:6:20 | endpoint | enclosingFunctionName | |
45-
| test.js:7:17:7:24 | endpoint | Callee_AccessPath | o.m |
46-
| test.js:7:17:7:24 | endpoint | Input_AccessPathFromCallee | 0.p.q |
47-
| test.js:7:17:7:24 | endpoint | Input_ArgumentIndex | 0 |
50+
| test.js:6:13:6:20 | endpoint | fileImports | foo |
51+
| test.js:7:17:7:24 | endpoint | CalleeFlexibleAccessPath | o.m |
52+
| test.js:7:17:7:24 | endpoint | InputAccessPathFromCallee | 0.p.q |
53+
| test.js:7:17:7:24 | endpoint | InputArgumentIndex | 0 |
4854
| test.js:7:17:7:24 | endpoint | calleeAccessPath | |
4955
| test.js:7:17:7:24 | endpoint | calleeAccessPathWithStructuralInfo | |
5056
| test.js:7:17:7:24 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
5157
| test.js:7:17:7:24 | endpoint | enclosingFunctionName | |
52-
| test.js:8:11:8:18 | endpoint | Callee_AccessPath | F |
53-
| test.js:8:11:8:18 | endpoint | Input_ArgumentIndex | 0 |
58+
| test.js:7:17:7:24 | endpoint | fileImports | foo |
59+
| test.js:8:11:8:18 | endpoint | CalleeFlexibleAccessPath | F |
60+
| test.js:8:11:8:18 | endpoint | InputArgumentIndex | 0 |
5461
| test.js:8:11:8:18 | endpoint | calleeAccessPath | |
5562
| test.js:8:11:8:18 | endpoint | calleeAccessPathWithStructuralInfo | |
5663
| test.js:8:11:8:18 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
5764
| test.js:8:11:8:18 | endpoint | enclosingFunctionName | |
58-
| test.js:9:17:9:24 | endpoint | Callee_AccessPath | o.m().m().m |
59-
| test.js:9:17:9:24 | endpoint | Input_ArgumentIndex | 0 |
65+
| test.js:8:11:8:18 | endpoint | fileImports | foo |
66+
| test.js:9:17:9:24 | endpoint | CalleeFlexibleAccessPath | o.m().m().m |
67+
| test.js:9:17:9:24 | endpoint | InputArgumentIndex | 0 |
6068
| test.js:9:17:9:24 | endpoint | argumentIndex | 0 |
6169
| test.js:9:17:9:24 | endpoint | calleeAccessPath | |
6270
| test.js:9:17:9:24 | endpoint | calleeAccessPathWithStructuralInfo | |
6371
| test.js:9:17:9:24 | endpoint | calleeName | m |
6472
| test.js:9:17:9:24 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
6573
| test.js:9:17:9:24 | endpoint | enclosingFunctionName | |
66-
| test.js:10:9:10:16 | endpoint | Callee_AccessPath | f() |
67-
| test.js:10:9:10:16 | endpoint | Input_ArgumentIndex | 0 |
74+
| test.js:9:17:9:24 | endpoint | fileImports | foo |
75+
| test.js:10:9:10:16 | endpoint | CalleeFlexibleAccessPath | f() |
76+
| test.js:10:9:10:16 | endpoint | InputArgumentIndex | 0 |
6877
| test.js:10:9:10:16 | endpoint | argumentIndex | 0 |
6978
| test.js:10:9:10:16 | endpoint | calleeAccessPath | |
7079
| test.js:10:9:10:16 | endpoint | calleeAccessPathWithStructuralInfo | |
7180
| test.js:10:9:10:16 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
7281
| test.js:10:9:10:16 | endpoint | enclosingFunctionName | |
73-
| test.js:11:12:11:19 | endpoint | Callee_AccessPath | o.?.m |
74-
| test.js:11:12:11:19 | endpoint | Input_ArgumentIndex | 0 |
82+
| test.js:10:9:10:16 | endpoint | fileImports | foo |
83+
| test.js:11:12:11:19 | endpoint | CalleeFlexibleAccessPath | o.?.m |
84+
| test.js:11:12:11:19 | endpoint | InputArgumentIndex | 0 |
7585
| test.js:11:12:11:19 | endpoint | argumentIndex | 0 |
7686
| test.js:11:12:11:19 | endpoint | calleeAccessPath | |
7787
| test.js:11:12:11:19 | endpoint | calleeAccessPathWithStructuralInfo | |
7888
| test.js:11:12:11:19 | endpoint | calleeName | m |
7989
| test.js:11:12:11:19 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
8090
| test.js:11:12:11:19 | endpoint | enclosingFunctionName | |
81-
| test.js:12:16:12:23 | endpoint | Callee_AccessPath | o.m.?.p.m |
82-
| test.js:12:16:12:23 | endpoint | Input_ArgumentIndex | 0 |
91+
| test.js:11:12:11:19 | endpoint | fileImports | foo |
92+
| test.js:12:16:12:23 | endpoint | CalleeFlexibleAccessPath | o.m.?.p.m |
93+
| test.js:12:16:12:23 | endpoint | InputArgumentIndex | 0 |
8394
| test.js:12:16:12:23 | endpoint | argumentIndex | 0 |
8495
| test.js:12:16:12:23 | endpoint | calleeAccessPath | |
8596
| test.js:12:16:12:23 | endpoint | calleeAccessPathWithStructuralInfo | |
8697
| test.js:12:16:12:23 | endpoint | calleeName | m |
8798
| test.js:12:16:12:23 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
8899
| test.js:12:16:12:23 | endpoint | enclosingFunctionName | |
89-
| test.js:13:15:13:22 | endpoint | Callee_AccessPath | (await p) |
90-
| test.js:13:15:13:22 | endpoint | Input_ArgumentIndex | 0 |
100+
| test.js:12:16:12:23 | endpoint | fileImports | foo |
101+
| test.js:13:15:13:22 | endpoint | CalleeFlexibleAccessPath | (await p) |
102+
| test.js:13:15:13:22 | endpoint | InputArgumentIndex | 0 |
91103
| test.js:13:15:13:22 | endpoint | argumentIndex | 0 |
92104
| test.js:13:15:13:22 | endpoint | calleeAccessPath | |
93105
| test.js:13:15:13:22 | endpoint | calleeAccessPathWithStructuralInfo | |
94106
| test.js:13:15:13:22 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
95107
| test.js:13:15:13:22 | endpoint | enclosingFunctionName | |
96-
| test.js:14:27:14:34 | endpoint | Callee_AccessPath | import(!).bar.baz |
97-
| test.js:14:27:14:34 | endpoint | Input_ArgumentIndex | 0 |
108+
| test.js:13:15:13:22 | endpoint | fileImports | foo |
109+
| test.js:14:27:14:34 | endpoint | CalleeFlexibleAccessPath | import(!).bar.baz |
110+
| test.js:14:27:14:34 | endpoint | InputArgumentIndex | 0 |
98111
| test.js:14:27:14:34 | endpoint | argumentIndex | 0 |
99112
| test.js:14:27:14:34 | endpoint | calleeAccessPath | foo bar baz |
100113
| test.js:14:27:14:34 | endpoint | calleeAccessPathWithStructuralInfo | foo member bar member baz instanceorreturn |
101114
| test.js:14:27:14:34 | endpoint | calleeApiName | foo |
102115
| test.js:14:27:14:34 | endpoint | calleeName | baz |
103116
| test.js:14:27:14:34 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
104117
| test.js:14:27:14:34 | endpoint | enclosingFunctionName | |
105-
| test.js:16:13:16:20 | endpoint | Callee_AccessPath | bar |
106-
| test.js:16:13:16:20 | endpoint | Input_ArgumentIndex | 0 |
118+
| test.js:14:27:14:34 | endpoint | fileImports | foo |
119+
| test.js:16:13:16:20 | endpoint | CalleeFlexibleAccessPath | bar |
120+
| test.js:16:13:16:20 | endpoint | InputArgumentIndex | 0 |
107121
| test.js:16:13:16:20 | endpoint | argumentIndex | 0 |
108122
| test.js:16:13:16:20 | endpoint | calleeAccessPath | |
109123
| test.js:16:13:16:20 | endpoint | calleeAccessPathWithStructuralInfo | |
110124
| test.js:16:13:16:20 | endpoint | calleeName | bar |
111125
| test.js:16:13:16:20 | endpoint | enclosingFunctionBody | f endpoint f p endpoint f p q endpoint o m endpoint o m p endpoint o m p q endpoint F endpoint o m m m endpoint f endpoint o x m endpoint o m x p m endpoint p endpoint foo bar baz endpoint foo bar endpoint |
112126
| test.js:16:13:16:20 | endpoint | enclosingFunctionName | |
127+
| test.js:16:13:16:20 | endpoint | fileImports | foo |

0 commit comments

Comments
 (0)