Skip to content

Commit 1daa83b

Browse files
committed
Add test cases
1 parent e69ff7b commit 1daa83b

4 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.io.IOException;
2+
import java.util.Random;
3+
import java.security.SecureRandom;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.http.HttpServlet;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import javax.servlet.http.Cookie;
9+
10+
public class WeakRandomCookies extends HttpServlet {
11+
HttpServletResponse response;
12+
13+
public void doGet() {
14+
Random r = new Random();
15+
16+
int c = r.nextInt();
17+
// BAD: The cookie value may be predictable.
18+
Cookie cookie = new Cookie("name", Integer.toString(c));
19+
response.addCookie(cookie); // $hasWeakRandomFlow
20+
21+
int c2 = r.nextInt();
22+
// BAD: The cookie value may be predictable.
23+
Cookie cookie2 = new Cookie("name" + c2, "value");
24+
response.addCookie(cookie2); // $hasWeakRandomFlow
25+
26+
byte[] bytes = new byte[16];
27+
r.nextBytes(bytes);
28+
// BAD: The cookie value may be predictable.
29+
Cookie cookie3 = new Cookie("name", new String(bytes));
30+
response.addCookie(cookie3); // $hasWeakRandomFlow
31+
32+
SecureRandom sr = new SecureRandom();
33+
34+
byte[] bytes2 = new byte[16];
35+
sr.nextBytes(bytes2);
36+
// GOOD: The cookie value is unpredictable.
37+
Cookie cookie4 = new Cookie("name", new String(bytes2));
38+
response.addCookie(cookie4);
39+
}
40+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failures
2+
testFailures
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java
2+
import semmle.code.java.security.WeakRandomnessQuery
3+
import TestUtilities.InlineExpectationsTest
4+
5+
module WeakRandomTest implements TestSig {
6+
string getARelevantTag() { result = "hasWeakRandomFlow" }
7+
8+
predicate hasActualResult(Location location, string element, string tag, string value) {
9+
tag = "hasWeakRandomFlow" and
10+
exists(DataFlow::Node sink | WeakRandomnessFlow::flowTo(sink) |
11+
sink.getLocation() = location and
12+
element = sink.toString() and
13+
value = ""
14+
)
15+
}
16+
}
17+
18+
import MakeTest<WeakRandomTest>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args -cp ${testdir}/../../../stubs/servlet-api-2.4

0 commit comments

Comments
 (0)