@@ -18,7 +18,7 @@ import DataFlow::PathGraph
18
18
*/
19
19
private string getPrivateHostRegex ( ) {
20
20
result =
21
- "(?i)localhost(?:[:/?#].*)?|127\\.0\\.0\\.1(?:[:/?#].*)?|10(?:\\.[0-9]+){3}(?:[:/?#].*)?|172\\.16(?:\\.[0-9]+){2}(?:[:/?#].*)?|192.168(?:\\.[0-9]+){2}(?:[:/?#].*)?|\\[0:0:0:0:0:0:0:1\\](?:[:/?#].*)?|\\[::1\\](?:[:/?#].*)?"
21
+ "(?i)localhost(?:[:/?#].*)?|127\\.0\\.0\\.1(?:[:/?#].*)?|10(?:\\.[0-9]+){3}(?:[:/?#].*)?|172\\.16(?:\\.[0-9]+){2}(?:[:/?#].*)?|192.168(?:\\.[0-9]+){2}(?:[:/?#].*)?|\\[? 0:0:0:0:0:0:0:1\\]? (?:[:/?#].*)?|\\[? ::1\\]? (?:[:/?#].*)?"
22
22
}
23
23
24
24
/**
@@ -48,9 +48,8 @@ class URLConstructor extends ClassInstanceExpr {
48
48
// `URL(String protocol, String host, int port, String file, URLStreamHandler handler)`,
49
49
// `URL(String protocol, String host, String file)`
50
50
this .getConstructor ( ) .getNumberOfParameters ( ) > 1 and
51
- concatHttpString ( getArgument ( 0 ) , this .getArgument ( 1 ) )
51
+ concatHttpString ( getArgument ( 0 ) , this .getArgument ( 1 ) ) // First argument contains the protocol part and the second argument contains the host part.
52
52
or
53
- // First argument contains the protocol part and the second argument contains the host part.
54
53
// URLs constructed with the string constructor `URL(String spec)`
55
54
this .getConstructor ( ) .getNumberOfParameters ( ) = 1 and
56
55
this .getArgument ( 0 ) instanceof HttpString // First argument contains the whole spec.
@@ -88,7 +87,7 @@ class HttpStringLiteral extends StringLiteral {
88
87
HttpStringLiteral ( ) {
89
88
// Match URLs with the HTTP protocol and without private IP addresses to reduce false positives.
90
89
exists ( string s | this .getRepresentedString ( ) = s |
91
- s .regexpMatch ( "(?i)http://[\\[a-zA-Z0-9].*" ) and
90
+ s .regexpMatch ( "(?i)http://[\\[: a-zA-Z0-9].*" ) and
92
91
not s .substring ( 7 , s .length ( ) ) .regexpMatch ( getPrivateHostRegex ( ) )
93
92
)
94
93
}
@@ -107,6 +106,16 @@ predicate concatHttpString(Expr protocol, Expr host) {
107
106
.( CompileTimeConstantExpr )
108
107
.getStringValue ( )
109
108
.regexpMatch ( "(?i)http(://)?" )
109
+ ) and // Not empty host string
110
+ (
111
+ host .( CompileTimeConstantExpr ) .getStringValue ( ) .length ( ) > 0 or
112
+ host
113
+ .( VarAccess )
114
+ .getVariable ( )
115
+ .getAnAssignedValue ( )
116
+ .( CompileTimeConstantExpr )
117
+ .getStringValue ( )
118
+ .length ( ) > 0
110
119
) and
111
120
not (
112
121
host .( CompileTimeConstantExpr ) .getStringValue ( ) .regexpMatch ( getPrivateHostRegex ( ) ) or
@@ -170,13 +179,15 @@ predicate apacheHttpRequest(DataFlow::Node node1, DataFlow::Node node2) {
170
179
171
180
/** `URI` methods */
172
181
predicate createURI ( DataFlow:: Node node1 , DataFlow:: Node node2 ) {
173
- exists ( URIConstructor cc | // new URI
182
+ exists (
183
+ URIConstructor cc // new URI
184
+ |
174
185
node2 .asExpr ( ) = cc and
175
186
cc .getArgument ( 0 ) = node1 .asExpr ( )
176
187
)
177
188
or
178
189
exists (
179
- StaticMethodAccess ma // URI.create
190
+ StaticMethodAccess ma // URI.create
180
191
|
181
192
ma .getMethod ( ) .getDeclaringType ( ) .hasQualifiedName ( "java.net" , "URI" ) and
182
193
ma .getMethod ( ) .hasName ( "create" ) and
0 commit comments