Skip to content

Commit bb15a8e

Browse files
author
Sakshis
committed
cookie-missing-samesite-java
1 parent 2f95a8e commit bb15a8e

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
id: cookie-missing-samesite-java
2+
severity: warning
3+
language: java
4+
message: >-
5+
The application does not appear to verify inbound requests which can
6+
lead to a Cross-site request forgery (CSRF) vulnerability. If the
7+
application uses cookie-based authentication, an attacker can trick users
8+
into sending authenticated HTTP requests without their knowledge from any
9+
arbitrary domain they visit. To prevent this vulnerability start by
10+
identifying if the framework or library leveraged has built-in features or
11+
offers plugins for CSRF protection. CSRF tokens should be unique and
12+
securely random. The `Synchronizer Token` or `Double Submit Cookie`
13+
patterns with defense-in-depth mechanisms such as the `sameSite` cookie
14+
flag can help prevent CSRF. For more information, see: [Cross-site request
15+
forgery prevention](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Req\
16+
uest_Forgery_Prevention_Cheat_Sheet.html).
17+
note: >-
18+
[CWE-352] Cross-Site Request Forgery (CSRF).
19+
[REFERENCES]
20+
- https://stackoverflow.com/questions/42717210/samesite-cookie-in-java-application
21+
rule:
22+
any:
23+
- pattern: $RESP.setHeader("Set-Cookie", $T);
24+
inside:
25+
stopBy: end
26+
kind: block
27+
follows:
28+
stopBy: end
29+
kind: formal_parameters
30+
has:
31+
stopBy: end
32+
kind: formal_parameter
33+
all:
34+
- has:
35+
stopBy: end
36+
kind: type_identifier
37+
regex: '^HttpServletResponse$'
38+
- has:
39+
stopBy: neighbor
40+
kind: identifier
41+
- pattern: $RESP.addCookie($$$);
42+
not:
43+
follows:
44+
stopBy: end
45+
kind: expression_statement
46+
pattern: $RESP.setHeader("Set-Cookie", $T);
47+
inside:
48+
stopBy: end
49+
kind: block
50+
follows:
51+
stopBy: end
52+
kind: formal_parameters
53+
has:
54+
stopBy: end
55+
kind: formal_parameter
56+
all:
57+
- has:
58+
stopBy: end
59+
kind: type_identifier
60+
regex: '^HttpServletResponse$'
61+
- has:
62+
stopBy: neighbor
63+
kind: identifier
64+
- pattern: $RESP.setHeader("Set-Cookie");
65+
constraints:
66+
T:
67+
not:
68+
regex: ".*SameSite=.*"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
id: cookie-missing-samesite-java
2+
snapshots:
3+
? |
4+
@RequestMapping(value = "/cookie3", method = "GET")
5+
public void setSecureHttponlyCookie(@RequestParam String value, HttpServletResponse response) {
6+
Cookie cookie = new Cookie("cookie", value);
7+
cookie.setSecure(true);
8+
cookie.setHttpOnly(true);
9+
response.addCookie(cookie);
10+
}
11+
@RequestMapping(value = "/cookie2", method = "GET")
12+
public void setSecureCookie(@RequestParam String value, HttpServletResponse response) {
13+
response.setHeader("Set-Cookie", "key=value; HttpOnly;");
14+
}
15+
: labels:
16+
- source: response.addCookie(cookie);
17+
style: primary
18+
start: 255
19+
end: 282
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
id: cookie-missing-samesite-java
2+
valid:
3+
- |
4+
@RequestMapping(value = "/cookie1", method = "GET")
5+
public void setCookie(@RequestParam String value, HttpServletResponse response) {
6+
response.setHeader("Set-Cookie", "key=value; HttpOnly; SameSite=strict");
7+
}
8+
invalid:
9+
- |
10+
@RequestMapping(value = "/cookie3", method = "GET")
11+
public void setSecureHttponlyCookie(@RequestParam String value, HttpServletResponse response) {
12+
Cookie cookie = new Cookie("cookie", value);
13+
cookie.setSecure(true);
14+
cookie.setHttpOnly(true);
15+
response.addCookie(cookie);
16+
}
17+
@RequestMapping(value = "/cookie2", method = "GET")
18+
public void setSecureCookie(@RequestParam String value, HttpServletResponse response) {
19+
response.setHeader("Set-Cookie", "key=value; HttpOnly;");
20+
}

0 commit comments

Comments
 (0)