/
FindFlowBetweenMethods.java
141 lines (118 loc) · 5.48 KB
/
FindFlowBetweenMethods.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2020 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.analysis.search;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.analysis.InvocationMatcher;
import org.openrewrite.analysis.dataflow.DataFlowNode;
import org.openrewrite.analysis.dataflow.DataFlowSpec;
import org.openrewrite.analysis.dataflow.TaintFlowSpec;
import org.openrewrite.analysis.dataflow.global.GlobalDataFlow;
import org.openrewrite.analysis.trait.expr.Call;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.MethodMatcher;
import java.util.function.Predicate;
/**
* Finds either Taint or Data flow between specified start and end methods.
*/
@Value
@EqualsAndHashCode(callSuper = false)
public class FindFlowBetweenMethods extends ScanningRecipe<GlobalDataFlow.Accumulator> {
/**
* A method pattern that is used to find matching method invocations.
* See {@link MethodMatcher} for details on the expression's syntax.
*/
@Option(displayName = "Start method pattern", description = "A method pattern that is used to find matching the start point's method invocations.", example = "java.util.List add(..)")
String startMethodPattern;
@Option(displayName = "Match start method on overrides", description = "When enabled, find methods that are overrides of the method pattern.", required = false)
@Nullable Boolean startMatchOverrides;
@Option(displayName = "End method pattern", description = "A method pattern that is used to find matching the end point's method invocations.", example = "java.util.List add(..)")
String endMethodPattern;
@Option(displayName = "Match end method on overrides", description = "When enabled, find methods that are overrides of the method pattern.", required = false)
@Nullable Boolean endMatchOverrides;
@Option(displayName = "To target", description = "The part of the method flow should traverse to", required = true, valid = {"Select", "Arguments", "Both"})
String target;
@Option(displayName = "Show flow", description = "When enabled, show the data or taint flow of the method invocation.", valid = {"Data", "Taint"}, required = true)
@Nullable String flow;
@Override
public String getDisplayName() {
return "Finds flow between two methods";
}
@Override
public String getDescription() {
return "Takes two patterns for the start/end methods to find flow between.";
}
@Override
public GlobalDataFlow.Accumulator getInitialValue(ExecutionContext ctx) {
InvocationMatcher startMatcher = InvocationMatcher.fromMethodMatcher(startMethodPattern, startMatchOverrides);
InvocationMatcher endMatcher = InvocationMatcher.fromMethodMatcher(endMethodPattern, endMatchOverrides);
InvocationMatcher.AdvancedInvocationMatcher endAdvanced = endMatcher.advanced();
final Predicate<Cursor> sinkMatcher;
switch (target) {
case "Select":
sinkMatcher = endAdvanced::isSelect;
break;
case "Arguments":
sinkMatcher = endAdvanced::isAnyArgument;
break;
case "Both":
sinkMatcher = cursor -> endAdvanced.isAnyArgument(cursor) ||
endAdvanced.isSelect(cursor);
break;
default:
throw new IllegalStateException("Unknown target: " + target);
}
String flow = this.flow == null ? "Data" : this.flow;
if ("Taint".equals(flow)) {
return GlobalDataFlow.accumulator(new TaintFlowSpec() {
@Override
public boolean isSource(DataFlowNode srcNode) {
return FindFlowBetweenMethods.isSource(srcNode, startMatcher);
}
@Override
public boolean isSink(DataFlowNode sinkNode) {
return sinkMatcher.test(sinkNode.getCursor());
}
});
}
return GlobalDataFlow.accumulator(new DataFlowSpec() {
@Override
public boolean isSource(DataFlowNode srcNode) {
return FindFlowBetweenMethods.isSource(srcNode, startMatcher);
}
@Override
public boolean isSink(DataFlowNode sinkNode) {
return sinkMatcher.test(sinkNode.getCursor());
}
});
}
private static boolean isSource(DataFlowNode srcNode, InvocationMatcher startMatcher) {
return srcNode
.asExprParent(Call.class)
.bind(Call::getMethodType)
.filter(startMatcher::matches)
.isSome();
}
@Override
public TreeVisitor<?, ExecutionContext> getScanner(GlobalDataFlow.Accumulator acc) {
return acc.scanner();
}
@Override
public TreeVisitor<?, ExecutionContext> getVisitor(GlobalDataFlow.Accumulator acc) {
return acc.renderer();
}
}