1010 * external/cwe/cwe-023
1111 */
1212
13- import java
14-
13+ import java
14+ private import semmle.code.java.dataflow.DataFlow
15+ private import semmle.code.java.environment.SystemProperty
1516
1617class MethodStringStartsWith extends Method {
17- MethodStringStartsWith ( ) {
18- this .hasName ( "startsWith" )
19- }
18+ MethodStringStartsWith ( ) {
19+ this .getDeclaringType ( ) instanceof TypeString and
20+ this .hasName ( "startsWith" )
21+ }
22+ }
23+
24+ class MethodFileGetCanonicalPath extends Method {
25+ MethodFileGetCanonicalPath ( ) {
26+ this .getDeclaringType ( ) instanceof TypeFile and
27+ this .hasName ( "getCanonicalPath" )
28+ }
2029}
2130
22- from MethodAccess ma
23- where ma .getMethod ( ) instanceof MethodStringStartsWith
24- select ma , "Partial Path Traversal Vulnerability due to insufficient guard against path traversal"
31+ class MethodAccessFileGetCanonicalPath extends MethodAccess {
32+ MethodAccessFileGetCanonicalPath ( ) { this .getMethod ( ) instanceof MethodFileGetCanonicalPath }
33+ }
34+
35+ abstract class FileSeparatorExpr extends Expr { }
36+
37+ class SystemPropFileSeparatorExpr extends FileSeparatorExpr {
38+ SystemPropFileSeparatorExpr ( ) { this = getSystemProperty ( "file.separator" ) }
39+ }
40+
41+ class StringLiteralFileSeparatorExpr extends FileSeparatorExpr , StringLiteral {
42+ StringLiteralFileSeparatorExpr ( ) { this .getValue ( ) = "/" }
43+ }
44+
45+ class FileSeparatorAppend extends AddExpr {
46+ FileSeparatorAppend ( ) { this .getRightOperand ( ) instanceof FileSeparatorExpr }
47+ }
48+
49+ predicate isSafe ( Expr expr ) { DataFlow:: localExprFlow ( any ( FileSeparatorAppend fsa ) , expr ) }
50+
51+ from MethodAccess ma
52+ where
53+ ma .getMethod ( ) instanceof MethodStringStartsWith and
54+ DataFlow:: localExprFlow ( any ( MethodAccessFileGetCanonicalPath gcpma ) , ma .getQualifier ( ) ) and
55+ not isSafe ( ma .getArgument ( 0 ) )
56+ select ma , "Partial Path Traversal Vulnerability due to insufficient guard against path traversal"
0 commit comments