@@ -14,23 +14,11 @@ private import semmle.code.java.Expr as Expr
1414private import semmle.code.java.security.QueryInjection
1515private import semmle.code.java.security.RequestForgery
1616private import semmle.code.java.dataflow.internal.ModelExclusions as ModelExclusions
17+ private import AutomodelJavaUtil as AutomodelJavaUtil
18+ private import AutomodelSharedGetCallable as AutomodelSharedGetCallable
1719import AutomodelSharedCharacteristics as SharedCharacteristics
1820import AutomodelEndpointTypes as AutomodelEndpointTypes
1921
20- /**
21- * A meta data extractor. Any Java extraction mode needs to implement exactly
22- * one instance of this class.
23- */
24- abstract class MetadataExtractor extends string {
25- bindingset [ this ]
26- MetadataExtractor ( ) { any ( ) }
27-
28- abstract predicate hasMetadata (
29- DataFlow:: ParameterNode e , string package , string type , boolean subtypes , string name ,
30- string signature , int input , string parameterName
31- ) ;
32- }
33-
3422newtype JavaRelatedLocationType =
3523 MethodDoc ( ) or
3624 ClassDoc ( )
@@ -60,31 +48,7 @@ module FrameworkCandidatesImpl implements SharedCharacteristics::CandidateSig {
6048
6149 RelatedLocation asLocation ( Endpoint e ) { result = e .asParameter ( ) }
6250
63- predicate isKnownKind ( string kind , string humanReadableKind , EndpointType type ) {
64- kind = "read-file" and
65- humanReadableKind = "read file" and
66- type instanceof AutomodelEndpointTypes:: TaintedPathSinkType
67- or
68- kind = "create-file" and
69- humanReadableKind = "create file" and
70- type instanceof AutomodelEndpointTypes:: TaintedPathSinkType
71- or
72- kind = "sql" and
73- humanReadableKind = "mad modeled sql" and
74- type instanceof AutomodelEndpointTypes:: SqlSinkType
75- or
76- kind = "open-url" and
77- humanReadableKind = "open url" and
78- type instanceof AutomodelEndpointTypes:: RequestForgerySinkType
79- or
80- kind = "jdbc-url" and
81- humanReadableKind = "jdbc url" and
82- type instanceof AutomodelEndpointTypes:: RequestForgerySinkType
83- or
84- kind = "command-injection" and
85- humanReadableKind = "command injection" and
86- type instanceof AutomodelEndpointTypes:: CommandInjectionSinkType
87- }
51+ predicate isKnownKind = AutomodelJavaUtil:: isKnownKind / 3 ;
8852
8953 predicate isSink ( Endpoint e , string kind ) {
9054 exists ( string package , string type , string name , string signature , string ext , string input |
@@ -103,33 +67,41 @@ module FrameworkCandidatesImpl implements SharedCharacteristics::CandidateSig {
10367 additional predicate sinkSpec (
10468 Endpoint e , string package , string type , string name , string signature , string ext , string input
10569 ) {
106- FrameworkCandidatesImpl :: getCallable ( e ) .hasQualifiedName ( package , type , name ) and
107- signature = ExternalFlow:: paramsString ( getCallable ( e ) ) and
70+ FrameworkModeGetCallable :: getCallable ( e ) .hasQualifiedName ( package , type , name ) and
71+ signature = ExternalFlow:: paramsString ( FrameworkModeGetCallable :: getCallable ( e ) ) and
10872 ext = "" and
10973 exists ( int paramIdx | e .isParameterOf ( _, paramIdx ) |
110- if paramIdx = - 1 then input = "Argument[this]" else input = "Argument[" + paramIdx + "]"
74+ input = AutomodelJavaUtil :: getArgumentForIndex ( paramIdx )
11175 )
11276 }
11377
11478 /**
115- * Returns the related location for the given endpoint.
79+ * Gets the related location for the given endpoint.
11680 *
11781 * Related locations can be JavaDoc comments of the class or the method.
11882 */
11983 RelatedLocation getRelatedLocation ( Endpoint e , RelatedLocationType type ) {
12084 type = MethodDoc ( ) and
121- result = FrameworkCandidatesImpl :: getCallable ( e ) .( Documentable ) .getJavadoc ( )
85+ result = FrameworkModeGetCallable :: getCallable ( e ) .( Documentable ) .getJavadoc ( )
12286 or
12387 type = ClassDoc ( ) and
124- result = FrameworkCandidatesImpl :: getCallable ( e ) .getDeclaringType ( ) .( Documentable ) .getJavadoc ( )
88+ result = FrameworkModeGetCallable :: getCallable ( e ) .getDeclaringType ( ) .( Documentable ) .getJavadoc ( )
12589 }
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 ;
12698
12799 /**
128100 * Returns the callable that contains the given endpoint.
129101 *
130102 * Each Java mode should implement this predicate.
131103 */
132- additional Callable getCallable ( Endpoint e ) { result = e .getEnclosingCallable ( ) }
104+ Callable getCallable ( Endpoint e ) { result = e .getEnclosingCallable ( ) }
133105}
134106
135107module CharacteristicsImpl = SharedCharacteristics:: SharedCharacteristics< FrameworkCandidatesImpl > ;
@@ -145,35 +117,19 @@ class Endpoint = FrameworkCandidatesImpl::Endpoint;
145117/**
146118 * A MetadataExtractor that extracts metadata for framework mode.
147119 */
148- class FrameworkModeMetadataExtractor extends MetadataExtractor {
120+ class FrameworkModeMetadataExtractor extends string {
149121 FrameworkModeMetadataExtractor ( ) { this = "FrameworkModeMetadataExtractor" }
150122
151- /**
152- * By convention, the subtypes property of the MaD declaration should only be
153- * true when there _can_ exist any subtypes with a different implementation.
154- *
155- * It would technically be ok to always use the value 'true', but this would
156- * break convention.
157- */
158- boolean considerSubtypes ( Callable callable ) {
159- if
160- callable .isStatic ( ) or
161- callable .getDeclaringType ( ) .isStatic ( ) or
162- callable .isFinal ( ) or
163- callable .getDeclaringType ( ) .isFinal ( )
164- then result = false
165- else result = true
166- }
167-
168- override predicate hasMetadata (
169- Endpoint e , string package , string type , boolean subtypes , string name , string signature ,
170- int input , string parameterName
123+ predicate hasMetadata (
124+ Endpoint e , string package , string type , string subtypes , string name , string signature ,
125+ string input , string parameterName
171126 ) {
172- exists ( Callable callable |
173- e .asParameter ( ) = callable .getParameter ( input ) and
127+ exists ( Callable callable , int paramIdx |
128+ e .asParameter ( ) = callable .getParameter ( paramIdx ) and
129+ input = AutomodelJavaUtil:: getArgumentForIndex ( paramIdx ) and
174130 package = callable .getDeclaringType ( ) .getPackage ( ) .getName ( ) and
175131 type = callable .getDeclaringType ( ) .getErasure ( ) .( RefType ) .nestedName ( ) and
176- subtypes = this . considerSubtypes ( callable ) and
132+ subtypes = AutomodelJavaUtil :: considerSubtypes ( callable ) . toString ( ) and
177133 name = callable .getName ( ) and
178134 parameterName = e .asParameter ( ) .getName ( ) and
179135 signature = ExternalFlow:: paramsString ( callable )
@@ -199,8 +155,8 @@ private class UnexploitableIsCharacteristic extends CharacteristicsImpl::NotASin
199155
200156 override predicate appliesToEndpoint ( Endpoint e ) {
201157 not FrameworkCandidatesImpl:: isSink ( e , _) and
202- FrameworkCandidatesImpl :: getCallable ( e ) .getName ( ) .matches ( "is%" ) and
203- FrameworkCandidatesImpl :: getCallable ( e ) .getReturnType ( ) instanceof BooleanType
158+ FrameworkModeGetCallable :: getCallable ( e ) .getName ( ) .matches ( "is%" ) and
159+ FrameworkModeGetCallable :: getCallable ( e ) .getReturnType ( ) instanceof BooleanType
204160 }
205161}
206162
@@ -218,7 +174,7 @@ private class UnexploitableExistsCharacteristic extends CharacteristicsImpl::Not
218174 override predicate appliesToEndpoint ( Endpoint e ) {
219175 not FrameworkCandidatesImpl:: isSink ( e , _) and
220176 exists ( Callable callable |
221- callable = FrameworkCandidatesImpl :: getCallable ( e ) and
177+ callable = FrameworkModeGetCallable :: getCallable ( e ) and
222178 callable .getName ( ) .toLowerCase ( ) = [ "exists" , "notexists" ] and
223179 callable .getReturnType ( ) instanceof BooleanType
224180 )
@@ -232,7 +188,7 @@ private class ExceptionCharacteristic extends CharacteristicsImpl::NotASinkChara
232188 ExceptionCharacteristic ( ) { this = "exception" }
233189
234190 override predicate appliesToEndpoint ( Endpoint e ) {
235- FrameworkCandidatesImpl :: getCallable ( e ) .getDeclaringType ( ) .getASupertype * ( ) instanceof
191+ FrameworkModeGetCallable :: getCallable ( e ) .getDeclaringType ( ) .getASupertype * ( ) instanceof
236192 TypeThrowable
237193 }
238194}
@@ -258,7 +214,7 @@ private class NonPublicMethodCharacteristic extends CharacteristicsImpl::Uninter
258214 NonPublicMethodCharacteristic ( ) { this = "non-public method" }
259215
260216 override predicate appliesToEndpoint ( Endpoint e ) {
261- not FrameworkCandidatesImpl :: getCallable ( e ) .isPublic ( )
217+ not FrameworkModeGetCallable :: getCallable ( e ) .isPublic ( )
262218 }
263219}
264220
0 commit comments