SKY-D231 · Security in code
Unsafe CORS configuration
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
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
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 responseWhat Skylos leaves alone
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
- Runs with
--dangeron Python files that mentioncors,originsor theAccess-Control-Allow-Originheader. - Reports a response header
Access-Control-Allow-Origin(set by subscript,.set(),.add()or a dict literal) whose value is the request's ownOriginheader, or a local holding it, unless an allowlist test protects it:origin in ALLOWEDororigin == "https://…"in an enclosingif, an earlierifthat returns, raises or resets the origin when the test fails, anany(origin == o for o in ALLOWED), afullmatchwith 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. - Reports flask-cors
CORS(…)and@cross_origin(…)with a wildcard origin andsupports_credentials=True, and Starlette or FastAPICORSMiddlewarewithallow_origins=["*"]or anallow_origin_regexsuch as.*andallow_credentials=True. - Reports a
CORS(…)call with nooriginsorresourcesargument (it allows every origin),CORS_ALLOW_ALL_ORIGINS = True(django-cors-headers), and"Access-Control-Allow-Origin": "*"in a dict or anACCESS_CONTROL_ALLOW_ORIGIN = "*"setting. - 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,testsorcorpusfolders, andtest_*.pyor*_test.pyfiles, are not searched for such routes. TypeScript and JavaScriptcors({ 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.
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 responseWhen 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
CORSwithout anoriginsorresourcesargument is reported, including a project's ownCORS(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.