44
55import Member
66import semmle.code.java.security.ExternalProcess
7+ private import semmle.code.java.dataflow.FlowSteps
78
89// --- Standard types ---
910/** The class `java.lang.Object`. */
@@ -37,6 +38,27 @@ class StringLengthMethod extends Method {
3738 StringLengthMethod ( ) { this .hasName ( "length" ) and this .getDeclaringType ( ) instanceof TypeString }
3839}
3940
41+ /**
42+ * The methods on the class `java.lang.String` that are used to perform partial matches with a specified substring or char.
43+ */
44+ class StringPartialMatchMethod extends Method {
45+ StringPartialMatchMethod ( ) {
46+ this .hasName ( [
47+ "contains" , "startsWith" , "endsWith" , "matches" , "indexOf" , "lastIndexOf" , "regionMatches"
48+ ] ) and
49+ this .getDeclaringType ( ) instanceof TypeString
50+ }
51+
52+ /**
53+ * Gets the index of the parameter that is being matched against.
54+ */
55+ int getMatchParameterIndex ( ) {
56+ if this .hasName ( "regionMatches" )
57+ then this .getParameterType ( result ) instanceof TypeString
58+ else result = 0
59+ }
60+ }
61+
4062/** The class `java.lang.StringBuffer`. */
4163class TypeStringBuffer extends Class {
4264 TypeStringBuffer ( ) { this .hasQualifiedName ( "java.lang" , "StringBuffer" ) }
@@ -228,11 +250,13 @@ class MethodSystemGetenv extends Method {
228250/**
229251 * Any method named `getProperty` on class `java.lang.System`.
230252 */
231- class MethodSystemGetProperty extends Method {
253+ class MethodSystemGetProperty extends ValuePreservingMethod {
232254 MethodSystemGetProperty ( ) {
233255 this .hasName ( "getProperty" ) and
234256 this .getDeclaringType ( ) instanceof TypeSystem
235257 }
258+
259+ override predicate returnsValue ( int arg ) { arg = 1 }
236260}
237261
238262/**
@@ -244,6 +268,9 @@ class MethodAccessSystemGetProperty extends MethodAccess {
244268 /**
245269 * Holds if this call has a compile-time constant first argument with the value `propertyName`.
246270 * For example: `System.getProperty("user.dir")`.
271+ *
272+ * Note: Better to use `semmle.code.java.environment.SystemProperty#getSystemProperty` instead
273+ * as that predicate covers ways of accessing the same information via various libraries.
247274 */
248275 predicate hasCompileTimeConstantGetPropertyName ( string propertyName ) {
249276 this .getArgument ( 0 ) .( CompileTimeConstantExpr ) .getStringValue ( ) = propertyName
0 commit comments