Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule for wildcard CORS in FastAPI #3137

Merged
merged 12 commits into from
Dec 13, 2023
46 changes: 46 additions & 0 deletions python/fastapi/security/wildcard-cors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = ["*"]


app.add_middleware(
CORSMiddleware,
# ruleid: wildcard-cors
allow_origins=origins,
allow_credentials=True,
allow=["*"]
)


app.add_middleware(
CORSMiddleware,
# ruleid: wildcard-cors
allow_origins=["*"],
allow_credentials=True,
allow=["*"]
)


app.add_middleware(
CORSMiddleware,
# ok: wildcard-cors
allow_origins=["https://github.com"],
allow_credentials=True,
allow=["*"]
)

app.add_middleware(
CORSMiddleware,
# ok: wildcard-cors
allow_origins=["https://github.com"],
allow_credentials=True,
allow=["www.semgrep.dev"]
)


@app.get("/")
async def main():
return {"message": "Hello Semgrep"}
37 changes: 37 additions & 0 deletions python/fastapi/security/wildcard-cors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
rules:
- id: wildcard-cors
languages:
- python
message: CORS policy allows any origin (using wildcard '*'). This is insecure
and should be avoided.
mode: taint
pattern-sources:
- pattern: '[..., "*", ...]'
pattern-sinks:
- patterns:
- pattern: |
$APP.add_middleware(
CORSMiddleware,
allow_origins=$ORIGIN,
...);
- focus-metavariable: $ORIGIN
severity: WARNING
metadata:
cwe:
- "CWE-942: Permissive Cross-domain Policy with Untrusted Domains"
owasp:
- A05:2021 - Security Misconfiguration
category: security
technology:
- python
- fastapi
references:
- https://owasp.org/Top10/A05_2021-Security_Misconfiguration
- https://cwe.mitre.org/data/definitions/942.html
likelihood: HIGH
impact: LOW
confidence: MEDIUM
vulnerability_class:
- Configuration
subcategory:
- vuln
Loading