Skip to content

Commit 5c9a3d5

Browse files
author
edvraa
committed
Single Secure query
1 parent 0732798 commit 5c9a3d5

File tree

21 files changed

+133
-304
lines changed

21 files changed

+133
-304
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Cross-Site Scripting (XSS) vulnerability the cookie can be stolen by malicious s
1313
<recommendation>
1414
<p>
1515
Protect sensitive cookies, such as related to authentication, by setting <code>HttpOnly</code> to <code>true</code> to make
16-
them not accessible to JavaScript.
16+
them not accessible to JavaScript. In ASP.NET case it is also possible to set the attribute via <code>&lt;httpCookies&gt;</code> element
17+
of <code>web.config</code> with the attribute <code>httpOnlyCookies="true"</code>.
1718
</p>
1819
</recommendation>
1920

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @kind problem
88
* @problem.severity warning
99
* @precision high
10-
* @id cs/web/httponly-not-set
10+
* @id cs/web/cookie-httponly-not-set
1111
* @tags security
1212
* external/cwe/cwe-1004
1313
*/

csharp/ql/src/experimental/Security Features/CWE-614/RequireSSLAspNetCore.qhelp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ cookies are sent via HTTP, not HTTPS.
1212

1313
<recommendation>
1414
<p>
15-
When using cookies ensure that HTTPS is used by setting the property <code>Microsoft.AspNetCore.Http.CookieOptions.Secure</code> to <code>true</code>.
15+
In ASP.NET case when using cookies ensure that HTTPS is used by setting the property <code>Microsoft.AspNetCore.Http.CookieOptions.Secure</code> to <code>true</code>.
16+
</p>
17+
<p>
18+
In ASP.NET Core case when using cookies, ensure that SSL is used, either via the <code>&lt;forms&gt;</code> attribute above, or
19+
the <code>&lt;httpCookies&gt;</code> element, with the attribute <code>requireSSL="true"</code>. It is also possible to require cookies
20+
to use SSL programmatically, by setting the property <code>System.Web.HttpCookie.Secure</code> to <code>true</code>.
1621
</p>
1722
</recommendation>
1823

1924
<example>
2025

2126
<p>
22-
In the example below to <code>Microsoft.AspNetCore.Http.CookieOptions.Secure</code> is set to <code>true</code> programmatically.
27+
In the example below <code>Microsoft.AspNetCore.Http.CookieOptions.Secure</code> is set to <code>true</code> programmatically.
2328
</p>
2429

2530
<sample src="secureflagcore.cs" />
@@ -30,12 +35,21 @@ In the following example <code>CookiePolicyOptions</code> are set programmatical
3035

3136
<sample src="cookiepolicyoptions.cs" />
3237

38+
<p>
39+
In the example below <code>System.Web.HttpCookie.Secure</code> is set to <code>true</code> programmatically.
40+
</p>
41+
42+
<sample src="secureflag.cs" />
43+
3344
</example>
3445

3546
<references>
3647

37-
<li>MSDN: <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.cookieoptions.secure">CookieOptions.Secure Property</a></li>
48+
<li><a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.cookieoptions.secure">CookieOptions.Secure Property</a></li>
3849
<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie">Set-Cookie</a> Header</li>
50+
<li><a href="https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.requiressl(v=vs.110).aspx">FormsAuthentication.RequireSSL Property</a></li>
51+
<li><a href="https://msdn.microsoft.com/en-us/library/1d3t3c61(v=vs.100).aspx">forms Element for authentication</a></li>
52+
<li><a href="https://msdn.microsoft.com/library/ms228262%28v=vs.100%29.aspx">httpCookies Element</a></li>
3953

4054
</references>
4155
</qhelp>

csharp/ql/src/experimental/Security Features/CWE-614/RequireSSLAspNetCore.ql

Lines changed: 95 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,113 @@
55
* is used at all times.
66
* @kind problem
77
* @problem.severity error
8-
* @precision medium
9-
* @id cs/web/cookie-secure-not-set-aspnetcore
8+
* @precision high
9+
* @id cs/web/cookie-secure-not-set
1010
* @tags security
1111
* external/cwe/cwe-319
1212
* external/cwe/cwe-614
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
21+
from Expr secureSink
2022
where
21-
// default is not configured or is not set to `Always` or `SameAsRequest`
22-
not (
23-
getAValueForCookiePolicyProp("Secure").getValue() = "0" or
24-
getAValueForCookiePolicyProp("Secure").getValue() = "1"
25-
) and
26-
// there is no callback `OnAppendCookie` that sets `Secure` to true
27-
not exists(OnAppendCookieSecureTrackingConfig config, DataFlow::Node source, DataFlow::Node sink |
28-
config.hasFlow(source, sink)
29-
) and
30-
(
31-
// `Secure` property in `CookieOptions` passed to IResponseCookies.Append(...) wasn't set
32-
exists(ObjectCreation oc |
33-
oc = c and
34-
oc.getType() instanceof MicrosoftAspNetCoreHttpCookieOptions and
35-
not isPropertySet(oc, "Secure") and
36-
exists(
37-
CookieOptionsTrackingConfiguration cookieTracking, DataFlow::Node creation,
38-
DataFlow::Node append
23+
exists(Call c |
24+
secureSink = c and
25+
(
26+
// default is not configured or is not set to `Always` or `SameAsRequest`
27+
not (
28+
getAValueForCookiePolicyProp("Secure").getValue() = "0" or
29+
getAValueForCookiePolicyProp("Secure").getValue() = "1"
30+
) and
31+
// there is no callback `OnAppendCookie` that sets `Secure` to true
32+
not exists(
33+
OnAppendCookieSecureTrackingConfig config, DataFlow::Node source, DataFlow::Node sink
3934
|
40-
cookieTracking.hasFlow(creation, append) and
41-
creation.asExpr() = oc
35+
config.hasFlow(source, sink)
36+
) and
37+
(
38+
// `Secure` property in `CookieOptions` passed to IResponseCookies.Append(...) wasn't set
39+
exists(ObjectCreation oc |
40+
oc = c and
41+
oc.getType() instanceof MicrosoftAspNetCoreHttpCookieOptions and
42+
not isPropertySet(oc, "Secure") and
43+
exists(
44+
CookieOptionsTrackingConfiguration cookieTracking, DataFlow::Node creation,
45+
DataFlow::Node append
46+
|
47+
cookieTracking.hasFlow(creation, append) and
48+
creation.asExpr() = oc
49+
)
50+
)
51+
or
52+
// IResponseCookies.Append(String, String) was called, `Secure` is set to `false` by default
53+
exists(MethodCall mc, MicrosoftAspNetCoreHttpResponseCookies iResponse |
54+
mc = c and
55+
iResponse.getAppendMethod() = mc.getTarget() and
56+
mc.getNumberOfArguments() < 3
57+
)
58+
)
59+
or
60+
exists(ObjectCreation oc |
61+
oc = c and
62+
oc.getType() instanceof SystemWebHttpCookie and
63+
// the property wasn't explicitly set, so a default value from config is used
64+
not isPropertySet(oc, "Secure") and
65+
// the default in config is not set to `true`
66+
not exists(XMLElement element |
67+
element instanceof FormsElement and
68+
element.(FormsElement).isRequireSSL()
69+
or
70+
element instanceof HttpCookiesElement and
71+
element.(HttpCookiesElement).isRequireSSL()
72+
)
4273
)
4374
)
44-
or
45-
// IResponseCookies.Append(String, String) was called, `Secure` is set to `false` by default
46-
exists(MethodCall mc, MicrosoftAspNetCoreHttpResponseCookies iResponse |
47-
mc = c and
48-
iResponse.getAppendMethod() = mc.getTarget() and
49-
mc.getNumberOfArguments() < 3
75+
)
76+
or
77+
exists(Assignment a, Expr val |
78+
secureSink = a.getRValue() and
79+
(
80+
exists(ObjectCreation oc |
81+
getAValueForProp(oc, a, "Secure") = val and
82+
val.getValue() = "false" and
83+
(
84+
oc.getType() instanceof SystemWebHttpCookie
85+
or
86+
oc.getType() instanceof MicrosoftAspNetCoreHttpCookieOptions and
87+
// there is no callback `OnAppendCookie` that sets `Secure` to true
88+
not exists(
89+
OnAppendCookieSecureTrackingConfig config, DataFlow::Node source, DataFlow::Node sink
90+
|
91+
config.hasFlow(source, sink)
92+
) and
93+
// the cookie option is passed to `Append`
94+
exists(
95+
CookieOptionsTrackingConfiguration cookieTracking, DataFlow::Node creation,
96+
DataFlow::Node append
97+
|
98+
cookieTracking.hasFlow(creation, append) and
99+
creation.asExpr() = oc
100+
)
101+
)
102+
)
103+
or
104+
exists(PropertyWrite pw |
105+
(
106+
pw.getProperty().getDeclaringType() instanceof MicrosoftAspNetCoreHttpCookieBuilder or
107+
pw.getProperty().getDeclaringType() instanceof
108+
MicrosoftAspNetCoreAuthenticationCookiesCookieAuthenticationOptions
109+
) and
110+
pw.getProperty().getName() = "SecurePolicy" and
111+
a.getLValue() = pw and
112+
DataFlow::localExprFlow(val, a.getRValue()) and
113+
val.getValue() = "2" // None
114+
)
50115
)
51116
)
52-
select c, "Cookie attribute 'Secure' is not set to true."
117+
select secureSink, "Cookie attribute 'Secure' is not set to true."

csharp/ql/src/experimental/Security Features/CWE-614/RequireSSLFalseAspNetCore.qhelp

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

csharp/ql/src/experimental/Security Features/CWE-614/RequireSSLFalseAspNetCore.ql

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

csharp/ql/src/experimental/Security Features/CWE-614/RequireSSLFalseSystemWeb.qhelp

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

csharp/ql/src/experimental/Security Features/CWE-614/RequireSSLFalseSystemWeb.ql

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

0 commit comments

Comments
 (0)