Skip to content

Commit 9500c9c

Browse files
author
Benjamin Muskalla
committed
Support lambda flow for source models
Also rely on public API to detect the source node
1 parent 35baa1c commit 9500c9c

4 files changed

Lines changed: 38 additions & 22 deletions

File tree

java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -889,17 +889,10 @@ module Private {
889889
* model.
890890
*/
891891
predicate isSourceNode(InterpretNode node, string kind) {
892-
exists(InterpretNode ref, string output | isSourceNode(ref, node, output, kind))
893-
}
894-
895-
// TODO: I wonder if this is actually the interface we want to expose.
896-
predicate isSourceNode(InterpretNode node, string output, string kind) {
897-
exists(InterpretNode ref | isSourceNode(ref, node, output, kind))
898-
}
899-
900-
predicate isSourceNode(InterpretNode ref, InterpretNode node, string output, string kind) {
901-
sourceElementRef(ref, output, kind) and
902-
interpretOutput(output, 0, ref, node)
892+
exists(InterpretNode ref, string output |
893+
sourceElementRef(ref, output, kind) and
894+
interpretOutput(output, 0, ref, node)
895+
)
903896
}
904897

905898
/**

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private import ModelGeneratorUtils
1313
private import semmle.code.java.dataflow.internal.FlowSummaryImplSpecific
1414
private import semmle.code.java.dataflow.internal.FlowSummaryImpl
1515
private import semmle.code.java.dataflow.internal.DataFlowImplCommon
16+
import semmle.code.java.dataflow.internal.DataFlowNodes::Private
1617

1718
class FromSourceConfiguration extends TaintTracking::Configuration {
1819
FromSourceConfiguration() { this = "FromSourceConfiguration" }
@@ -26,24 +27,33 @@ class FromSourceConfiguration extends TaintTracking::Configuration {
2627
c.isPublic() and
2728
c.fromSource()
2829
)
30+
or
31+
exists(MethodAccess c | sink.asExpr() = c.getAnArgument())
2932
}
3033
}
3134

32-
// TODO: better way than rely on internals to capture kind?
33-
cached
34-
predicate specificSourceNode(DataFlow::Node node, string output, string kind) {
35-
exists(InterpretNode n | Private::External::isSourceNode(n, output, kind) and n.asNode() = node)
35+
string asOutput(DataFlow::Node node) {
36+
if node instanceof ReturnNodeExt
37+
then result = "ReturnValue"
38+
else
39+
result =
40+
"Parameter[" +
41+
node.(ArgumentNode)
42+
.getCall()
43+
.asCall()
44+
.getQualifier()
45+
.(VarAccess)
46+
.getVariable()
47+
.(Parameter)
48+
.getPosition() + "]"
3649
}
3750

3851
string captureSource(Callable api) {
39-
exists(
40-
DataFlow::Node src, DataFlow::Node sink, FromSourceConfiguration config, string kind,
41-
string output
42-
|
52+
exists(DataFlow::Node src, DataFlow::Node sink, FromSourceConfiguration config, string kind |
4353
config.hasFlow(src, sink) and
44-
specificSourceNode(sink, output, kind) and
54+
sourceNode(sink, kind) and
4555
api = src.getEnclosingCallable() and
46-
result = asSourceModel(api, output, kind)
56+
result = asSourceModel(api, asOutput(sink), kind)
4757
)
4858
}
4959

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
| p;Sources;true;readUrl;(URL);;ReturnValue;remote; |
1+
| p;Sources;true;consumeSource;(int,Consumer);;Parameter[1];remote; |
2+
| p;Sources;true;readUrl;(URL);;ReturnValue;remote; |
3+
| p;Sources;true;socketStream;();;ReturnValue;remote; |

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.net.ServerSocket;
56
import java.net.URL;
7+
import java.util.function.Consumer;
68

79

810
public class Sources {
@@ -11,4 +13,13 @@ public InputStream readUrl(final URL url) throws IOException {
1113
return url.openConnection().getInputStream();
1214
}
1315

16+
public InputStream socketStream() throws IOException {
17+
ServerSocket socket = new ServerSocket(123);
18+
return socket.accept().getInputStream();
19+
}
20+
21+
public void consumeSource(int port, Consumer<InputStream> consumer) throws IOException {
22+
ServerSocket socket = new ServerSocket(port);
23+
consumer.accept(socket.accept().getInputStream());
24+
}
1425
}

0 commit comments

Comments
 (0)