Skip to content

Commit 0732798

Browse files
author
edvraa
committed
Single HttpOnly query
1 parent dea4d67 commit 0732798

File tree

27 files changed

+132
-516
lines changed

27 files changed

+132
-516
lines changed

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,31 @@ them not accessible to JavaScript.
2020
<example>
2121

2222
<p>
23-
In the example below to <code>Microsoft.AspNetCore.Http.CookieOptions.HttpOnly</code> is set to <code>true</code>.
23+
In the example below <code>Microsoft.AspNetCore.Http.CookieOptions.HttpOnly</code> is set to <code>true</code>.
2424
</p>
2525

2626
<sample src="httponlyflagcore.cs" />
2727

28+
<p>
29+
In the following example <code>CookiePolicyOptions</code> are set programmatically to configure defaults.
30+
</p>
31+
32+
<sample src="cookiepolicyoptions.cs" />
33+
34+
<p>
35+
In the example below <code>System.Web.HttpCookie.HttpOnly</code> is set to <code>true</code>.
36+
</p>
37+
38+
<sample src="httponlyflag.cs" />
39+
2840
</example>
2941

3042
<references>
3143

32-
<li>MSDN: <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.cookieoptions.httponly">CookieOptions.HttpOnly Property</a></li>
44+
<li><a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.cookieoptions.httponly">CookieOptions.HttpOnly Property</a></li>
3345
<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie">Set-Cookie</a> Header</li>
46+
<li><a href="https://msdn.microsoft.com/en-us/library/system.web.httpcookie.httponly(v=vs.110).aspx">HttpCookie.HttpOnly Property</a></li>
47+
<li><a href="https://msdn.microsoft.com/library/ms228262%28v=vs.100%29.aspx">httpCookies Element</a></li>
3448

3549
</references>
3650
</qhelp>

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

Lines changed: 99 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,114 @@
66
* not accessible by JavaScript.
77
* @kind problem
88
* @problem.severity warning
9-
* @precision medium
10-
* @id cs/web/httponly-possibly-not-set-aspnetcore
9+
* @precision high
10+
* @id cs/web/httponly-not-set
1111
* @tags security
1212
* external/cwe/cwe-1004
1313
*/
1414

1515
import csharp
16+
import semmle.code.asp.WebConfig
17+
import semmle.code.csharp.frameworks.system.Web
1618
import semmle.code.csharp.frameworks.microsoft.AspNetCore
1719
import semmle.code.csharp.dataflow.flowsources.AuthCookie
1820

19-
from Call c, MicrosoftAspNetCoreHttpResponseCookies iResponse, MethodCall mc
21+
from Expr httpOnlySink
2022
where
21-
// default is not configured or is not set to `Always`
22-
not getAValueForCookiePolicyProp("HttpOnly").getValue() = "1" and
23-
// there is no callback `OnAppendCookie` that sets `HttpOnly` to true
24-
not exists(
25-
OnAppendCookieHttpOnlyTrackingConfig config, DataFlow::Node source, DataFlow::Node sink
26-
|
27-
config.hasFlow(source, sink)
28-
) and
29-
iResponse.getAppendMethod() = mc.getTarget() and
30-
isCookieWithSensitiveName(mc.getArgument(0)) and
31-
(
32-
// `HttpOnly` property in `CookieOptions` passed to IResponseCookies.Append(...) wasn't set
33-
exists(ObjectCreation oc |
34-
oc = c and
35-
oc.getType() instanceof MicrosoftAspNetCoreHttpCookieOptions and
36-
not isPropertySet(oc, "HttpOnly") and
37-
exists(
38-
CookieOptionsTrackingConfiguration cookieTracking, DataFlow::Node creation,
39-
DataFlow::Node append
40-
|
41-
cookieTracking.hasFlow(creation, append) and
42-
creation.asExpr() = oc
23+
exists(Assignment a, Expr val |
24+
httpOnlySink = a.getRValue() and
25+
val.getValue() = "false" and
26+
(
27+
exists(ObjectCreation oc |
28+
getAValueForProp(oc, a, "HttpOnly") = val and
29+
(
30+
oc.getType() instanceof SystemWebHttpCookie and
31+
isCookieWithSensitiveName(oc.getArgument(0))
32+
or
33+
exists(MethodCall mc, MicrosoftAspNetCoreHttpResponseCookies iResponse |
34+
oc.getType() instanceof MicrosoftAspNetCoreHttpCookieOptions and
35+
iResponse.getAppendMethod() = mc.getTarget() and
36+
isCookieWithSensitiveName(mc.getArgument(0)) and
37+
// there is no callback `OnAppendCookie` that sets `HttpOnly` to true
38+
not exists(
39+
OnAppendCookieHttpOnlyTrackingConfig config, DataFlow::Node source,
40+
DataFlow::Node sink
41+
|
42+
config.hasFlow(source, sink)
43+
) and
44+
// Passed as third argument to `IResponseCookies.Append`
45+
exists(
46+
CookieOptionsTrackingConfiguration cookieTracking, DataFlow::Node creation,
47+
DataFlow::Node append
48+
|
49+
cookieTracking.hasFlow(creation, append) and
50+
creation.asExpr() = oc and
51+
append.asExpr() = mc.getArgument(2)
52+
)
53+
)
54+
)
55+
)
56+
or
57+
exists(PropertyWrite pw |
58+
(
59+
pw.getProperty().getDeclaringType() instanceof MicrosoftAspNetCoreHttpCookieBuilder or
60+
pw.getProperty().getDeclaringType() instanceof
61+
MicrosoftAspNetCoreAuthenticationCookiesCookieAuthenticationOptions
62+
) and
63+
pw.getProperty().getName() = "HttpOnly" and
64+
a.getLValue() = pw and
65+
DataFlow::localExprFlow(val, a.getRValue())
66+
)
67+
)
68+
)
69+
or
70+
exists(Call c |
71+
httpOnlySink = c and
72+
(
73+
exists(MicrosoftAspNetCoreHttpResponseCookies iResponse, MethodCall mc |
74+
// default is not configured or is not set to `Always`
75+
not getAValueForCookiePolicyProp("HttpOnly").getValue() = "1" and
76+
// there is no callback `OnAppendCookie` that sets `HttpOnly` to true
77+
not exists(
78+
OnAppendCookieHttpOnlyTrackingConfig config, DataFlow::Node source, DataFlow::Node sink
79+
|
80+
config.hasFlow(source, sink)
81+
) and
82+
iResponse.getAppendMethod() = mc.getTarget() and
83+
isCookieWithSensitiveName(mc.getArgument(0)) and
84+
(
85+
// `HttpOnly` property in `CookieOptions` passed to IResponseCookies.Append(...) wasn't set
86+
exists(ObjectCreation oc |
87+
oc = c and
88+
oc.getType() instanceof MicrosoftAspNetCoreHttpCookieOptions and
89+
not isPropertySet(oc, "HttpOnly") and
90+
exists(
91+
CookieOptionsTrackingConfiguration cookieTracking, DataFlow::Node creation,
92+
DataFlow::Node append
93+
|
94+
cookieTracking.hasFlow(creation, append) and
95+
creation.asExpr() = oc
96+
)
97+
)
98+
or
99+
// IResponseCookies.Append(String, String) was called, `HttpOnly` is set to `false` by default
100+
mc = c and
101+
mc.getNumberOfArguments() < 3
102+
)
103+
)
104+
or
105+
exists(ObjectCreation oc |
106+
oc = c and
107+
oc.getType() instanceof SystemWebHttpCookie and
108+
isCookieWithSensitiveName(oc.getArgument(0)) and
109+
// the property wasn't explicitly set, so a default value from config is used
110+
not isPropertySet(oc, "HttpOnly") and
111+
// the default in config is not set to `true`
112+
not exists(XMLElement element |
113+
element instanceof HttpCookiesElement and
114+
element.(HttpCookiesElement).isHttpOnlyCookies()
115+
)
43116
)
44117
)
45-
or
46-
// IResponseCookies.Append(String, String) was called, `HttpOnly` is set to `false` by default
47-
mc = c and
48-
mc.getNumberOfArguments() < 3 and
49-
isCookieWithSensitiveName(mc.getArgument(0))
50118
)
51-
select c, "Cookie attribute 'HttpOnly' is not set to true."
119+
select httpOnlySink, "Cookie attribute 'HttpOnly' is not set to true."

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)