Skip to content

Commit 9a8eed8

Browse files
committed
Enhance address match
1 parent ff0dacf commit 9a8eed8

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

java/ql/src/experimental/Security/CWE/CWE-522/InsecureBasicAuth.ql

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import DataFlow::PathGraph
1818
*/
1919
private string getPrivateHostRegex() {
2020
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\\]?(?:[:/?#].*)?"
2222
}
2323

2424
/**
@@ -48,9 +48,8 @@ class URLConstructor extends ClassInstanceExpr {
4848
// `URL(String protocol, String host, int port, String file, URLStreamHandler handler)`,
4949
// `URL(String protocol, String host, String file)`
5050
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.
5252
or
53-
// First argument contains the protocol part and the second argument contains the host part.
5453
// URLs constructed with the string constructor `URL(String spec)`
5554
this.getConstructor().getNumberOfParameters() = 1 and
5655
this.getArgument(0) instanceof HttpString // First argument contains the whole spec.
@@ -88,7 +87,7 @@ class HttpStringLiteral extends StringLiteral {
8887
HttpStringLiteral() {
8988
// Match URLs with the HTTP protocol and without private IP addresses to reduce false positives.
9089
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
9291
not s.substring(7, s.length()).regexpMatch(getPrivateHostRegex())
9392
)
9493
}
@@ -107,6 +106,16 @@ predicate concatHttpString(Expr protocol, Expr host) {
107106
.(CompileTimeConstantExpr)
108107
.getStringValue()
109108
.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
110119
) and
111120
not (
112121
host.(CompileTimeConstantExpr).getStringValue().regexpMatch(getPrivateHostRegex()) or
@@ -170,13 +179,15 @@ predicate apacheHttpRequest(DataFlow::Node node1, DataFlow::Node node2) {
170179

171180
/** `URI` methods */
172181
predicate createURI(DataFlow::Node node1, DataFlow::Node node2) {
173-
exists(URIConstructor cc | // new URI
182+
exists(
183+
URIConstructor cc // new URI
184+
|
174185
node2.asExpr() = cc and
175186
cc.getArgument(0) = node1.asExpr()
176187
)
177188
or
178189
exists(
179-
StaticMethodAccess ma // URI.create
190+
StaticMethodAccess ma // URI.create
180191
|
181192
ma.getMethod().getDeclaringType().hasQualifiedName("java.net", "URI") and
182193
ma.getMethod().hasName("create") and

java/ql/test/experimental/query-tests/security/CWE-522/InsecureBasicAuth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void testApacheHttpRequest(String username, String password) {
3232
* Test basic authentication with Apache HTTP GET request.
3333
*/
3434
public void testApacheHttpRequest2(String url) throws java.io.IOException {
35-
String urlStr = "http://www.example.com:dashboardPort/payment/retrieve";
35+
String urlStr = "http://www.example.com:8000/payment/retrieve";
3636
HttpGet get = new HttpGet(urlStr);
3737
get.setHeader("Accept", "application/json");
3838
get.setHeader("Authorization", "Basic " + new String(Base64.getEncoder().encode("admin:test".getBytes())));

0 commit comments

Comments
 (0)