Skip to content

Commit ed2c778

Browse files
committed
Fixed tests
1 parent 44a1c3c commit ed2c778

7 files changed

+73
-22
lines changed

csharp/ql/src/experimental/CWE-942/CorsMisconfiguration.ql

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* @name CORS misconfiguration
3-
* @description Keeping an open CORS policy may result in security issues as third party website may be able to
4-
* access other websites.
2+
* @name Credentialed CORS Misconfiguration
3+
* @description Allowing any origin while allowing credentials may result in security issues as third party website may be able to
4+
* access private resources.
55
* @kind problem
66
* @problem.severity error
77
* @security-severity 7.5
88
* @precision high
9-
* @id cs/web/cors-misconfiguration
9+
* @id cs/web/cors-misconfiguration-credentials
1010
* @tags security
1111
* external/cwe/cwe-942
1212
*/
@@ -37,22 +37,22 @@ private predicate alwaysReturnsTrue(Callable c) {
3737
}
3838

3939
/**
40-
* Holds if the application uses a vulnerable CORS policy.
40+
* Holds if the application allows an origin using "*" origin.
4141
*/
42-
private predicate hasDangerousOrigins(MethodCall m) {
42+
private predicate allowAnyOrigin(MethodCall m) {
4343
m.getTarget()
4444
.hasFullyQualifiedName("Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder",
45-
"WithOrigins") and
46-
m.getAnArgument().getValue() = ["null", "*"]
45+
"AllowAnyOrigin")
4746
}
4847

4948
/**
50-
* Holds if the application allows an origin using "*" origin.
49+
* Holds if the application uses a vulnerable CORS policy.
5150
*/
52-
private predicate allowAnyOrigin(MethodCall m) {
51+
private predicate hasDangerousOrigins(MethodCall m) {
5352
m.getTarget()
5453
.hasFullyQualifiedName("Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder",
55-
"AllowAnyOrigin")
54+
"WithOrigins") and
55+
m.getAnArgument().getValue() = ["null", "*"]
5656
}
5757

5858
/**
@@ -64,25 +64,30 @@ private predicate configIsUsed(MethodCall add_policy) {
6464
.hasFullyQualifiedName("Microsoft.AspNetCore.Builder.CorsMiddlewareExtensions", "UseCors") and
6565
(
6666
uc.getArgument(1).getValue() = add_policy.getArgument(0).getValue() or
67+
uc.getArgument(1).(VariableAccess).getTarget() =
68+
add_policy.getArgument(0).(VariableAccess).getTarget() or
6769
localFlow(DataFlow::exprNode(add_policy.getArgument(0)), DataFlow::exprNode(uc.getArgument(1)))
6870
)
6971
)
7072
}
7173

72-
from MethodCall add_policy, MethodCall m
74+
from MethodCall add_policy, MethodCall m, MethodCall allowsCredentials
7375
where
7476
(
7577
add_policy
7678
.getTarget()
7779
.hasFullyQualifiedName("Microsoft.AspNetCore.Cors.Infrastructure.CorsOptions", "AddPolicy") and
7880
add_policy.getArgument(1) = m.getParent*() and
79-
configIsUsed(add_policy)
81+
configIsUsed(add_policy) and
82+
add_policy.getArgument(1) = allowsCredentials.getParent*()
8083
or
8184
add_policy
8285
.getTarget()
8386
.hasFullyQualifiedName("Microsoft.AspNetCore.Cors.Infrastructure.CorsOptions",
8487
"AddDefaultPolicy") and
85-
add_policy.getArgument(0) = m.getParent*()
88+
add_policy.getArgument(0) = m.getParent*() and
89+
add_policy.getArgument(0) = allowsCredentials.getParent*()
8690
) and
8791
(hasDangerousOrigins(m) or allowAnyOrigin(m) or functionAlwaysReturnsTrue(m))
88-
select add_policy, "The following CORS policy may be vulnerable to 3rd party websites"
92+
select add_policy,
93+
"The following CORS policy may allow credentialed requests from 3rd party websites"
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Mvc;
33
using System;
4+
using Microsoft.Extensions.DependencyInjection;
45

6+
7+
8+
public class Startup
9+
{
10+
    public void ConfigureServices(string[] args)
11+
    {
512
var builder = WebApplication.CreateBuilder(args);
613
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
714

815

916
builder.Services.AddCors(options =>
1017
{
11-
options.AddPolicy(MyAllowSpecificOrigins,
12-
policy =>
13-
{
14-
policy.SetIsOriginAllowed(test => true).AllowCredentials().AllowAnyHeader().AllowAnyMethod();
15-
});
18+
    options.AddPolicy(MyAllowSpecificOrigins,
19+
                      policy =>
20+
                      {
21+
                          policy.SetIsOriginAllowed(test => true).AllowCredentials().AllowAnyHeader().AllowAnyMethod();
22+
                      });
1623
});
1724

1825
var app = builder.Build();
@@ -22,4 +29,6 @@
2229
app.MapGet("/", () => "Hello World!");
2330
app.UseCors(MyAllowSpecificOrigins);
2431

25-
app.Run();
32+
app.Run();
33+
    }
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
7+
8+
public class Test
9+
{
10+
    public void ConfigureServices(string[] args)
11+
    {
12+
var builder = WebApplication.CreateBuilder(args);
13+
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
14+
15+
16+
builder.Services.AddCors(options =>
17+
{
18+
    options.AddPolicy(MyAllowSpecificOrigins,
19+
                      policy =>
20+
                      {
21+
                          policy.AllowAnyOrigin().AllowCredentials().AllowAnyHeader().AllowAnyMethod();
22+
                      });
23+
});
24+
25+
var app = builder.Build();
26+
27+
28+
29+
app.MapGet("/", () => "Hello World!");
30+
app.UseCors(MyAllowSpecificOrigins);
31+
32+
app.Run();
33+
    }
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| CorsMiconfigurationCredentials.cs:18:5:22:24 | call to method AddPolicy | The following CORS policy may allow credentialed requests from 3rd party websites |
2+
| CorsMisconfiguration.cs:18:5:22:24 | call to method AddPolicy | The following CORS policy may allow credentialed requests from 3rd party websites |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/CWE-942/CorsMisconfiguration.ql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| CorsMiconfigurationCredentials.cs:18:5:22:24 | call to method AddPolicy | The following CORS policy may allow credentialed requests from 3rd party websites |
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
semmle-extractor-options: /nostdlib /noconfig
22
semmle-extractor-options: --load-sources-from-project:${testdir}/../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj
33
semmle-extractor-options: --load-sources-from-project:../../resources/stubs/_frameworks/Microsoft.AspNetCore.App/Microsoft.AspNetCore.App.csproj
4-
semmle-extractor-options: --load-sources-from-project:../../resources/stubs/Microsoft.Extensions.DependencyInjection.Abstractions/6.0.0/Microsoft.Extensions.DependencyInjection.Abstractions.csproj
54

0 commit comments

Comments
 (0)