Skip to main content

SKY-D247 · Security in code

CORS middleware open to every origin

Medium

The cors package for Express and similar Node servers decides which other websites may call your API from a browser and read the answer. SKY-D247 reports cors({ origin: "*" }) and cors({ origin: true }), which allow every website.

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

Why it matters

Any page a user opens can then read your API's responses from their browser. With origin: true and credentials: true, the browser also sends the user's cookies, so another site can read their account data.

What Skylos flags

src/server.ts · TypeScriptReported on line 6
import cors from "cors";import express from "express"; const app = express(); app.use(cors({ origin: "*", methods: ["GET", "POST"] }));

What Skylos leaves alone

src/server.ts · TypeScript
import cors from "cors";import express from "express"; const app = express();const allowed = new Set(["https://app.example.com", "https://admin.example.com"]); app.use(  cors({    origin: (origin, callback) => callback(null, !origin || allowed.has(origin)),    credentials: true,  }),);

Why Skylos leaves it alone

origin is a function that accepts only the two listed sites (and requests with no Origin, such as server-to-server calls). The rule reports only the literal values "*" and true.

How Skylos decides

  1. Runs with --danger on TypeScript and JavaScript files. Finds calls to a function named exactly cors whose first argument is an object literal.
  2. Reports MEDIUM when that object has a property written origin whose value, with quotes removed, is * or true.
  3. Nothing else is read: cors() with no options (which the cors package also treats as "every origin"), options held in a variable, a quoted "origin" key, origin: ["*"] and an Access-Control-Allow-Origin header set by hand are not reported. Whether credentials are enabled does not change the result. Python CORS settings are SKY-D231.

How to fix it

List the sites that may call the API. Use an array of origins, or a function that checks the request's origin against a fixed set. Enable credentials only with such a list.

src/server.ts · TypeScript · the fix, against the flagged file
import cors from "cors";import express from "express"; const app = express(); Removed: app.use(cors({ origin: "*", methods: ["GET", "POST"] }));Added: app.use(Added:   cors({Added:     origin: ["https://app.example.com", "https://admin.example.com"],Added:     methods: ["GET", "POST"],Added:   }),Added: );

When it can be wrong

  • A public API that is meant to be read by every site, such as open data or a status endpoint with no cookies, is still reported.
  • app.use(cors()) with no options allows every origin but is not reported, nor is a configuration object built elsewhere and passed in as a variable.

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 line of the cors(…) call and say why every origin is acceptable.

    TypeScript
    app.use(cors({ origin: "*" })); // skylos: ignore[SKY-D247] public API
  • [tool.skylos] ignore = ["SKY-D247"] turns the rule off for the whole project.

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