Skip to content

Commit bbf9937

Browse files
committed
Alter cookie sinks to instead focus on creation of a cookie
1 parent 4bdf2b5 commit bbf9937

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@ private class TypeHadoopOsSecureRandom extends SafeRandomImplementation {
4646
abstract class WeakRandomnessSink extends DataFlow::Node { }
4747

4848
/**
49-
* A node which creates a cookie.
49+
* A node which sets the value of a cookie.
5050
*/
5151
private class CookieSink extends WeakRandomnessSink {
5252
CookieSink() {
53-
this.getType() instanceof TypeCookie and
54-
exists(MethodCall mc |
55-
mc.getMethod().hasQualifiedName("javax.servlet.http", "HttpServletResponse", "addCookie")
56-
|
57-
mc.getArgument(0) = this.asExpr()
53+
exists(Call c |
54+
c.(ClassInstanceExpr).getConstructedType() instanceof TypeCookie and
55+
this.asExpr() = c.getArgument(1)
56+
or
57+
c.(MethodCall).getMethod().getDeclaringType() instanceof TypeCookie and
58+
c.(MethodCall).getMethod().hasName("setValue") and
59+
this.asExpr() = c.getArgument(0)
5860
)
5961
}
6062
}

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,44 @@ public void doGet() {
1818

1919
int c = r.nextInt();
2020
// BAD: The cookie value may be predictable.
21-
Cookie cookie = new Cookie("name", Integer.toString(c));
22-
response.addCookie(cookie); // $hasWeakRandomFlow
21+
Cookie cookie = new Cookie("name", Integer.toString(c)); // $hasWeakRandomFlow
2322

2423
Encoder enc = null;
2524
int c2 = r.nextInt();
2625
String value = enc.encodeForHTML(Integer.toString(c2));
2726
// BAD: The cookie value may be predictable.
28-
Cookie cookie2 = new Cookie("name", value);
29-
response.addCookie(cookie2); // $hasWeakRandomFlow
27+
Cookie cookie2 = new Cookie("name", value); // $hasWeakRandomFlow
3028

3129
byte[] bytes = new byte[16];
3230
r.nextBytes(bytes);
3331
// BAD: The cookie value may be predictable.
34-
Cookie cookie3 = new Cookie("name", new String(bytes));
35-
response.addCookie(cookie3); // $hasWeakRandomFlow
32+
Cookie cookie3 = new Cookie("name", new String(bytes)); // $hasWeakRandomFlow
3633

3734
SecureRandom sr = new SecureRandom();
3835

3936
byte[] bytes2 = new byte[16];
4037
sr.nextBytes(bytes2);
4138
// GOOD: The cookie value is unpredictable.
42-
Cookie cookie4 = new Cookie("name", new String(bytes2));
43-
response.addCookie(cookie4);
44-
39+
Cookie cookie4 = new Cookie("name", new String(bytes2));
40+
4541
ThreadLocalRandom tlr = ThreadLocalRandom.current();
4642

47-
Cookie cookie5 = new Cookie("name", Integer.toString(tlr.nextInt()));
48-
response.addCookie(cookie5); // $hasWeakRandomFlow
43+
Cookie cookie5 = new Cookie("name", Integer.toString(tlr.nextInt())); // $hasWeakRandomFlow
4944

50-
Cookie cookie6 = new Cookie("name", RandomStringUtils.random(10));
51-
response.addCookie(cookie6); // $hasWeakRandomFlow
45+
Cookie cookie6 = new Cookie("name", RandomStringUtils.random(10)); // $hasWeakRandomFlow
5246

53-
Cookie cookie7 = new Cookie("name", RandomStringUtils.randomAscii(10));
54-
response.addCookie(cookie7); // $hasWeakRandomFlow
47+
Cookie cookie7 = new Cookie("name", RandomStringUtils.randomAscii(10)); // $hasWeakRandomFlow
5548

5649
long c3 = r.nextLong();
5750
// BAD: The cookie value may be predictable.
58-
Cookie cookie8 = new Cookie("name", Long.toString(c3 * 5));
59-
response.addCookie(cookie8); // $hasWeakRandomFlow
51+
Cookie cookie8 = new Cookie("name", Long.toString(c3 * 5)); // $hasWeakRandomFlow
6052

6153
double c4 = Math.random();
6254
// BAD: The cookie value may be predictable.
63-
Cookie cookie9 = new Cookie("name", Double.toString(c4));
64-
response.addCookie(cookie9); // $hasWeakRandomFlow
55+
Cookie cookie9 = new Cookie("name", Double.toString(c4)); // $hasWeakRandomFlow
6556

6657
double c5 = Math.random();
6758
// BAD: The cookie value may be predictable.
68-
Cookie cookie10 = new Cookie("name", Double.toString(++c5));
69-
response.addCookie(cookie10); // $hasWeakRandomFlow
59+
Cookie cookie10 = new Cookie("name", Double.toString(++c5)); // $hasWeakRandomFlow
7060
}
7161
}

0 commit comments

Comments
 (0)