Skip to content

Commit 5ad6c05

Browse files
authored
Merge pull request #10205 from asgerf/mad-generics
Support type variables in MaD typings
2 parents 65095e0 + 0d88d20 commit 5ad6c05

File tree

6 files changed

+419
-12
lines changed

6 files changed

+419
-12
lines changed

javascript/ql/lib/semmle/javascript/frameworks/data/internal/ApiGraphModels.qll

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ module ModelInput {
155155
*/
156156
abstract predicate row(string row);
157157
}
158+
159+
/**
160+
* A unit class for adding additional type variable model rows.
161+
*/
162+
class TypeVariableModelCsv extends Unit {
163+
/**
164+
* Holds if `row` specifies a path through a type variable.
165+
*
166+
* A row of form,
167+
* ```
168+
* name;path
169+
* ```
170+
* means `path` can be substituted for a token `TypeVar[name]`.
171+
*/
172+
abstract predicate row(string row);
173+
}
158174
}
159175

160176
private import ModelInput
@@ -182,6 +198,8 @@ private predicate summaryModel(string row) { any(SummaryModelCsv s).row(inverseP
182198

183199
private predicate typeModel(string row) { any(TypeModelCsv s).row(inversePad(row)) }
184200

201+
private predicate typeVariableModel(string row) { any(TypeVariableModelCsv s).row(inversePad(row)) }
202+
185203
/** Holds if a source model exists for the given parameters. */
186204
predicate sourceModel(string package, string type, string path, string kind) {
187205
exists(string row |
@@ -219,7 +237,7 @@ private predicate summaryModel(
219237
)
220238
}
221239

222-
/** Holds if an type model exists for the given parameters. */
240+
/** Holds if a type model exists for the given parameters. */
223241
private predicate typeModel(
224242
string package1, string type1, string package2, string type2, string path
225243
) {
@@ -233,6 +251,15 @@ private predicate typeModel(
233251
)
234252
}
235253

254+
/** Holds if a type variable model exists for the given parameters. */
255+
private predicate typeVariableModel(string name, string path) {
256+
exists(string row |
257+
typeVariableModel(row) and
258+
row.splitAt(";", 0) = name and
259+
row.splitAt(";", 1) = path
260+
)
261+
}
262+
236263
/**
237264
* Gets a package that should be seen as an alias for the given other `package`,
238265
* or the `package` itself.
@@ -253,7 +280,7 @@ private predicate isRelevantPackage(string package) {
253280
sourceModel(package, _, _, _) or
254281
sinkModel(package, _, _, _) or
255282
summaryModel(package, _, _, _, _, _) or
256-
typeModel(package, _, _, _, _)
283+
typeModel(_, _, package, _, _)
257284
) and
258285
(
259286
Specific::isPackageUsed(package)
@@ -290,6 +317,8 @@ private class AccessPathRange extends AccessPath::Range {
290317
summaryModel(package, _, _, this, _, _) or
291318
summaryModel(package, _, _, _, this, _)
292319
)
320+
or
321+
typeVariableModel(_, this)
293322
}
294323
}
295324

@@ -361,13 +390,93 @@ private API::Node getNodeFromPath(string package, string type, AccessPath path,
361390
// Similar to the other recursive case, but where the path may have stepped through one or more call-site filters
362391
result =
363392
getSuccessorFromInvoke(getInvocationFromPath(package, type, path, n - 1), path.getToken(n - 1))
393+
or
394+
// Apply a subpath
395+
result =
396+
getNodeFromSubPath(getNodeFromPath(package, type, path, n - 1), getSubPathAt(path, n - 1))
397+
or
398+
// Apply a type step
399+
typeStep(getNodeFromPath(package, type, path, n), result)
400+
}
401+
402+
/**
403+
* Gets a subpath for the `TypeVar` token found at the `n`th token of `path`.
404+
*/
405+
pragma[nomagic]
406+
private AccessPath getSubPathAt(AccessPath path, int n) {
407+
exists(string typeVarName |
408+
path.getToken(n).getAnArgument("TypeVar") = typeVarName and
409+
typeVariableModel(typeVarName, result)
410+
)
411+
}
412+
413+
/**
414+
* Gets a node that is found by evaluating the first `n` tokens of `subPath` starting at `base`.
415+
*/
416+
pragma[nomagic]
417+
private API::Node getNodeFromSubPath(API::Node base, AccessPath subPath, int n) {
418+
exists(AccessPath path, int k |
419+
base = [getNodeFromPath(_, _, path, k), getNodeFromSubPath(_, path, k)] and
420+
subPath = getSubPathAt(path, k) and
421+
result = base and
422+
n = 0
423+
)
424+
or
425+
exists(string package, string type, AccessPath basePath |
426+
typeStepModel(package, type, basePath, subPath) and
427+
base = getNodeFromPath(package, type, basePath) and
428+
result = base and
429+
n = 0
430+
)
431+
or
432+
result = getSuccessorFromNode(getNodeFromSubPath(base, subPath, n - 1), subPath.getToken(n - 1))
433+
or
434+
result =
435+
getSuccessorFromInvoke(getInvocationFromSubPath(base, subPath, n - 1), subPath.getToken(n - 1))
436+
or
437+
result =
438+
getNodeFromSubPath(getNodeFromSubPath(base, subPath, n - 1), getSubPathAt(subPath, n - 1))
439+
or
440+
typeStep(getNodeFromSubPath(base, subPath, n), result)
441+
}
442+
443+
/**
444+
* Gets a call site that is found by evaluating the first `n` tokens of `subPath` starting at `base`.
445+
*/
446+
private Specific::InvokeNode getInvocationFromSubPath(API::Node base, AccessPath subPath, int n) {
447+
result = Specific::getAnInvocationOf(getNodeFromSubPath(base, subPath, n))
448+
or
449+
result = getInvocationFromSubPath(base, subPath, n - 1) and
450+
invocationMatchesCallSiteFilter(result, subPath.getToken(n - 1))
451+
}
452+
453+
/**
454+
* Gets a node that is found by evaluating `subPath` starting at `base`.
455+
*/
456+
pragma[nomagic]
457+
private API::Node getNodeFromSubPath(API::Node base, AccessPath subPath) {
458+
result = getNodeFromSubPath(base, subPath, subPath.getNumToken())
364459
}
365460

366461
/** Gets the node identified by the given `(package, type, path)` tuple. */
367462
API::Node getNodeFromPath(string package, string type, AccessPath path) {
368463
result = getNodeFromPath(package, type, path, path.getNumToken())
369464
}
370465

466+
pragma[nomagic]
467+
private predicate typeStepModel(string package, string type, AccessPath basePath, AccessPath output) {
468+
summaryModel(package, type, basePath, "", output, "type")
469+
}
470+
471+
pragma[nomagic]
472+
private predicate typeStep(API::Node pred, API::Node succ) {
473+
exists(string package, string type, AccessPath basePath, AccessPath output |
474+
typeStepModel(package, type, basePath, output) and
475+
pred = getNodeFromPath(package, type, basePath) and
476+
succ = getNodeFromSubPath(pred, output)
477+
)
478+
}
479+
371480
/**
372481
* Gets an invocation identified by the given `(package, type, path)` tuple.
373482
*
@@ -390,7 +499,7 @@ Specific::InvokeNode getInvocationFromPath(string package, string type, AccessPa
390499
*/
391500
bindingset[name]
392501
predicate isValidTokenNameInIdentifyingAccessPath(string name) {
393-
name = ["Argument", "Parameter", "ReturnValue", "WithArity"]
502+
name = ["Argument", "Parameter", "ReturnValue", "WithArity", "TypeVar"]
394503
or
395504
Specific::isExtraValidTokenNameInIdentifyingAccessPath(name)
396505
}
@@ -418,6 +527,9 @@ predicate isValidTokenArgumentInIdentifyingAccessPath(string name, string argume
418527
name = "WithArity" and
419528
argument.regexpMatch("\\d+(\\.\\.(\\d+)?)?")
420529
or
530+
name = "TypeVar" and
531+
exists(argument)
532+
or
421533
Specific::isExtraValidTokenArgumentInIdentifyingAccessPath(name, argument)
422534
}
423535

@@ -489,6 +601,8 @@ module ModelOutput {
489601
any(SummaryModelCsv csv).row(row) and kind = "summary" and expectedArity = 6
490602
or
491603
any(TypeModelCsv csv).row(row) and kind = "type" and expectedArity = 5
604+
or
605+
any(TypeVariableModelCsv csv).row(row) and kind = "type-variable" and expectedArity = 2
492606
|
493607
actualArity = count(row.indexOf(";")) + 1 and
494608
actualArity != expectedArity and
@@ -499,7 +613,7 @@ module ModelOutput {
499613
or
500614
// Check names and arguments of access path tokens
501615
exists(AccessPath path, AccessPathToken token |
502-
isRelevantFullPath(_, _, path) and
616+
(isRelevantFullPath(_, _, path) or typeVariableModel(_, path)) and
503617
token = path.getToken(_)
504618
|
505619
not isValidTokenNameInIdentifyingAccessPath(token.getName()) and

javascript/ql/test/library-tests/frameworks/data/test.expected

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ taintFlow
5858
| test.js:207:24:207:31 | source() | test.js:207:24:207:31 | source() |
5959
| test.js:208:24:208:31 | source() | test.js:208:24:208:31 | source() |
6060
| test.js:211:34:211:41 | source() | test.js:211:34:211:41 | source() |
61+
| test.js:214:34:214:41 | source() | test.js:214:34:214:41 | source() |
62+
| test.js:223:45:223:52 | source() | test.js:223:45:223:52 | source() |
63+
| test.js:225:39:225:46 | source() | test.js:225:39:225:46 | source() |
64+
| test.js:226:50:226:57 | source() | test.js:226:50:226:57 | source() |
65+
| test.js:230:59:230:66 | source() | test.js:230:59:230:66 | source() |
66+
| test.js:231:59:231:66 | source() | test.js:231:59:231:66 | source() |
67+
| test.js:232:59:232:66 | source() | test.js:232:59:232:66 | source() |
68+
| test.js:233:59:233:66 | source() | test.js:233:59:233:66 | source() |
6169
isSink
6270
| test.js:54:18:54:25 | source() | test-sink |
6371
| test.js:55:22:55:29 | source() | test-sink |
@@ -119,6 +127,15 @@ isSink
119127
| test.js:207:24:207:31 | source() | test-sink |
120128
| test.js:208:24:208:31 | source() | test-sink |
121129
| test.js:211:34:211:41 | source() | test-sink |
130+
| test.js:214:34:214:41 | source() | test-sink |
131+
| test.js:222:52:222:52 | 0 | test-sink |
132+
| test.js:223:45:223:52 | source() | test-sink |
133+
| test.js:225:39:225:46 | source() | test-sink |
134+
| test.js:226:50:226:57 | source() | test-sink |
135+
| test.js:230:59:230:66 | source() | test-sink |
136+
| test.js:231:59:231:66 | source() | test-sink |
137+
| test.js:232:59:232:66 | source() | test-sink |
138+
| test.js:233:59:233:66 | source() | test-sink |
122139
syntaxErrors
123140
| Member[foo |
124141
| Member[foo] .Member[bar] |

javascript/ql/test/library-tests/frameworks/data/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,26 @@ testlib.bar.memberSink(source()); // NOT OK
209209
testlib.memberSink(source()); // OK
210210
testlib.overloadedSink('safe', source()); // OK
211211
testlib.overloadedSink('danger', source()); // NOT OK
212+
213+
function typeVars() {
214+
testlib.typevar.a.b().c.mySink(source()); // NOT OK
215+
216+
testlib.typevar.mySink(source()); // OK - does not match sub path
217+
testlib.typevar.a.mySink(source()); // OK - does not match sub path
218+
testlib.typevar.a.b.mySink(source()); // OK - does not match sub path
219+
testlib.typevar.a.b.c.mySink(source()); // OK - does not match sub path
220+
testlib.typevar.a.b(1).c.mySink(source()); // OK - does not match sub path
221+
222+
testlib.typevar.a.b().c.a.b().c.mySink(source(), 0); // OK
223+
testlib.typevar.a.b().c.a.b().c.mySink(0, source()); // NOT OK
224+
225+
testlib.typevar.left.x.right.mySink(source()); // NOT OK
226+
testlib.typevar.left.left.x.right.right.mySink(source()); // NOT OK
227+
testlib.typevar.left.x.right.right.mySink(source()); // OK - mismatched left and right
228+
testlib.typevar.left.left.x.right.mySink(source()); // OK - mismatched left and right
229+
230+
testlib.typevar.getThis().getThis().left.x.right.mySink(source()); // NOT OK
231+
testlib.typevar.left.getThis().getThis().x.right.mySink(source()); // NOT OK
232+
testlib.typevar.left.x.getThis().getThis().right.mySink(source()); // NOT OK
233+
testlib.typevar.left.x.right.getThis().getThis().mySink(source()); // NOT OK
234+
}

javascript/ql/test/library-tests/frameworks/data/test.ql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ class Steps extends ModelInput::SummaryModelCsv {
1414
"testlib;;Member[preserveAllButFirstArgument];Argument[1..];ReturnValue;taint",
1515
"testlib;;Member[preserveAllIfCall].Call;Argument[0..];ReturnValue;taint",
1616
"testlib;;Member[getSource].ReturnValue.Member[continue];Argument[this];ReturnValue;taint",
17+
"testlib;~HasThisFlow;;;Member[getThis].ReturnValue;type",
18+
]
19+
}
20+
}
21+
22+
class TypeDefs extends ModelInput::TypeModelCsv {
23+
override predicate row(string row) {
24+
row =
25+
[
26+
"testlib;~HasThisFlow;testlib;;Member[typevar]",
27+
"testlib;~HasThisFlow;testlib;~HasThisFlow;Member[left,right,x]",
1728
]
1829
}
1930
}
@@ -40,6 +51,20 @@ class Sinks extends ModelInput::SinkModelCsv {
4051
"testlib;;Member[ParamDecoratorSink].DecoratedParameter;test-sink",
4152
"testlib;;AnyMember.Member[memberSink].Argument[0];test-sink",
4253
"testlib;;Member[overloadedSink].WithStringArgument[0=danger].Argument[1];test-sink",
54+
"testlib;;Member[typevar].TypeVar[ABC].Member[mySink].Argument[0];test-sink",
55+
"testlib;;Member[typevar].TypeVar[ABC].TypeVar[ABC].Member[mySink].Argument[1];test-sink",
56+
"testlib;;Member[typevar].TypeVar[LeftRight].Member[mySink].Argument[0];test-sink",
57+
]
58+
}
59+
}
60+
61+
class TypeVars extends ModelInput::TypeVariableModelCsv {
62+
override predicate row(string row) {
63+
row =
64+
[
65+
"ABC;Member[a].Member[b].WithArity[0].ReturnValue.Member[c]", //
66+
"LeftRight;Member[left].TypeVar[LeftRight].Member[right]", //
67+
"LeftRight;Member[x]",
4368
]
4469
}
4570
}

0 commit comments

Comments
 (0)