Skip to content

Commit cabce5f

Browse files
authored
Merge pull request github#11549 from mbaluda/mbaluda/insecure-cookie
Java: Support interprocedural setting of cookie security
2 parents 321a2f5 + 8422df1 commit cabce5f

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query `java/insecure-cookie` now uses global dataflow to track secure cookies being set to the HTTP response object.

java/ql/src/Security/CWE/CWE-614/InsecureCookie.ql

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,31 @@ predicate isSafeSecureCookieSetting(Expr e) {
2626
)
2727
}
2828

29+
class SecureCookieConfiguration extends DataFlow::Configuration {
30+
SecureCookieConfiguration() { this = "SecureCookieConfiguration" }
31+
32+
override predicate isSource(DataFlow::Node source) {
33+
exists(MethodAccess ma, Method m | ma.getMethod() = m |
34+
m.getDeclaringType() instanceof TypeCookie and
35+
m.getName() = "setSecure" and
36+
source.asExpr() = ma.getQualifier() and
37+
forex(DataFlow::Node argSource |
38+
DataFlow::localFlow(argSource, DataFlow::exprNode(ma.getArgument(0))) and
39+
not DataFlow::localFlowStep(_, argSource)
40+
|
41+
isSafeSecureCookieSetting(argSource.asExpr())
42+
)
43+
)
44+
}
45+
46+
override predicate isSink(DataFlow::Node sink) {
47+
sink.asExpr() =
48+
any(MethodAccess add | add.getMethod() instanceof ResponseAddCookieMethod).getArgument(0)
49+
}
50+
}
51+
2952
from MethodAccess add
3053
where
3154
add.getMethod() instanceof ResponseAddCookieMethod and
32-
not exists(Variable cookie, MethodAccess m |
33-
add.getArgument(0) = cookie.getAnAccess() and
34-
m.getMethod().getName() = "setSecure" and
35-
forex(DataFlow::Node argSource |
36-
DataFlow::localFlow(argSource, DataFlow::exprNode(m.getArgument(0))) and
37-
not DataFlow::localFlowStep(_, argSource)
38-
|
39-
isSafeSecureCookieSetting(argSource.asExpr())
40-
) and
41-
m.getQualifier() = cookie.getAnAccess()
42-
)
55+
not any(SecureCookieConfiguration df).hasFlowToExpr(add.getArgument(0))
4356
select add, "Cookie is added to response without the 'secure' flag being set."

java/ql/test/query-tests/security/CWE-311/CWE-614/semmle/tests/Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,15 @@ public static void test(HttpServletRequest request, HttpServletResponse response
8484
response.addCookie(cookie);
8585
}
8686

87+
{
88+
// GOOD: set secure flag in call to `createSecureCookie`
89+
response.addCookie(createSecureCookie());
90+
}
91+
}
92+
93+
private static Cookie createSecureCookie() {
94+
Cookie cookie = new Cookie("secret", "fakesecret");
95+
cookie.setSecure(constTrue);
96+
return cookie;
8797
}
8898
}

0 commit comments

Comments
 (0)