Skip to main content

SKY-D231 · Security in code

Unsafe CORS configuration

A wildcard origin without credentials is reported only when the same module has state-changing routes that nothing authenticates: MEDIUM, or HIGH when one of those routes performs a privileged action. Every other finding is HIGH.

Cross-Origin Resource Sharing (CORS) headers tell the browser which other websites may call your API and read the answer. SKY-D231 reports Python web apps that allow every website: by echoing back the request's Origin header, by a * setting combined with cookies, or by a CORS setup with no origin list at all.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

When your API answers every origin with credentials allowed, any page a logged-in user visits can call it with their cookies and read the reply: their profile, orders or API keys, or change their settings.

What Skylos flags

app/main.py · PythonReported on line 9
from flask import Flask, request app = Flask(__name__)  @app.after_requestdef add_cors_headers(response):    origin = request.headers.get("Origin", "")    response.headers["Access-Control-Allow-Origin"] = origin    response.headers["Access-Control-Allow-Credentials"] = "true"    return response

What Skylos leaves alone

app/main.py · Python
from fastapi import FastAPIfrom fastapi.middleware.cors import CORSMiddleware app = FastAPI()app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["GET"])  @app.get("/status")def status():    return {"ok": True}

Why Skylos leaves it alone

A public, read-only status API may be readable by every site. The wildcard is used without allow_credentials=True, so browsers send no cookies, and the app has no state-changing route a page could call.

How Skylos decides

  1. Runs with --danger on Python files that mention cors, origins or the Access-Control-Allow-Origin header.
  2. Reports a response header Access-Control-Allow-Origin (set by subscript, .set(), .add() or a dict literal) whose value is the request's own Origin header, or a local holding it, unless an allowlist test protects it: origin in ALLOWED or origin == "https://…" in an enclosing if, an earlier if that returns, raises or resets the origin when the test fails, an any(origin == o for o in ALLOWED), a fullmatch with a pattern that has no .* or .+, or a same-module helper that returns one of those tests. An allowlist that is a single string, or contains "*" or "null", does not count.
  3. Reports flask-cors CORS(…) and @cross_origin(…) with a wildcard origin and supports_credentials=True, and Starlette or FastAPI CORSMiddleware with allow_origins=["*"] or an allow_origin_regex such as .* and allow_credentials=True.
  4. Reports a CORS(…) call with no origins or resources argument (it allows every origin), CORS_ALLOW_ALL_ORIGINS = True (django-cors-headers), and "Access-Control-Allow-Origin": "*" in a dict or an ACCESS_CONTROL_ALLOW_ORIGIN = "*" setting.
  5. A wildcard origin without credentials is reported only when the module creates an app with state-changing routes (POST, PUT, PATCH, DELETE) that nothing in the route, its body or the module authenticates. Files in test, tests or corpus folders, and test_*.py or *_test.py files, are not searched for such routes. TypeScript and JavaScript cors({ origin: "*" }) is SKY-D247.

How to fix it

Answer only origins you list. Compare the request's Origin with a set of your own sites before echoing it, add Vary: Origin so caches keep the answers apart, and never combine * with credentials. With flask-cors or CORSMiddleware, pass the explicit list of origins.

app/main.py · Python · the fix, against the flagged file
from flask import Flask, request app = Flask(__name__) Added: ALLOWED_ORIGINS = {"https://app.example.com", "https://admin.example.com"} Added:  @app.after_requestdef add_cors_headers(response):    origin = request.headers.get("Origin", "")Removed:     response.headers["Access-Control-Allow-Origin"] = originRemoved:     response.headers["Access-Control-Allow-Credentials"] = "true"Added:     if origin in ALLOWED_ORIGINS:Added:         response.headers["Access-Control-Allow-Origin"] = originAdded:         response.headers["Access-Control-Allow-Credentials"] = "true"Added:         response.headers["Vary"] = "Origin"    return response

When it can be wrong

  • An origin checked by a helper in another module, or by a test Skylos does not model (such as origin.endswith(".example.com")), is reported as echoed without a check.
  • Any call whose name ends in CORS without an origins or resources argument is reported, including a project's own CORS(app) wrapper that reads its origins from settings.
  • Only the file that holds the CORS setup is searched for open routes, so a wildcard without credentials is missed when the routes live in other modules.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Mark the reported line and say why every origin is acceptable.

    Python
    response.headers["Access-Control-Allow-Origin"] = origin  # skylos: ignore[SKY-D231] gateway
  • [tool.skylos] ignore = ["SKY-D231"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.