Skip to content

Commit 85c2f72

Browse files
committed
Java: convert InsecureRandomness test to .qlref
1 parent 288a938 commit 85c2f72

File tree

4 files changed

+93
-39
lines changed

4 files changed

+93
-39
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ public class InsecureRandomCookies extends HttpServlet {
1616
public void doGet() {
1717
Random r = new Random();
1818

19-
int c = r.nextInt();
19+
int c = r.nextInt(); // $ Source
2020
// BAD: The cookie value may be predictable.
21-
Cookie cookie = new Cookie("name", Integer.toString(c)); // $hasWeakRandomFlow
22-
cookie.setValue(Integer.toString(c)); // $hasWeakRandomFlow
21+
Cookie cookie = new Cookie("name", Integer.toString(c)); // $ Alert
22+
cookie.setValue(Integer.toString(c)); // $ Alert
2323

2424
io.netty.handler.codec.http.Cookie nettyCookie =
25-
new io.netty.handler.codec.http.DefaultCookie("name", Integer.toString(c)); // $hasWeakRandomFlow
26-
nettyCookie.setValue(Integer.toString(c)); // $hasWeakRandomFlow
25+
new io.netty.handler.codec.http.DefaultCookie("name", Integer.toString(c)); // $ Alert
26+
nettyCookie.setValue(Integer.toString(c)); // $ Alert
2727
io.netty.handler.codec.http.cookie.Cookie nettyCookie2 =
28-
new io.netty.handler.codec.http.cookie.DefaultCookie("name", Integer.toString(c)); // $hasWeakRandomFlow
29-
nettyCookie2.setValue(Integer.toString(c)); // $hasWeakRandomFlow
28+
new io.netty.handler.codec.http.cookie.DefaultCookie("name", Integer.toString(c)); // $ Alert
29+
nettyCookie2.setValue(Integer.toString(c)); // $ Alert
3030

3131
Encoder enc = null;
32-
int c2 = r.nextInt();
32+
int c2 = r.nextInt(); // $ Source
3333
String value = enc.encodeForHTML(Integer.toString(c2));
3434
// BAD: The cookie value may be predictable.
35-
Cookie cookie2 = new Cookie("name", value); // $hasWeakRandomFlow
35+
Cookie cookie2 = new Cookie("name", value); // $ Alert
3636

3737
byte[] bytes = new byte[16];
38-
r.nextBytes(bytes);
38+
r.nextBytes(bytes); // $ Source
3939
// BAD: The cookie value may be predictable.
40-
Cookie cookie3 = new Cookie("name", new String(bytes)); // $hasWeakRandomFlow
40+
Cookie cookie3 = new Cookie("name", new String(bytes)); // $ Alert
4141

4242
SecureRandom sr = new SecureRandom();
4343

@@ -48,22 +48,22 @@ public void doGet() {
4848

4949
ThreadLocalRandom tlr = ThreadLocalRandom.current();
5050

51-
Cookie cookie5 = new Cookie("name", Integer.toString(tlr.nextInt())); // $hasWeakRandomFlow
51+
Cookie cookie5 = new Cookie("name", Integer.toString(tlr.nextInt())); // $ Alert
5252

53-
Cookie cookie6 = new Cookie("name", RandomStringUtils.random(10)); // $hasWeakRandomFlow
53+
Cookie cookie6 = new Cookie("name", RandomStringUtils.random(10)); // $ Alert
5454

55-
Cookie cookie7 = new Cookie("name", RandomStringUtils.randomAscii(10)); // $hasWeakRandomFlow
55+
Cookie cookie7 = new Cookie("name", RandomStringUtils.randomAscii(10)); // $ Alert
5656

57-
long c3 = r.nextLong();
57+
long c3 = r.nextLong(); // $ Source
5858
// BAD: The cookie value may be predictable.
59-
Cookie cookie8 = new Cookie("name", Long.toString(c3 * 5)); // $hasWeakRandomFlow
59+
Cookie cookie8 = new Cookie("name", Long.toString(c3 * 5)); // $ Alert
6060

61-
double c4 = Math.random();
61+
double c4 = Math.random(); // $ Source
6262
// BAD: The cookie value may be predictable.
63-
Cookie cookie9 = new Cookie("name", Double.toString(c4)); // $hasWeakRandomFlow
63+
Cookie cookie9 = new Cookie("name", Double.toString(c4)); // $ Alert
6464

65-
double c5 = Math.random();
65+
double c5 = Math.random(); // $ Source
6666
// BAD: The cookie value may be predictable.
67-
Cookie cookie10 = new Cookie("name", Double.toString(++c5)); // $hasWeakRandomFlow
67+
Cookie cookie10 = new Cookie("name", Double.toString(++c5)); // $ Alert
6868
}
6969
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#select
2+
| InsecureRandomCookies.java:21:44:21:62 | toString(...) | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:21:44:21:62 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) | Insecure randomness source. |
3+
| InsecureRandomCookies.java:22:25:22:43 | toString(...) | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:22:25:22:43 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) | Insecure randomness source. |
4+
| InsecureRandomCookies.java:25:71:25:89 | toString(...) | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:25:71:25:89 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) | Insecure randomness source. |
5+
| InsecureRandomCookies.java:26:30:26:48 | toString(...) | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:26:30:26:48 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) | Insecure randomness source. |
6+
| InsecureRandomCookies.java:28:78:28:96 | toString(...) | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:28:78:28:96 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) | Insecure randomness source. |
7+
| InsecureRandomCookies.java:29:31:29:49 | toString(...) | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:29:31:29:49 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:19:17:19:27 | nextInt(...) | Insecure randomness source. |
8+
| InsecureRandomCookies.java:35:45:35:49 | value | InsecureRandomCookies.java:32:18:32:28 | nextInt(...) : Number | InsecureRandomCookies.java:35:45:35:49 | value | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:32:18:32:28 | nextInt(...) | Insecure randomness source. |
9+
| InsecureRandomCookies.java:40:45:40:61 | new String(...) | InsecureRandomCookies.java:38:21:38:25 | bytes : byte[] | InsecureRandomCookies.java:40:45:40:61 | new String(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:38:21:38:25 | bytes | Insecure randomness source. |
10+
| InsecureRandomCookies.java:51:45:51:75 | toString(...) | InsecureRandomCookies.java:51:62:51:74 | nextInt(...) : Number | InsecureRandomCookies.java:51:45:51:75 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:51:62:51:74 | nextInt(...) | Insecure randomness source. |
11+
| InsecureRandomCookies.java:53:45:53:72 | random(...) | InsecureRandomCookies.java:53:45:53:72 | random(...) | InsecureRandomCookies.java:53:45:53:72 | random(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:53:45:53:72 | random(...) | Insecure randomness source. |
12+
| InsecureRandomCookies.java:55:45:55:77 | randomAscii(...) | InsecureRandomCookies.java:55:45:55:77 | randomAscii(...) | InsecureRandomCookies.java:55:45:55:77 | randomAscii(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:55:45:55:77 | randomAscii(...) | Insecure randomness source. |
13+
| InsecureRandomCookies.java:59:45:59:65 | toString(...) | InsecureRandomCookies.java:57:19:57:30 | nextLong(...) : Number | InsecureRandomCookies.java:59:45:59:65 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:57:19:57:30 | nextLong(...) | Insecure randomness source. |
14+
| InsecureRandomCookies.java:63:45:63:63 | toString(...) | InsecureRandomCookies.java:61:21:61:33 | random(...) : Number | InsecureRandomCookies.java:63:45:63:63 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:61:21:61:33 | random(...) | Insecure randomness source. |
15+
| InsecureRandomCookies.java:67:46:67:66 | toString(...) | InsecureRandomCookies.java:65:21:65:33 | random(...) : Number | InsecureRandomCookies.java:67:46:67:66 | toString(...) | Potential Insecure randomness due to a $@. | InsecureRandomCookies.java:65:21:65:33 | random(...) | Insecure randomness source. |
16+
edges
17+
| InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:21:44:21:62 | toString(...) | provenance | TaintPreservingCallable |
18+
| InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:22:25:22:43 | toString(...) | provenance | TaintPreservingCallable |
19+
| InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:25:71:25:89 | toString(...) | provenance | TaintPreservingCallable |
20+
| InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:26:30:26:48 | toString(...) | provenance | TaintPreservingCallable |
21+
| InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:28:78:28:96 | toString(...) | provenance | TaintPreservingCallable |
22+
| InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | InsecureRandomCookies.java:29:31:29:49 | toString(...) | provenance | TaintPreservingCallable |
23+
| InsecureRandomCookies.java:32:18:32:28 | nextInt(...) : Number | InsecureRandomCookies.java:33:42:33:61 | toString(...) : String | provenance | TaintPreservingCallable |
24+
| InsecureRandomCookies.java:33:24:33:62 | encodeForHTML(...) : String | InsecureRandomCookies.java:35:45:35:49 | value | provenance | |
25+
| InsecureRandomCookies.java:33:42:33:61 | toString(...) : String | InsecureRandomCookies.java:33:24:33:62 | encodeForHTML(...) : String | provenance | Config |
26+
| InsecureRandomCookies.java:33:42:33:61 | toString(...) : String | InsecureRandomCookies.java:33:24:33:62 | encodeForHTML(...) : String | provenance | MaD:2 |
27+
| InsecureRandomCookies.java:38:21:38:25 | bytes : byte[] | InsecureRandomCookies.java:40:56:40:60 | bytes : byte[] | provenance | |
28+
| InsecureRandomCookies.java:40:56:40:60 | bytes : byte[] | InsecureRandomCookies.java:40:45:40:61 | new String(...) | provenance | MaD:1 |
29+
| InsecureRandomCookies.java:51:62:51:74 | nextInt(...) : Number | InsecureRandomCookies.java:51:45:51:75 | toString(...) | provenance | TaintPreservingCallable |
30+
| InsecureRandomCookies.java:57:19:57:30 | nextLong(...) : Number | InsecureRandomCookies.java:59:59:59:60 | c3 : Number | provenance | |
31+
| InsecureRandomCookies.java:59:59:59:60 | c3 : Number | InsecureRandomCookies.java:59:59:59:64 | ... * ... : Number | provenance | Config |
32+
| InsecureRandomCookies.java:59:59:59:64 | ... * ... : Number | InsecureRandomCookies.java:59:45:59:65 | toString(...) | provenance | TaintPreservingCallable |
33+
| InsecureRandomCookies.java:61:21:61:33 | random(...) : Number | InsecureRandomCookies.java:63:45:63:63 | toString(...) | provenance | TaintPreservingCallable |
34+
| InsecureRandomCookies.java:65:21:65:33 | random(...) : Number | InsecureRandomCookies.java:67:64:67:65 | c5 : Number | provenance | |
35+
| InsecureRandomCookies.java:67:62:67:65 | ++... : Number | InsecureRandomCookies.java:67:46:67:66 | toString(...) | provenance | TaintPreservingCallable |
36+
| InsecureRandomCookies.java:67:64:67:65 | c5 : Number | InsecureRandomCookies.java:67:62:67:65 | ++... : Number | provenance | Config |
37+
models
38+
| 1 | Summary: java.lang; String; false; String; ; ; Argument[0]; Argument[this]; taint; manual |
39+
| 2 | Summary: org.owasp.esapi; Encoder; true; encodeForHTML; (String); ; Argument[0]; ReturnValue; taint; manual |
40+
nodes
41+
| InsecureRandomCookies.java:19:17:19:27 | nextInt(...) : Number | semmle.label | nextInt(...) : Number |
42+
| InsecureRandomCookies.java:21:44:21:62 | toString(...) | semmle.label | toString(...) |
43+
| InsecureRandomCookies.java:22:25:22:43 | toString(...) | semmle.label | toString(...) |
44+
| InsecureRandomCookies.java:25:71:25:89 | toString(...) | semmle.label | toString(...) |
45+
| InsecureRandomCookies.java:26:30:26:48 | toString(...) | semmle.label | toString(...) |
46+
| InsecureRandomCookies.java:28:78:28:96 | toString(...) | semmle.label | toString(...) |
47+
| InsecureRandomCookies.java:29:31:29:49 | toString(...) | semmle.label | toString(...) |
48+
| InsecureRandomCookies.java:32:18:32:28 | nextInt(...) : Number | semmle.label | nextInt(...) : Number |
49+
| InsecureRandomCookies.java:33:24:33:62 | encodeForHTML(...) : String | semmle.label | encodeForHTML(...) : String |
50+
| InsecureRandomCookies.java:33:42:33:61 | toString(...) : String | semmle.label | toString(...) : String |
51+
| InsecureRandomCookies.java:35:45:35:49 | value | semmle.label | value |
52+
| InsecureRandomCookies.java:38:21:38:25 | bytes : byte[] | semmle.label | bytes : byte[] |
53+
| InsecureRandomCookies.java:40:45:40:61 | new String(...) | semmle.label | new String(...) |
54+
| InsecureRandomCookies.java:40:56:40:60 | bytes : byte[] | semmle.label | bytes : byte[] |
55+
| InsecureRandomCookies.java:51:45:51:75 | toString(...) | semmle.label | toString(...) |
56+
| InsecureRandomCookies.java:51:62:51:74 | nextInt(...) : Number | semmle.label | nextInt(...) : Number |
57+
| InsecureRandomCookies.java:53:45:53:72 | random(...) | semmle.label | random(...) |
58+
| InsecureRandomCookies.java:55:45:55:77 | randomAscii(...) | semmle.label | randomAscii(...) |
59+
| InsecureRandomCookies.java:57:19:57:30 | nextLong(...) : Number | semmle.label | nextLong(...) : Number |
60+
| InsecureRandomCookies.java:59:45:59:65 | toString(...) | semmle.label | toString(...) |
61+
| InsecureRandomCookies.java:59:59:59:60 | c3 : Number | semmle.label | c3 : Number |
62+
| InsecureRandomCookies.java:59:59:59:64 | ... * ... : Number | semmle.label | ... * ... : Number |
63+
| InsecureRandomCookies.java:61:21:61:33 | random(...) : Number | semmle.label | random(...) : Number |
64+
| InsecureRandomCookies.java:63:45:63:63 | toString(...) | semmle.label | toString(...) |
65+
| InsecureRandomCookies.java:65:21:65:33 | random(...) : Number | semmle.label | random(...) : Number |
66+
| InsecureRandomCookies.java:67:46:67:66 | toString(...) | semmle.label | toString(...) |
67+
| InsecureRandomCookies.java:67:62:67:65 | ++... : Number | semmle.label | ++... : Number |
68+
| InsecureRandomCookies.java:67:64:67:65 | c5 : Number | semmle.label | c5 : Number |
69+
subpaths

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: Security/CWE/CWE-330/InsecureRandomness.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql

0 commit comments

Comments
 (0)