Skip to content

Commit a8799fe

Browse files
author
Stephan Brandauer
committed
Java: share getCallable interface between automodel extraction modes
1 parent 92ad02a commit a8799fe

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

java/ql/src/Telemetry/AutomodelApplicationModeCharacteristics.qll

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private import semmle.code.java.security.RequestForgery
1616
private import semmle.code.java.dataflow.internal.ModelExclusions as ModelExclusions
1717
private import AutomodelSharedUtil as AutomodelSharedUtil
1818
private import semmle.code.java.security.PathSanitizer as PathSanitizer
19+
private import AutomodelSharedGetCallable as AutomodelSharedGetCallable
1920
import AutomodelSharedCharacteristics as SharedCharacteristics
2021
import AutomodelEndpointTypes as AutomodelEndpointTypes
2122

@@ -85,8 +86,8 @@ module ApplicationCandidatesImpl implements SharedCharacteristics::CandidateSig
8586
additional predicate sinkSpec(
8687
Endpoint e, string package, string type, string name, string signature, string ext, string input
8788
) {
88-
ApplicationCandidatesImpl::getCallable(e).hasQualifiedName(package, type, name) and
89-
signature = ExternalFlow::paramsString(getCallable(e)) and
89+
ApplicationModeGetCallable::getCallable(e).hasQualifiedName(package, type, name) and
90+
signature = ExternalFlow::paramsString(ApplicationModeGetCallable::getCallable(e)) and
9091
ext = "" and
9192
(
9293
exists(Call c, int argIdx |
@@ -110,13 +111,19 @@ module ApplicationCandidatesImpl implements SharedCharacteristics::CandidateSig
110111
type = CallContext() and
111112
result = any(Call c | e.asExpr() = [c.getAnArgument(), c.getQualifier()])
112113
}
114+
}
115+
116+
private class JavaCallable = Callable;
117+
118+
private module ApplicationModeGetCallable implements AutomodelSharedGetCallable::GetCallableSig {
119+
class Callable = JavaCallable;
120+
121+
class Endpoint = ApplicationCandidatesImpl::Endpoint;
113122

114123
/**
115124
* Returns the API callable being modeled.
116-
*
117-
* Each Java mode should implement this predicate.
118125
*/
119-
additional Callable getCallable(Endpoint e) {
126+
Callable getCallable(Endpoint e) {
120127
exists(Call c |
121128
e.asExpr() = [c.getAnArgument(), c.getQualifier()] and
122129
result = c.getCallee()
@@ -209,8 +216,8 @@ private class UnexploitableIsCharacteristic extends CharacteristicsImpl::NotASin
209216

210217
override predicate appliesToEndpoint(Endpoint e) {
211218
not ApplicationCandidatesImpl::isSink(e, _) and
212-
ApplicationCandidatesImpl::getCallable(e).getName().matches("is%") and
213-
ApplicationCandidatesImpl::getCallable(e).getReturnType() instanceof BooleanType
219+
ApplicationModeGetCallable::getCallable(e).getName().matches("is%") and
220+
ApplicationModeGetCallable::getCallable(e).getReturnType() instanceof BooleanType
214221
}
215222
}
216223

@@ -228,7 +235,7 @@ private class UnexploitableExistsCharacteristic extends CharacteristicsImpl::Not
228235
override predicate appliesToEndpoint(Endpoint e) {
229236
not ApplicationCandidatesImpl::isSink(e, _) and
230237
exists(Callable callable |
231-
callable = ApplicationCandidatesImpl::getCallable(e) and
238+
callable = ApplicationModeGetCallable::getCallable(e) and
232239
callable.getName().toLowerCase() = ["exists", "notexists"] and
233240
callable.getReturnType() instanceof BooleanType
234241
)
@@ -242,7 +249,7 @@ private class ExceptionCharacteristic extends CharacteristicsImpl::NotASinkChara
242249
ExceptionCharacteristic() { this = "exception" }
243250

244251
override predicate appliesToEndpoint(Endpoint e) {
245-
ApplicationCandidatesImpl::getCallable(e).getDeclaringType().getASupertype*() instanceof
252+
ApplicationModeGetCallable::getCallable(e).getDeclaringType().getASupertype*() instanceof
246253
TypeThrowable
247254
}
248255
}
@@ -291,7 +298,7 @@ private class ArgumentToLocalCall extends CharacteristicsImpl::UninterestingToMo
291298
ArgumentToLocalCall() { this = "argument to local call" }
292299

293300
override predicate appliesToEndpoint(Endpoint e) {
294-
ApplicationCandidatesImpl::getCallable(e).fromSource()
301+
ApplicationModeGetCallable::getCallable(e).fromSource()
295302
}
296303
}
297304

@@ -302,7 +309,7 @@ private class ExcludedFromModeling extends CharacteristicsImpl::UninterestingToM
302309
ExcludedFromModeling() { this = "excluded from modeling" }
303310

304311
override predicate appliesToEndpoint(Endpoint e) {
305-
ModelExclusions::isUninterestingForModels(ApplicationCandidatesImpl::getCallable(e)) or
312+
ModelExclusions::isUninterestingForModels(ApplicationModeGetCallable::getCallable(e)) or
306313
ModelExclusions::isUninterestingForModels(e.getEnclosingCallable())
307314
}
308315
}
@@ -316,7 +323,7 @@ private class NonPublicMethodCharacteristic extends CharacteristicsImpl::Uninter
316323
NonPublicMethodCharacteristic() { this = "non-public method" }
317324

318325
override predicate appliesToEndpoint(Endpoint e) {
319-
not ApplicationCandidatesImpl::getCallable(e).isPublic()
326+
not ApplicationModeGetCallable::getCallable(e).isPublic()
320327
}
321328
}
322329

java/ql/src/Telemetry/AutomodelFrameworkModeCharacteristics.qll

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ private import semmle.code.java.security.QueryInjection
1515
private import semmle.code.java.security.RequestForgery
1616
private import semmle.code.java.dataflow.internal.ModelExclusions as ModelExclusions
1717
private import AutomodelSharedUtil as AutomodelSharedUtil
18+
private import AutomodelSharedGetCallable as AutomodelSharedGetCallable
1819
import AutomodelSharedCharacteristics as SharedCharacteristics
1920
import AutomodelEndpointTypes as AutomodelEndpointTypes
2021

@@ -66,8 +67,8 @@ module FrameworkCandidatesImpl implements SharedCharacteristics::CandidateSig {
6667
additional predicate sinkSpec(
6768
Endpoint e, string package, string type, string name, string signature, string ext, string input
6869
) {
69-
FrameworkCandidatesImpl::getCallable(e).hasQualifiedName(package, type, name) and
70-
signature = ExternalFlow::paramsString(getCallable(e)) and
70+
FrameworkModeGetCallable::getCallable(e).hasQualifiedName(package, type, name) and
71+
signature = ExternalFlow::paramsString(FrameworkModeGetCallable::getCallable(e)) and
7172
ext = "" and
7273
exists(int paramIdx | e.isParameterOf(_, paramIdx) |
7374
input = AutomodelSharedUtil::getArgumentForIndex(paramIdx)
@@ -81,18 +82,26 @@ module FrameworkCandidatesImpl implements SharedCharacteristics::CandidateSig {
8182
*/
8283
RelatedLocation getRelatedLocation(Endpoint e, RelatedLocationType type) {
8384
type = MethodDoc() and
84-
result = FrameworkCandidatesImpl::getCallable(e).(Documentable).getJavadoc()
85+
result = FrameworkModeGetCallable::getCallable(e).(Documentable).getJavadoc()
8586
or
8687
type = ClassDoc() and
87-
result = FrameworkCandidatesImpl::getCallable(e).getDeclaringType().(Documentable).getJavadoc()
88+
result = FrameworkModeGetCallable::getCallable(e).getDeclaringType().(Documentable).getJavadoc()
8889
}
90+
}
91+
92+
private class JavaCallable = Callable;
93+
94+
private module FrameworkModeGetCallable implements AutomodelSharedGetCallable::GetCallableSig {
95+
class Callable = JavaCallable;
96+
97+
class Endpoint = FrameworkCandidatesImpl::Endpoint;
8998

9099
/**
91100
* Returns the callable that contains the given endpoint.
92101
*
93102
* Each Java mode should implement this predicate.
94103
*/
95-
additional Callable getCallable(Endpoint e) { result = e.getEnclosingCallable() }
104+
Callable getCallable(Endpoint e) { result = e.getEnclosingCallable() }
96105
}
97106

98107
module CharacteristicsImpl = SharedCharacteristics::SharedCharacteristics<FrameworkCandidatesImpl>;
@@ -163,8 +172,8 @@ private class UnexploitableIsCharacteristic extends CharacteristicsImpl::NotASin
163172

164173
override predicate appliesToEndpoint(Endpoint e) {
165174
not FrameworkCandidatesImpl::isSink(e, _) and
166-
FrameworkCandidatesImpl::getCallable(e).getName().matches("is%") and
167-
FrameworkCandidatesImpl::getCallable(e).getReturnType() instanceof BooleanType
175+
FrameworkModeGetCallable::getCallable(e).getName().matches("is%") and
176+
FrameworkModeGetCallable::getCallable(e).getReturnType() instanceof BooleanType
168177
}
169178
}
170179

@@ -182,7 +191,7 @@ private class UnexploitableExistsCharacteristic extends CharacteristicsImpl::Not
182191
override predicate appliesToEndpoint(Endpoint e) {
183192
not FrameworkCandidatesImpl::isSink(e, _) and
184193
exists(Callable callable |
185-
callable = FrameworkCandidatesImpl::getCallable(e) and
194+
callable = FrameworkModeGetCallable::getCallable(e) and
186195
callable.getName().toLowerCase() = ["exists", "notexists"] and
187196
callable.getReturnType() instanceof BooleanType
188197
)
@@ -196,7 +205,7 @@ private class ExceptionCharacteristic extends CharacteristicsImpl::NotASinkChara
196205
ExceptionCharacteristic() { this = "exception" }
197206

198207
override predicate appliesToEndpoint(Endpoint e) {
199-
FrameworkCandidatesImpl::getCallable(e).getDeclaringType().getASupertype*() instanceof
208+
FrameworkModeGetCallable::getCallable(e).getDeclaringType().getASupertype*() instanceof
200209
TypeThrowable
201210
}
202211
}
@@ -222,7 +231,7 @@ private class NonPublicMethodCharacteristic extends CharacteristicsImpl::Uninter
222231
NonPublicMethodCharacteristic() { this = "non-public method" }
223232

224233
override predicate appliesToEndpoint(Endpoint e) {
225-
not FrameworkCandidatesImpl::getCallable(e).isPublic()
234+
not FrameworkModeGetCallable::getCallable(e).isPublic()
226235
}
227236
}
228237

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* An automodel extraction mode instantiates this interface to define how to access
3+
* the callable that's associated with an endpoint.
4+
*/
5+
signature module GetCallableSig {
6+
/**
7+
* A callable is the definition of a method, function, etc. - something that can be called.
8+
*/
9+
class Callable;
10+
11+
/**
12+
* An endpoint is a potential candidate for modeling. This will typically be bound to the language's
13+
* DataFlow node class, or a subtype thereof.
14+
*/
15+
class Endpoint;
16+
17+
/**
18+
* Gets the callable that's associated with the given endpoint.
19+
*/
20+
Callable getCallable(Endpoint endpoint);
21+
}

0 commit comments

Comments
 (0)