Skip to content

Commit b713efb

Browse files
committed
Add ThreadLocalRandom.current as another source
1 parent bf0123d commit b713efb

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

java/ql/lib/semmle/code/java/security/WeakRandomnessQuery.qll

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ private class JavaRandomSource extends WeakRandomnessSource {
2929
}
3030
}
3131

32+
/**
33+
* A node representing a call to one of the methods of `org.apache.commons.lang.RandomStringUtils`.
34+
*/
3235
private class ApacheRandomStringUtilsMethodAccessSource extends WeakRandomnessSource {
3336
ApacheRandomStringUtilsMethodAccessSource() {
3437
exists(MethodAccess ma | this.asExpr() = ma |
@@ -44,6 +47,17 @@ private class ApacheRandomStringUtilsMethodAccessSource extends WeakRandomnessSo
4447
}
4548
}
4649

50+
private class ThreadLocalRandomSource extends WeakRandomnessSource {
51+
ThreadLocalRandomSource() {
52+
exists(MethodAccess ma | this.asExpr() = ma |
53+
ma.getMethod().hasName("current") and
54+
ma.getMethod()
55+
.getDeclaringType()
56+
.hasQualifiedName("java.util.concurrent", "ThreadLocalRandom")
57+
)
58+
}
59+
}
60+
4761
/**
4862
* The `random` method of `java.lang.Math`.
4963
*/
@@ -123,7 +137,7 @@ module WeakRandomnessConfig implements DataFlow::ConfigSig {
123137
exists(MethodAccess ma, Method m |
124138
n1.asExpr() = ma.getQualifier() and
125139
ma.getMethod() = m and
126-
m.getDeclaringType() instanceof TypeRandom and
140+
m.getDeclaringType().getAnAncestor() instanceof TypeRandom and
127141
(
128142
m.hasName(["nextInt", "nextLong", "nextFloat", "nextDouble", "nextBoolean", "nextGaussian"]) and
129143
n2.asExpr() = ma

java/ql/test/query-tests/security/CWE-330/WeakRandomCookies.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.io.IOException;
22
import java.util.Random;
3+
import java.util.concurrent.ThreadLocalRandom;
34
import java.security.SecureRandom;
45
import javax.servlet.ServletException;
56
import javax.servlet.http.HttpServlet;
@@ -36,5 +37,10 @@ public void doGet() {
3637
// GOOD: The cookie value is unpredictable.
3738
Cookie cookie4 = new Cookie("name", new String(bytes2));
3839
response.addCookie(cookie4);
40+
41+
ThreadLocalRandom tlr = ThreadLocalRandom.current();
42+
43+
Cookie cookie5 = new Cookie("name", Integer.toString(tlr.nextInt()));
44+
response.addCookie(cookie5); // $hasWeakRandomFlow
3945
}
4046
}

0 commit comments

Comments
 (0)