Skip to content

[Query Create]: CWE-693, CWE-942 MEDIUM - Python: new query for CORS, CSP and security headers #282

Open

Description

@felickz

Target Language

python

Query Name (Optional)

CorsCspSecurityHeaders

Query Type

Security

Query Description

AI Scan reported 4 medium and low-severity CWE-693, CWE-942 (cors csp security headers) finding(s) in Python public synthetic repositories that the baseline CodeQL default-setup scan did not report. Gap categories: query-absent. Existing queries considered: none.

Why CodeQL appears to have missed these (unverified until reproduced with a test database):

  • CORSMiddleware with unanchored allow_origin_regex plus allow_credentials=True; no Python CORS misconfiguration query (JS has one).
  • Access-Control-Allow-Origin: * set on responses; no Python CORS misconfiguration query.
  • Header value containing unsafe-inline in CSP set in a Flask after_request could be matched syntactically.
  • Setting Access-Control-Allow-Origin to "*" in Flask/Django responses has no Python query (JS has cors-misconfiguration).

Linked findings (gap-key: python/query-create/cors-csp-security-headers)

Repo CWE Severity AI scan alert Summary
Damn-Vulnerable-RESTaurant-API-Game CWE-942 medium https://github.com/codeql-forks-felickz/Damn-Vulnerable-RESTaurant-API-Game/security/code-scanning/26 allow_origin_regex=".*.(restaurant.com/deliveryservice.com)" combined with allow_credentials=True and allow_methods=["*"]. The regex is not anchored to a sub-domain boundary and the dots are unescaped, so origins such as https://evilrestaurant.com, https://xrestaurant_com or https://attackerXrestaurantYcom match. Any such attacker-controlled origin can issue credentialed cross-origin requests to the API and read the responses. The pattern should be anchored and escaped (e.g. ^https://([a-z0-9-]+\.)?(restaurant\.com/deliveryservice\.com)$).
flask-webgoat CWE-942 medium https://github.com/codeql-forks-felickz/flask-webgoat/security/code-scanning/22 Every response is given Access-Control-Allow-Origin: *, allowing scripts on any website to read responses from the application's unauthenticated endpoints. This is particularly dangerous for deployments reachable only from a victim's browser or internal network, because a malicious origin can invoke and read results from sensitive endpoints such as process search.
shiftleft-python-demo CWE-693 low https://github.com/codeql-forks-felickz/shiftleft-python-demo/security/code-scanning/22 The global CSP includes 'unsafe-inline' in script-src. This negates CSP's protection against injected inline scripts and would allow an HTML injection flaw to execute JavaScript rather than being contained; nonce- or hash-based authorization should be used instead.
shiftleft-python-demo CWE-942 low https://github.com/codeql-forks-felickz/shiftleft-python-demo/security/code-scanning/21 Every response is assigned Access-Control-Allow-Origin: *. This allows scripts hosted on any attacker-controlled origin to read the application's unauthenticated responses, including user-search results and process-search output, and unnecessarily expands the exposure of an internal or locally hosted deployment. CORS should be limited to explicitly trusted origins and endpoints.

Findings come from AI Scan on public synthetic apps. Hypotheses above are unverified until reproduced with a test database. Verify first whether CodeQL already covers a case (for example via a different query or threat model) before adding code.

Handling decision

  • Type: Query Create
  • Decision: Create a new CodeQL query. No existing query covers these patterns (most findings are gap type query-absent or missing-query). Because coverage of lower-severity patterns can be heuristic, target Community Packs unless precision is shown to be high enough for github/codeql.
  • Recommended actions across the findings: write-new-query (4)

Patterns in this gap

  • permissive-cors-config (2 findings), for example Damn-Vulnerable-RESTaurant-API-Game app/init_app.py
  • weak-security-headers (1 finding), for example shiftleft-python-demo run.py
  • permissive-cors-python (1 finding), for example shiftleft-python-demo run.py

Finding details

Each finding below has its own context so the agent can work from this issue alone.

1. Damn-Vulnerable-RESTaurant-API-Game app/init_app.py:22 (CWE-942, medium)
  • AI Scan alert: https://github.com/codeql-forks-felickz/Damn-Vulnerable-RESTaurant-API-Game/security/code-scanning/26
  • AI Scan message: allow_origin_regex=".*.(restaurant.com|deliveryservice.com)" combined with allow_credentials=True and allow_methods=["*"]. The regex is not anchored to a sub-domain boundary and the dots are unescaped, so origins such as https://evilrestaurant.com, https://xrestaurant_com or https://attackerXrestaurantYcom match. Any such attacker-controlled origin can issue credentialed cross-origin requests to the API and read the responses. The pattern should be anchored and escaped (e.g. ^https://([a-z0-9-]+\.)?(restaurant\.com|deliveryservice\.com)$).
  • Why CodeQL appears to miss it: query-absent: CORSMiddleware with unanchored allow_origin_regex plus allow_credentials=True; no Python CORS misconfiguration query (JS has one).
  • Recommended action from the comparison: write-new-query (classification confidence: medium)
2. flask-webgoat run.py:8 (CWE-942, medium)
  • AI Scan alert: https://github.com/codeql-forks-felickz/flask-webgoat/security/code-scanning/22
  • AI Scan message: Every response is given Access-Control-Allow-Origin: *, allowing scripts on any website to read responses from the application's unauthenticated endpoints. This is particularly dangerous for deployments reachable only from a victim's browser or internal network, because a malicious origin can invoke and read results from sensitive endpoints such as process search.
  • Why CodeQL appears to miss it: query-absent: Access-Control-Allow-Origin: * set on responses; no Python CORS misconfiguration query.
  • Recommended action from the comparison: write-new-query (classification confidence: medium)
3. shiftleft-python-demo run.py:10 (CWE-693, low)
  • AI Scan alert: https://github.com/codeql-forks-felickz/shiftleft-python-demo/security/code-scanning/22
  • AI Scan message: The global CSP includes 'unsafe-inline' in script-src. This negates CSP's protection against injected inline scripts and would allow an HTML injection flaw to execute JavaScript rather than being contained; nonce- or hash-based authorization should be used instead.
  • Context for the agent: In shiftleft-python-demo run.py:10: The global CSP includes 'unsafe-inline' in script-src. This negates CSP's protection against injected inline scripts and would allow an HTML injection flaw to execute JavaScript rather than being contained; nonce- or hash-based authorization should be used Please re-validate whether CodeQL (existing queries none) misses this and what source/sink/model or query would be needed.
  • Why CodeQL appears to miss it: query-absent: Header value containing unsafe-inline in CSP set in a Flask after_request could be matched syntactically.
  • Recommended action from the comparison: write-new-query (classification confidence: medium)
4. shiftleft-python-demo run.py:8 (CWE-942, low)
  • AI Scan alert: https://github.com/codeql-forks-felickz/shiftleft-python-demo/security/code-scanning/21
  • AI Scan message: Every response is assigned Access-Control-Allow-Origin: *. This allows scripts hosted on any attacker-controlled origin to read the application's unauthenticated responses, including user-search results and process-search output, and unnecessarily expands the exposure of an internal or locally hosted deployment. CORS should be limited to explicitly trusted origins and endpoints.
  • Context for the agent: In shiftleft-python-demo run.py:8: Every response is assigned Access-Control-Allow-Origin: *. This allows scripts hosted on any attacker-controlled origin to read the application's unauthenticated responses, including user-search results and process-search output, and unnecessarily expands th Please re-validate whether CodeQL (existing queries none) misses this and what source/sink/model or query would be needed.
  • Why CodeQL appears to miss it: query-absent: Setting Access-Control-Allow-Origin to "*" in Flask/Django responses has no Python query (JS has cors-misconfiguration).
  • Recommended action from the comparison: write-new-query (classification confidence: high)

Expected Severity

Medium

Code Examples

See the linked alerts above for the vulnerable pattern; reduce each to a minimal test case with a safe counterpart that should not alert.

CWE/CVE Reference (Optional)

CWE-693, CWE-942

References (Optional)

https://cwe.mitre.org/data/definitions/693.html

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    enhancement
    severity:medium
    on Oct 31, 2026
  2. added
    precision:low
    tags:experimental
    on Nov 3, 2026
  3. felickz commented on Nov 3, 2026

    @felickz
    Author

    Upstream triage

    precision:low Audit-grade or weak signal. Community Packs, always tags:experimental. tags:experimental Goes to Community Packs: performance, false-positive or synthetic-only concerns.

    Community Packs, experimental. Recommended for the Community Packs repository, not github/codeql. The issue is labeled tags:experimental for this.

    Linked pull request: #332

    Why. One query bundling three checks. Access-Control-Allow-Origin '*' and CSP 'unsafe-inline' are widely intentional settings, so those branches are audit grade, and the origin-regex check relies on regexp heuristics that review found over- and under-matching. The allow_origins star with credentials branch duplicates the upstream experimental py/cors-misconfiguration-with-credentials. Community Packs fits, ideally as per-check queries.

    Change New query
    Target New py/cors-csp-security-headers: ACAO '*' header, CSP 'unsafe-inline', credentialed CORSMiddleware origins
    Precision assessed low (declared medium in the pull request). One query bundles three checks. ACAO '*' and CSP 'unsafe-inline' on constant header writes are audit-grade, often intentional settings. The origin-regex check uses regexp heuristics (unescaped dot, leading wildcard) that review found over- and under-matching.
    Real-world basis general. Public frameworks: Flask after_request and response headers, Django HttpResponse, FastAPI/Starlette CORSMiddleware. ACAO '*' and 'unsafe-inline' are common real settings, so the issue is noise, not synthetic-only logic.
    Stock overlap partial. Upstream experimental py/cors-misconfiguration-with-credentials already reports allow_origins star with credentials; header, CSP and origin-regex checks have no upstream query (gh api, source read).
    Concerns, overlap and next steps

    False-positive concerns

    • Access-Control-Allow-Origin '*' on public APIs, static assets or fonts without credentials is a common intentional setting and is reported
    • CSP 'unsafe-inline' is reported on any constant header write even if a proxy or another layer sets a stricter policy
    • script-src 'unsafe-inline' with script-src-elem and script-src-attr 'none' is still alerted (accepted limitation in review)
    • Equivalent safe origin regexes can be flagged because matching is by pattern shape, not by regex semantics

    Heuristics used

    • Regexps over CORS origin patterns (unescaped dot, leading wildcard, catch-all)
    • Regexp parsing of CSP header text for unsafe-inline, nonce and hash sources
    • Only constant header and argument values are analysed

    What an upstream change would look like. Community Packs: python/src/<category>/ split into per-check queries (ACAO header, CSP, credentialed origin regex) with tests under python/test. The credentialed allow_origin_regex check could instead extend the upstream experimental CORS query.

    Before opening an upstream pull request

    • Show a true positive on a real (non-synthetic) project for each check, especially allow_origin_regex with credentials
    • Split the three checks into separate queries so each has its own id, precision and tests
    • Remove the allow_origins star branch, which duplicates upstream py/cors-misconfiguration-with-credentials
    • Decide whether the ACAO '*' check should require credentials or sensitive routes to cut noise

    Split note. The credentialed allow_origin_regex branch is a candidate extension of the upstream experimental CORS query; the ACAO '*' and CSP branches are audit grade. The single query is rated at its lowest branch.

    This is a recommendation only. A human reviews it before any pull request is opened against github/codeql or the Community Packs repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CWE-693CWE-942enhancementprecision:lowquery-createseverity:mediumtags:experimental

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions