Skip to content

Commit 05ba854

Browse files
authored
Merge pull request #7 from GitHubSecurityLab/cors
feat(queries): Add CORS queries
2 parents a33b213 + 17e8b81 commit 05ba854

17 files changed

+142
-0
lines changed

ql/queries/security/CWE-942/InsecureCorsAllHeaders.ql

Whitespace-only changes.

ql/queries/security/CWE-942/InsecureCorsAllMethods.ql

Whitespace-only changes.

ql/queries/security/CWE-942/InsecureCorsAllowCredentialsWildcard.ql

Whitespace-only changes.

ql/queries/security/CWE-942/InsecureCorsWildcardOrigin.ql

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @name Insecure CORS: All Headers Allowed
3+
* @description Flags CORS policies that allow all headers ("*"), which can increase attack surface.
4+
* @kind problem
5+
* @problem.severity error
6+
* @security-severity 4.0
7+
* @precision high
8+
* @id bicep/insecure-cors-all-headers
9+
* @tags security
10+
* bicep
11+
*/
12+
13+
import bicep
14+
15+
from Network::CorsPolicy cors, Containers::ContainerResource resource
16+
where
17+
resource.getCorsPolicy() = cors and
18+
exists(Array headers | headers = cors.getAllowedHeaders() |
19+
exists(StringLiteral header | header = headers.getElements() | header.getValue() = "*")
20+
)
21+
select cors.getAllowedHeaders(),
22+
"CORS policy allows all headers (\"*\"), which can increase attack surface."
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @name Insecure CORS: All Methods Allowed
3+
* @description Flags CORS policies that allow all HTTP methods ("*") which can expose APIs to abuse.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id bicep/insecure-cors-all-methods
7+
*/
8+
9+
import bicep
10+
11+
from Network::CorsPolicy cors, Network::Ingress ingress, Resource resource
12+
where
13+
ingress.getCorsPolicy() = cors and
14+
exists(Array methods | methods = cors.getAllowedMethods() |
15+
exists(StringLiteral method | method = methods.getElements() | method.getValue() = "*")
16+
)
17+
select resource, "CORS policy allows all HTTP methods (\"*\"), which can expose APIs to abuse."
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @name Insecure CORS: AllowCredentials with Wildcard Origin
3+
* @description Flags CORS policies that allow credentials with a wildcard origin, which is insecure.
4+
* @kind problem
5+
* @problem.severity error
6+
* @id bicep/insecure-cors-allowcredentials-wildcard
7+
*/
8+
import bicep
9+
10+
from
11+
Network::CorsPolicy cors,
12+
Network::Ingress ingress,
13+
Resource resource
14+
where
15+
ingress.getCorsPolicy() = cors and
16+
cors.allowCredentials() = true and
17+
exists(Array origins | origins = cors.getAllowedOrigins() |
18+
exists(StringLiteral origin | origin = origins.getElements() | origin.getValue() = "*" )
19+
)
20+
select resource, "CORS policy allows credentials with a wildcard origin, which is insecure."
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Insecure CORS: Wildcard Origin
3+
* @description Flags CORS policies that allow any origin ("*"), which is insecure for sensitive APIs.
4+
* @kind problem
5+
* @problem.severity error
6+
* @security-severity 4.0
7+
* @precision high
8+
* @id bicep/insecure-cors-wildcard-origin
9+
* @tags security
10+
* bicep
11+
*/
12+
13+
import bicep
14+
15+
from Network::CorsPolicy cors, Network::Ingress ingress, Resource resource
16+
where
17+
ingress.getCorsPolicy() = cors and
18+
exists(Array origins | origins = cors.getAllowedOrigins() |
19+
exists(StringLiteral origin | origin = origins.getElements() | origin.getValue() = "*")
20+
)
21+
select resource, "CORS policy allows any origin (\"*\"), which is insecure for sensitive APIs."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| app.bicep:42:27:42:33 | Array | CORS policy allows all headers ("*"), which can increase attack surface. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
security/CWE-942/InsecureCorsAllHeaders.ql

ql/test/queries-tests/security/CWE-942/InsecureCorsAllMethods.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
security/CWE-942/InsecureCorsAllMethods.ql

ql/test/queries-tests/security/CWE-942/InsecureCorsAllowCredentialsWildcard.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
security/CWE-942/InsecureCorsAllowCredentialsWildcard.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| app.bicep:2:1:27:1 | ContainerResource | CORS policy allows any origin ("*"), which is insecure for sensitive APIs. |
2+
| app.bicep:30:1:55:1 | ContainerResource | CORS policy allows any origin ("*"), which is insecure for sensitive APIs. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
security/CWE-942/InsecureCorsWildcardOrigin.ql
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Secure CORS example: only specific headers allowed
2+
resource secureContainerApp 'Microsoft.App/containerApps@2022-03-01' = {
3+
name: 'secure-container-app'
4+
location: 'eastus'
5+
properties: {
6+
managedEnvironmentId: '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.App/managedEnvironments/my-env'
7+
configuration: {
8+
ingress: {
9+
external: true
10+
targetPort: 80
11+
corsPolicy: {
12+
allowCredentials: false
13+
allowedOrigins: [ 'https://example.com' ]
14+
allowedHeaders: [ 'Authorization', 'Content-Type' ]
15+
}
16+
}
17+
}
18+
template: {
19+
containers: [
20+
{
21+
name: 'app'
22+
image: 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest'
23+
}
24+
]
25+
}
26+
}
27+
}
28+
29+
// Insecure CORS example: all headers allowed (should be flagged)
30+
resource insecureContainerApp 'Microsoft.App/containerApps@2022-03-01' = {
31+
name: 'insecure-container-app'
32+
location: 'eastus'
33+
properties: {
34+
managedEnvironmentId: '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.App/managedEnvironments/my-env'
35+
configuration: {
36+
ingress: {
37+
external: true
38+
targetPort: 80
39+
corsPolicy: {
40+
allowCredentials: false
41+
allowedOrigins: [ '*' ]
42+
allowedHeaders: [ '*' ]
43+
}
44+
}
45+
}
46+
template: {
47+
containers: [
48+
{
49+
name: 'app'
50+
image: 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest'
51+
}
52+
]
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)