Skip to content

Commit 715b135

Browse files
author
Stephan Brandauer
committed
Java: share considerSubtypes predicate between Java modes
1 parent 7e77e2e commit 715b135

8 files changed

+34
-50
lines changed

java/ql/src/Telemetry/AutomodelApplicationModeCharacteristics.qll

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private import semmle.code.java.Expr as Expr
1414
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
17-
private import AutomodelSharedUtil as AutomodelSharedUtil
17+
private import AutomodelJavaUtil as AutomodelJavaUtil
1818
private import semmle.code.java.security.PathSanitizer as PathSanitizer
1919
private import AutomodelSharedGetCallable as AutomodelSharedGetCallable
2020
import AutomodelSharedCharacteristics as SharedCharacteristics
@@ -65,7 +65,7 @@ module ApplicationCandidatesImpl implements SharedCharacteristics::CandidateSig
6565

6666
RelatedLocation asLocation(Endpoint e) { result = e.asExpr() }
6767

68-
predicate isKnownKind = AutomodelSharedUtil::isKnownKind/3;
68+
predicate isKnownKind = AutomodelJavaUtil::isKnownKind/3;
6969

7070
predicate isSink(Endpoint e, string kind) {
7171
exists(string package, string type, string name, string signature, string ext, string input |
@@ -92,11 +92,11 @@ module ApplicationCandidatesImpl implements SharedCharacteristics::CandidateSig
9292
(
9393
exists(Call c, int argIdx |
9494
e.asExpr() = c.getArgument(argIdx) and
95-
input = AutomodelSharedUtil::getArgumentForIndex(argIdx)
95+
input = AutomodelJavaUtil::getArgumentForIndex(argIdx)
9696
)
9797
or
9898
exists(Call c |
99-
e.asExpr() = c.getQualifier() and input = AutomodelSharedUtil::getArgumentForIndex(-1)
99+
e.asExpr() = c.getQualifier() and input = AutomodelJavaUtil::getArgumentForIndex(-1)
100100
)
101101
)
102102
}
@@ -160,23 +160,6 @@ class Endpoint = ApplicationCandidatesImpl::Endpoint;
160160
class ApplicationModeMetadataExtractor extends string {
161161
ApplicationModeMetadataExtractor() { this = "ApplicationModeMetadataExtractor" }
162162

163-
/**
164-
* By convention, the subtypes property of the MaD declaration should only be
165-
* true when there _can_ exist any subtypes with a different implementation.
166-
*
167-
* It would technically be ok to always use the value 'true', but this would
168-
* break convention.
169-
*/
170-
boolean considerSubtypes(Callable callable) {
171-
if
172-
callable.isStatic() or
173-
callable.getDeclaringType().isStatic() or
174-
callable.isFinal() or
175-
callable.getDeclaringType().isFinal()
176-
then result = false
177-
else result = true
178-
}
179-
180163
predicate hasMetadata(
181164
Endpoint e, string package, string type, string subtypes, string name, string signature,
182165
string input
@@ -188,12 +171,12 @@ class ApplicationModeMetadataExtractor extends string {
188171
or
189172
e.asExpr() = call.getQualifier() and argIdx = -1
190173
) and
191-
input = AutomodelSharedUtil::getArgumentForIndex(argIdx) and
174+
input = AutomodelJavaUtil::getArgumentForIndex(argIdx) and
192175
package = callable.getDeclaringType().getPackage().getName() and
193176
// we're using the erased types because the MaD convention is to not specify type parameters.
194177
// Whether something is or isn't a sink doesn't usually depend on the type parameters.
195178
type = callable.getDeclaringType().getErasure().(RefType).nestedName() and
196-
subtypes = this.considerSubtypes(callable).toString() and
179+
subtypes = AutomodelJavaUtil::considerSubtypes(callable).toString() and
197180
name = callable.getName() and
198181
signature = ExternalFlow::paramsString(callable)
199182
)

java/ql/src/Telemetry/AutomodelApplicationModeExtractNegativeExamples.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
private import java
1212
private import AutomodelApplicationModeCharacteristics
1313
private import AutomodelEndpointTypes
14-
private import AutomodelSharedUtil
14+
private import AutomodelJavaUtil
1515

1616
/**
1717
* Gets a sample of endpoints (of at most `limit` samples) for which the given characteristic applies.

java/ql/src/Telemetry/AutomodelApplicationModeExtractPositiveExamples.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
private import AutomodelApplicationModeCharacteristics
1212
private import AutomodelEndpointTypes
13-
private import AutomodelSharedUtil
13+
private import AutomodelJavaUtil
1414

1515
from
1616
Endpoint endpoint, SinkType sinkType, ApplicationModeMetadataExtractor meta,

java/ql/src/Telemetry/AutomodelFrameworkModeCharacteristics.qll

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private import semmle.code.java.Expr as Expr
1414
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
17-
private import AutomodelSharedUtil as AutomodelSharedUtil
17+
private import AutomodelJavaUtil as AutomodelJavaUtil
1818
private import AutomodelSharedGetCallable as AutomodelSharedGetCallable
1919
import AutomodelSharedCharacteristics as SharedCharacteristics
2020
import AutomodelEndpointTypes as AutomodelEndpointTypes
@@ -48,7 +48,7 @@ module FrameworkCandidatesImpl implements SharedCharacteristics::CandidateSig {
4848

4949
RelatedLocation asLocation(Endpoint e) { result = e.asParameter() }
5050

51-
predicate isKnownKind = AutomodelSharedUtil::isKnownKind/3;
51+
predicate isKnownKind = AutomodelJavaUtil::isKnownKind/3;
5252

5353
predicate isSink(Endpoint e, string kind) {
5454
exists(string package, string type, string name, string signature, string ext, string input |
@@ -71,7 +71,7 @@ module FrameworkCandidatesImpl implements SharedCharacteristics::CandidateSig {
7171
signature = ExternalFlow::paramsString(FrameworkModeGetCallable::getCallable(e)) and
7272
ext = "" and
7373
exists(int paramIdx | e.isParameterOf(_, paramIdx) |
74-
input = AutomodelSharedUtil::getArgumentForIndex(paramIdx)
74+
input = AutomodelJavaUtil::getArgumentForIndex(paramIdx)
7575
)
7676
}
7777

@@ -120,33 +120,16 @@ class Endpoint = FrameworkCandidatesImpl::Endpoint;
120120
class FrameworkModeMetadataExtractor extends string {
121121
FrameworkModeMetadataExtractor() { this = "FrameworkModeMetadataExtractor" }
122122

123-
/**
124-
* By convention, the subtypes property of the MaD declaration should only be
125-
* true when there _can_ exist any subtypes with a different implementation.
126-
*
127-
* It would technically be ok to always use the value 'true', but this would
128-
* break convention.
129-
*/
130-
boolean considerSubtypes(Callable callable) {
131-
if
132-
callable.isStatic() or
133-
callable.getDeclaringType().isStatic() or
134-
callable.isFinal() or
135-
callable.getDeclaringType().isFinal()
136-
then result = false
137-
else result = true
138-
}
139-
140123
predicate hasMetadata(
141124
Endpoint e, string package, string type, string subtypes, string name, string signature,
142125
string input, string parameterName
143126
) {
144127
exists(Callable callable, int paramIdx |
145128
e.asParameter() = callable.getParameter(paramIdx) and
146-
input = AutomodelSharedUtil::getArgumentForIndex(paramIdx) and
129+
input = AutomodelJavaUtil::getArgumentForIndex(paramIdx) and
147130
package = callable.getDeclaringType().getPackage().getName() and
148131
type = callable.getDeclaringType().getErasure().(RefType).nestedName() and
149-
subtypes = this.considerSubtypes(callable).toString() and
132+
subtypes = AutomodelJavaUtil::considerSubtypes(callable).toString() and
150133
name = callable.getName() and
151134
parameterName = e.asParameter().getName() and
152135
signature = ExternalFlow::paramsString(callable)

java/ql/src/Telemetry/AutomodelFrameworkModeExtractCandidates.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414

1515
private import AutomodelFrameworkModeCharacteristics
16-
private import AutomodelSharedUtil
16+
private import AutomodelJavaUtil
1717

1818
from
1919
Endpoint endpoint, string message, FrameworkModeMetadataExtractor meta, DollarAtString package,

java/ql/src/Telemetry/AutomodelFrameworkModeExtractNegativeExamples.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
private import AutomodelFrameworkModeCharacteristics
1212
private import AutomodelEndpointTypes
13-
private import AutomodelSharedUtil
13+
private import AutomodelJavaUtil
1414

1515
from
1616
Endpoint endpoint, EndpointCharacteristic characteristic, float confidence,

java/ql/src/Telemetry/AutomodelFrameworkModeExtractPositiveExamples.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
private import AutomodelFrameworkModeCharacteristics
1212
private import AutomodelEndpointTypes
13-
private import AutomodelSharedUtil
13+
private import AutomodelJavaUtil
1414

1515
from
1616
Endpoint endpoint, SinkType sinkType, FrameworkModeMetadataExtractor meta, DollarAtString package,

java/ql/src/Telemetry/AutomodelSharedUtil.qll renamed to java/ql/src/Telemetry/AutomodelJavaUtil.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
private import java
12
private import AutomodelEndpointTypes as AutomodelEndpointTypes
23

34
/**
@@ -61,3 +62,20 @@ string getArgumentForIndex(int index) {
6162
or
6263
index >= 0 and result = "Argument[" + index + "]"
6364
}
65+
66+
/**
67+
* By convention, the subtypes property of the MaD declaration should only be
68+
* true when there _can_ exist any subtypes with a different implementation.
69+
*
70+
* It would technically be ok to always use the value 'true', but this would
71+
* break convention.
72+
*/
73+
boolean considerSubtypes(Callable callable) {
74+
if
75+
callable.isStatic() or
76+
callable.getDeclaringType().isStatic() or
77+
callable.isFinal() or
78+
callable.getDeclaringType().isFinal()
79+
then result = false
80+
else result = true
81+
}

0 commit comments

Comments
 (0)