Repository navigation
Draft
[NEW QUERY] Python: py/cors-csp-security-headers for permissive CORS and weak CSP (CWE-942, CWE-693)#332
Conversation
Open
1 task done
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
…sp-security-headers Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add new query for CORS, CSP and security headers
[NEW QUERY] Python: py/cors-csp-security-headers for permissive CORS and weak CSP (CWE-942, CWE-693)
Oct 31, 2026
There was a problem hiding this comment.
🟡 Changes recommended
CSP analysis mishandles directive precedence, combined policies, and 'strict-dynamic', while CORS regex checks misinterpret grouping, causing false positives and missed insecure configurations.
5 open issues
Ignore unsafe-inline when strict-dynamic is present · New Resolve script directive fallback before flagging inline permissions · New Intersect inline permissions across policies by execution type · New Split regex alternatives only at the top level · New Recognize grouped escaped-dot suffixes as safe domain boundaries · New
What changed in this PR
Adds the Python security query py/cors-csp-security-headers to address the CORS and CSP detection gaps described in #282, targeting Community Packs with medium-precision findings.
Changes:
- Detects permissive CORS response headers, credentialed middleware configurations, and CSP policies allowing unsafe inline scripts.
- Adds query-local modeling for response headers in Flask after-request hooks.
- Adds framework-specific fixtures, expected results, and query documentation.
| File | Description |
|---|---|
languages/python/custom/test/CorsCspSecurityHeaders/run.py |
Tests factory-created Flask apps and Blueprint hooks. |
languages/python/custom/test/CorsCspSecurityHeaders/flask_app.py |
Tests Flask header writes and CSP edge cases. |
languages/python/custom/test/CorsCspSecurityHeaders/fastapi_app.py |
Tests permissive and restricted CORS middleware configurations. |
languages/python/custom/test/CorsCspSecurityHeaders/django_views.py |
Tests Django response-header detection. |
languages/python/custom/test/CorsCspSecurityHeaders/CorsCspSecurityHeaders.qlref |
Connects the query to inline-expectation tests. |
languages/python/custom/test/CorsCspSecurityHeaders/CorsCspSecurityHeaders.expected |
Records expected query results. |
languages/python/custom/test/CorsCspSecurityHeaders/app_factory.py |
Supplies the cross-module Flask factory fixture. |
languages/python/custom/src/CorsCspSecurityHeaders/CorsCspSecurityHeaders.ql |
Implements detection logic and Flask hook modeling. |
languages/python/custom/src/CorsCspSecurityHeaders/CorsCspSecurityHeaders.md |
Documents coverage, remediation, and examples. |
Review effort: Balanced
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Open
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

📝 Query Information
py/cors-csp-security-headers@security-severity 5.3,@precision medium, so it targets Community Packs)🎯 Description
What This Query Detects
The default CodeQL setup missed four AI Scan findings in public synthetic Python apps. The cause was a combination of a missing query and missing library modeling:
Http::Server::ResponseHeaderWritealready models header writes, but nothing inspects the values.after_request/after_app_requestresponse parameter is not modeled in python-all 7.2.8. Writes such asresponse.headers[...] = vinside these hooks are notResponseHeaderWrites, so even a header-value query would miss both Flask findings.py/cors-misconfiguration-with-credentialsquery is narrow. It only checksallow_originscontaining*, ignoresallow_origin_regex, and is not in the default suite.This PR adds
languages/python/custom/src/CorsCspSecurityHeaders/CorsCspSecurityHeaders.ql. It reports the following.Access-Control-Allow-Originheader writes*ornullvalues.Content-Security-Policyheader writes that allow inline scripts'unsafe-inline'inscript-src,script-src-elemorscript-src-attr.'unsafe-inline'indefault-srcwhen noscript-srcdirective is present.'nonce-…'or'sha256|384|512-…'source, because browsers then ignore'unsafe-inline'.Starlette/FastAPI
CORSMiddlewarewithallow_credentials=TrueStarlette matches origins with
re.fullmatch. The query reports when either of the following holds:allow_origin_regexis permissive. That means one of:.*,https://.*,^https?://[^/]+$or…|.*;restaurant.com;.*example\.com;example\.com.*.The checks ignore the contents of character classes.
allow_originscontains"*".Library gap fix (local to the query)
FlaskAfterRequestResponseHeadersclass extendsWerkzeug::Headers::InstanceSourceto cover<param0>.headersin functions decorated with@<Flask app or Blueprint>.after_request/.after_app_request.create_app()factory in another module, which is the pattern in the affected repos.Example Vulnerable Code
Example Safe Code
🧪 Testing
Tests live in
languages/python/custom/test/CorsCspSecurityHeaders/and use inline expectations (# $ Alert):flask_app.pyafter_request,Response,headers.set, bulkmake_responseheaders, malformed nonce, multi-policy and duplicate-directive negativesrun.py+app_factory.pycreate_app()factory andBlueprint.after_app_request, plus an undecorated function as a negativedjango_views.pyresponse[...]andresponse.headers[...]fastapi_app.py📋 Checklist
.mdand.qhelp):.mdonly, no.qhelpReviewer notes
Access-Control-Allow-Origin: *is reported, as the issue requests, but that value is legitimate for intentionally public APIs. This is why precision is set tomedium.flask-cors(CORS(app, supports_credentials=True)) anddjango-cors-headerssettings.🔗 References
py/cors-misconfiguration-with-credentials(experimental),js/cors-misconfiguration-for-credentialsNote: This query was developed using Test-Driven Development methodology.