Skip to content

Commit dea4d67

Browse files
author
edvraa
committed
Extract to predicate isCookieWithSensitiveName
1 parent 7e723e9 commit dea4d67

File tree

5 files changed

+19
-29
lines changed

5 files changed

+19
-29
lines changed

csharp/ql/src/experimental/Security Features/CWE-1004/CookieHttpOnlyFalseAspNetCore.ql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ where
2929
|
3030
config.hasFlow(source, sink)
3131
) and
32-
// It is a sensitive cookie name
33-
exists(AuthCookieNameConfiguration dataflow, DataFlow::Node source, DataFlow::Node sink |
34-
dataflow.hasFlow(source, sink) and
35-
sink.asExpr() = mc.getArgument(0)
36-
) and
32+
isCookieWithSensitiveName(mc.getArgument(0)) and
3733
// Passed as third argument to `IResponseCookies.Append`
3834
exists(
3935
CookieOptionsTrackingConfiguration cookieTracking, DataFlow::Node creation,

csharp/ql/src/experimental/Security Features/CWE-1004/CookieHttpOnlyFalseSystemWeb.ql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,5 @@ where
2121
getAValueForProp(oc, a, "HttpOnly") = val and
2222
val.getValue() = "false" and
2323
oc.getType() instanceof SystemWebHttpCookie and
24-
// It is a sensitive cookie name
25-
exists(AuthCookieNameConfiguration dataflow, DataFlow::Node source, DataFlow::Node sink |
26-
dataflow.hasFlow(source, sink) and
27-
sink.asExpr() = oc.getArgument(0)
28-
)
24+
isCookieWithSensitiveName(oc.getArgument(0))
2925
select a.getRValue(), "Cookie attribute 'HttpOnly' is set to false."

csharp/ql/src/experimental/Security Features/CWE-1004/CookieWithoutHttpOnlyAspNetCore.ql

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ where
2626
|
2727
config.hasFlow(source, sink)
2828
) and
29-
// It is a sensitive cookie name
30-
exists(AuthCookieNameConfiguration dataflow, DataFlow::Node source, DataFlow::Node sink |
31-
iResponse.getAppendMethod() = mc.getTarget() and
32-
dataflow.hasFlow(source, sink) and
33-
sink.asExpr() = mc.getArgument(0)
34-
) and
29+
iResponse.getAppendMethod() = mc.getTarget() and
30+
isCookieWithSensitiveName(mc.getArgument(0)) and
3531
(
3632
// `HttpOnly` property in `CookieOptions` passed to IResponseCookies.Append(...) wasn't set
3733
exists(ObjectCreation oc |
@@ -48,12 +44,8 @@ where
4844
)
4945
or
5046
// IResponseCookies.Append(String, String) was called, `HttpOnly` is set to `false` by default
51-
exists(AuthCookieNameConfiguration dataflow, DataFlow::Node source, DataFlow::Node sink |
52-
mc = c and
53-
mc.getNumberOfArguments() < 3 and
54-
// It is a sensitive cookie name
55-
dataflow.hasFlow(source, sink) and
56-
sink.asExpr() = mc.getArgument(0)
57-
)
47+
mc = c and
48+
mc.getNumberOfArguments() < 3 and
49+
isCookieWithSensitiveName(mc.getArgument(0))
5850
)
5951
select c, "Cookie attribute 'HttpOnly' is not set to true."

csharp/ql/src/experimental/Security Features/CWE-1004/CookieWithoutHttpOnlySystemWeb.ql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,5 @@ where
2727
element instanceof HttpCookiesElement and
2828
element.(HttpCookiesElement).isHttpOnlyCookies()
2929
) and
30-
// it is a cookie with a sensitive name
31-
exists(AuthCookieNameConfiguration dataflow, DataFlow::Node source, DataFlow::Node sink |
32-
dataflow.hasFlow(source, sink) and
33-
sink.asExpr() = oc.getArgument(0)
34-
)
30+
isCookieWithSensitiveName(oc.getArgument(0))
3531
select oc, "Cookie attribute 'HttpOnly' is not set to true."

csharp/ql/src/semmle/code/csharp/dataflow/flowsources/AuthCookie.qll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
import csharp
66
import semmle.code.csharp.frameworks.microsoft.AspNetCore
77

8+
/**
9+
* Holds if the expression is a variable with a sensitive name.
10+
*/
11+
predicate isCookieWithSensitiveName(Expr cookieExpr) {
12+
exists(AuthCookieNameConfiguration dataflow, DataFlow::Node source, DataFlow::Node sink |
13+
dataflow.hasFlow(source, sink) and
14+
sink.asExpr() = cookieExpr
15+
)
16+
}
17+
818
/**
919
* Tracks if a variable with a sensitive name is used as an argument.
1020
*/
11-
class AuthCookieNameConfiguration extends DataFlow::Configuration {
21+
private class AuthCookieNameConfiguration extends DataFlow::Configuration {
1222
AuthCookieNameConfiguration() { this = "AuthCookieNameConfiguration" }
1323

1424
private predicate isAuthVariable(Expr expr) {

0 commit comments

Comments
 (0)