Skip to content

Commit 0e9fcc6

Browse files
author
Benjamin Muskalla
committed
Only generate models for local supertypes
Avoid generating models for classes implementing external SPI (e.g. `FileFilter`). Keep `toString` models intact as they're commonly used as taint-propagation method (e.g. see `Joiner`).
1 parent 157f56f commit 0e9fcc6

6 files changed

Lines changed: 51 additions & 11 deletions

File tree

java/ql/src/utils/model-generator/CaptureSourceModels.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class FromSourceConfiguration extends TaintTracking::Configuration {
2929
}
3030
}
3131

32-
// TODO: better way than rely on internals?
32+
// TODO: better way than rely on internals to capture kind?
3333
cached
3434
predicate specificSourceNode(DataFlow::Node node, string output, string kind) {
3535
exists(InterpretNode n | Private::External::isSourceNode(n, output, kind) and n.asNode() = node)

java/ql/src/utils/model-generator/CaptureSummaryModels.ql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ predicate isRelevantType(Type t) {
130130
not t.(CollectionType).getElementType() instanceof BoxedType
131131
}
132132

133-
// TODO: "com.google.common.base;Converter;true;convertAll;(Iterable);;Element of Argument[0];Element of ReturnValue;taint",
134-
// TODO: infer interface from multiple implementations? e.g. UriComponentsContributor
135-
// TODO: distinguish between taint and value flows. If we find a value flow, omit the taint flow
136-
// TODO: merge param->return value with param->parameter flow?
137133
from TargetAPI api, string flow
138134
where flow = captureFlow(api)
139135
select flow order by flow

java/ql/src/utils/model-generator/ModelGeneratorUtils.qll

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ string asSourceModel(Callable api, string output, string kind) {
6969
*/
7070
private string asPartialModel(Callable api) {
7171
result =
72-
asModelName(api) + ";" //
72+
typeAsSummaryModel(api) + ";" //
7373
+ isExtensible(api.getDeclaringType()).toString() + ";" //
7474
+ api.getName() + ";" //
7575
+ paramsString(api) + ";" //
@@ -80,12 +80,20 @@ private string asPartialModel(Callable api) {
8080
* Returns the appropriate type name for the model. Either the type
8181
* declaring the method or the supertype introducing the method.
8282
*/
83-
private string asModelName(Callable api) {
84-
if api.(Method).getASourceOverriddenMethod().fromSource()
85-
then result = typeAsModel(api.(Method).getASourceOverriddenMethod().getDeclaringType())
83+
private string typeAsSummaryModel(Callable api) {
84+
if exists(superImpl(api.(Method)))
85+
then
86+
superImpl(api.(Method)).fromSource() and
87+
result = typeAsModel(superImpl(api.(Method)).getDeclaringType())
8688
else result = typeAsModel(api.getDeclaringType())
8789
}
8890

91+
Method superImpl(Method m) {
92+
result = m.getAnOverride() and
93+
not exists(result.getAnOverride()) and
94+
not m instanceof ToStringMethod
95+
}
96+
8997
private string typeAsModel(RefType type) {
9098
result = type.getCompilationUnit().getPackage().getName() + ";" + type.nestedName()
9199
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package p;
2+
3+
import java.io.File;
4+
import java.io.FileFilter;
5+
6+
public abstract class AbstractImplOfExternalSPI implements FileFilter {
7+
8+
@Override
9+
public boolean accept(File pathname) {
10+
return false;
11+
}
12+
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package p;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
7+
public class ImplOfExternalSPI extends AbstractImplOfExternalSPI {
8+
9+
@Override
10+
public boolean accept(File pathname) {
11+
try {
12+
Files.createFile(pathname.toPath());
13+
} catch (IOException e) {
14+
e.printStackTrace();
15+
}
16+
return false;
17+
}
18+
19+
}

java/ql/test/utils/model-generator/p/MultipleImpls.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package p;
22

3+
import java.io.File;
4+
import java.io.FileFilter;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
37
import java.util.concurrent.Callable;
48

59
public class MultipleImpls {
6-
10+
711
public static interface Strategy {
812
String doSomething(String value);
913
}
@@ -22,7 +26,7 @@ public String call() throws Exception {
2226
return null;
2327
}
2428

25-
}
29+
}
2630
public static class Strat2 implements Strategy {
2731
private String foo;
2832

0 commit comments

Comments
 (0)