Skip to content

Commit 0b643e1

Browse files
Add tests for secure cookie using aspnetcore
1 parent dcf7e29 commit 0b643e1

File tree

17 files changed

+216
-6
lines changed

17 files changed

+216
-6
lines changed

csharp/ql/src/Security Features/CWE-614/CookieWithoutSecure.ql

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,12 @@ predicate insecureSecurePolicyAssignment(Assignment a, Expr val) {
104104
)
105105
}
106106

107-
from Expr secureSink, string msg
107+
from Expr secureSink
108108
where
109-
insecureCookieCall(secureSink) and
110-
msg = "Cookie attribute 'Secure' is not set to true."
109+
insecureCookieCall(secureSink)
111110
or
112111
exists(Assignment a |
113112
secureSink = a.getRValue() and
114113
insecureSecurePolicyAssignment(a, _)
115-
) and
116-
msg = "Cookie security policy sets cookies as insecure by default."
117-
select secureSink, msg
114+
)
115+
select secureSink, "Cookie attribute 'Secure' is not set to true."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| Program.cs:14:37:14:85 | access to constant None | Cookie attribute 'Secure' is not set to true. |
2+
| Program.cs:19:43:19:91 | access to constant None | Cookie attribute 'Secure' is not set to true. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security Features/CWE-614/CookieWithoutSecure.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Authentication;
6+
7+
public class Startup
8+
{
9+
public void ConfigureServices(IServiceCollection services)
10+
{
11+
services.AddAuthentication().AddCookie(o =>
12+
{
13+
o.Cookie.HttpOnly = false;
14+
o.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.None; // $ Alert
15+
});
16+
17+
services.AddSession(options =>
18+
{
19+
options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.None; // $ Alert
20+
options.Cookie.HttpOnly = false;
21+
});
22+
}
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Program.cs:5:9:5:48 | call to method Append | Cookie attribute 'Secure' is not set to true. |
2+
| Program.cs:10:29:10:73 | object creation of type CookieOptions | Cookie attribute 'Secure' is not set to true. |
3+
| Program.cs:35:29:35:73 | object creation of type CookieOptions | Cookie attribute 'Secure' is not set to true. |
4+
| Program.cs:42:29:42:92 | object creation of type CookieOptions | Cookie attribute 'Secure' is not set to true. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security Features/CWE-614/CookieWithoutSecure.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
public class MyController : Microsoft.AspNetCore.Mvc.Controller
2+
{
3+
public void CookieDefault()
4+
{
5+
Response.Cookies.Append("name", "value"); // $Alert // BAD: Secure is set to false by default
6+
}
7+
8+
public void CookieDefault2()
9+
{
10+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions(); // $Alert
11+
Response.Cookies.Append("name", "value", cookieOptions); // BAD: Secure is set to false by default
12+
}
13+
14+
public void CookieDelete()
15+
{
16+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions();
17+
Response.Cookies.Delete("name", cookieOptions); // GOOD: Delete call
18+
}
19+
20+
void CookieDirectTrue()
21+
{
22+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions();
23+
cookieOptions.Secure = true;
24+
Response.Cookies.Append("auth", "secret", cookieOptions); // GOOD
25+
}
26+
27+
void CookieDirectTrueInitializer()
28+
{
29+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions() { Secure = true };
30+
Response.Cookies.Append("auth", "secret", cookieOptions); // GOOD
31+
}
32+
33+
void CookieDirectFalse()
34+
{
35+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions(); // $Alert
36+
cookieOptions.Secure = false;
37+
Response.Cookies.Append("auth", "secret", cookieOptions); // BAD
38+
}
39+
40+
void CookieDirectFalseInitializer()
41+
{
42+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions() { Secure = false }; // $Alert
43+
Response.Cookies.Append("auth", "secret", cookieOptions); // BAD
44+
}
45+
46+
void CookieIntermediateTrue()
47+
{
48+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions();
49+
bool v = true;
50+
cookieOptions.Secure = v;
51+
Response.Cookies.Append("auth", "secret", cookieOptions); // GOOD: should track local data flow
52+
}
53+
54+
void CookieIntermediateTrueInitializer()
55+
{
56+
bool v = true;
57+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions() { Secure = v };
58+
Response.Cookies.Append("auth", "secret", cookieOptions); // GOOD: should track local data flow
59+
}
60+
61+
void CookieIntermediateFalse()
62+
{
63+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions(); // $MISSING:Alert
64+
bool v = false;
65+
cookieOptions.Secure = v;
66+
Response.Cookies.Append("auth", "secret", cookieOptions); // BAD, but not detected
67+
}
68+
69+
void CookieIntermediateFalseInitializer()
70+
{
71+
bool v = false;
72+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions() { Secure = v }; // $MISSING:Alert
73+
Response.Cookies.Append("auth", "secret", cookieOptions); // BAD, but not detected
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security Features/CWE-614/CookieWithoutSecure.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
4+
public class MyController : Microsoft.AspNetCore.Mvc.Controller
5+
{
6+
public void CookieDefault()
7+
{
8+
Response.Cookies.Append("auth", "secret"); // GOOD: Secure is set in policy
9+
}
10+
11+
public void CookieDefault2()
12+
{
13+
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions();
14+
Response.Cookies.Append("auth", "secret", cookieOptions); // GOOD: Secure is set in policy
15+
}
16+
}
17+
18+
public class Startup
19+
{
20+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
21+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
22+
{
23+
app.UseCookiePolicy(new CookiePolicyOptions() { Secure = Microsoft.AspNetCore.Http.CookieSecurePolicy.Always });
24+
}
25+
}

0 commit comments

Comments
 (0)