Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2654e27

Browse files
author
Benjamin Muskalla
committed
Exclude known internal APIs from being modeled
1 parent 6b2460d commit 2654e27

5 files changed

Lines changed: 35 additions & 28 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class PropagateToSinkConfiguration extends TaintTracking::Configuration {
1717
override predicate isSource(DataFlow::Node source) {
1818
source instanceof DataFlow::ParameterNode and
1919
source.asParameter().getCallable().isPublic() and
20-
source.asParameter().getCallable().getDeclaringType().isPublic()
20+
source.asParameter().getCallable().getDeclaringType().isPublic() and
21+
isRelevantForModels(source.getEnclosingCallable())
2122
}
2223

23-
override predicate isSink(DataFlow::Node sink) { sinkNode(sink, _) }
24+
override predicate isSink(DataFlow::Node sink) { sinkNode(sink, _)}
2425
}
2526

2627
string asInputArgument(DataFlow::Node source) {
@@ -36,8 +37,7 @@ string captureSink(Callable api) {
3637
)
3738
}
3839

39-
from Callable api, string sink
40+
from TargetAPI api, string sink
4041
where
41-
sink = captureSink(api) and
42-
not isInTestFile(api)
42+
sink = captureSink(api)
4343
select sink order by sink

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ string captureSource(Callable api) {
4747
)
4848
}
4949

50-
from Callable api, string sink
50+
from TargetAPI api, string sink
5151
where
52-
sink = captureSource(api) and
53-
not isInTestFile(api)
52+
sink = captureSource(api)
5453
select sink order by sink

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ModelGeneratorUtils
99
import semmle.code.java.dataflow.TaintTracking
1010
import semmle.code.java.dataflow.internal.DataFlowImplCommon
1111
import semmle.code.java.dataflow.internal.DataFlowNodes
12+
import ModelGeneratorUtils
1213

1314
string captureFlow(Callable api) {
1415
result = captureQualifierFlow(api) or
@@ -123,15 +124,6 @@ string captureParameterToParameterFlow(Callable api) {
123124
// TODO: infer interface from multiple implementations? e.g. UriComponentsContributor
124125
// TODO: distinguish between taint and value flows. If we find a value flow, omit the taint flow
125126
// TODO: merge param->return value with param->parameter flow?
126-
class TargetAPI extends Callable {
127-
TargetAPI() {
128-
this.isPublic() and
129-
this.fromSource() and
130-
this.getDeclaringType().isPublic() and
131-
not isInTestFile(this)
132-
}
133-
}
134-
135127
from TargetAPI api, string flow
136128
where flow = captureFlow(api)
137129
select flow order by flow

java/ql/src/utils/model-generator/GenerateFlowModel.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#!/usr/bin/python3
22

3-
import errno
43
import json
54
import os
65
import os.path
7-
import re
86
import shlex
9-
import shutil
107
import subprocess
118
import sys
129
import tempfile

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,32 @@ import java
22
import semmle.code.java.dataflow.ExternalFlow
33
import semmle.code.java.dataflow.internal.ContainerFlow
44

5-
string isExtensible(RefType ref) { if ref.isFinal() then result = "false" else result = "true" }
5+
class TargetAPI extends Callable {
6+
TargetAPI() {
7+
this.isPublic() and
8+
this.fromSource() and
9+
this.getDeclaringType().isPublic() and
10+
isRelevantForModels(this)
11+
}
12+
13+
}
14+
15+
private string isExtensible(RefType ref) { if ref.isFinal() then result = "false" else result = "true" }
16+
17+
predicate isRelevantForModels(Callable api) {
18+
not isInTestFile(api.getCompilationUnit().getFile()) and
19+
not isJdkInternal(api.getCompilationUnit())
20+
}
21+
22+
private predicate isInTestFile(File file) {
23+
file.getAbsolutePath().matches("%src/test/%") or
24+
file.getAbsolutePath().matches("%/guava-tests/%") or
25+
file.getAbsolutePath().matches("%/guava-testlib/%")
26+
}
27+
28+
private predicate isJdkInternal(CompilationUnit cu) {
29+
cu.getPackage().getName().matches("com.sun") or cu.getPackage().getName().matches("sun") or cu.getPackage().getName().matches("")
30+
}
631

732
bindingset[input, output]
833
string asTaintModel(Callable api, string input, string output) {
@@ -56,10 +81,4 @@ string parameterAccess(Parameter p) {
5681
if p.getType() instanceof ContainerType
5782
then result = "Element of Argument[" + p.getPosition() + "]"
5883
else result = "Argument[" + p.getPosition() + "]"
59-
}
60-
61-
predicate isInTestFile(Callable api) {
62-
api.getCompilationUnit().getFile().getAbsolutePath().matches("%src/test/%") or
63-
api.getCompilationUnit().getFile().getAbsolutePath().matches("%/guava-tests/%") or
64-
api.getCompilationUnit().getFile().getAbsolutePath().matches("%/guava-testlib/%")
65-
}
84+
}

0 commit comments

Comments
 (0)