Dynamic CORS origins with credentials allowed

Dynamic CORS origins with credentials allowed

Description

If a server echoes an unverified Origin in Access-Control-Allow-Origin and returns Access-Control-Allow-Credentials: true, scripts from an untrusted origin may read an authenticated response. Whether cookies are sent depends on the client’s request options, SameSite settings and third-party cookie policy.

CORS controls which responses a browser exposes to scripts. Some cross-origin requests still reach the server even when it omits CORS headers.

Potential impact

An untrusted site may read private data, including responses from internal APIs reachable by the browser. If server-side authorization or CSRF checks also fail, data may be changed with the user’s permissions. An object-level authorization flaw (IDOR) can expose additional records.

Remediation

  • Match the request’s Origin against exact entries in a Set or Map. If you use a regex, match the complete scheme, host and port. Never copy a query or header value into Access-Control-Allow-Origin unchecked, or use null for a sensitive response.
  • For a trusted origin that needs credentials, return that origin and Access-Control-Allow-Credentials: true. For other origins, omit CORS permission or return 403. Include Vary: Origin when the response depends on the origin.
  • Check OPTIONS preflights against the same policy. Allow only the methods and headers the route needs and reject unsupported combinations.
  • Enforce authentication, authorization and CSRF checks on the server. Use SameSite to restrict cross-site cookies, Secure to require HTTPS and HttpOnly to prevent scripts from reading the cookie.

Examples

These excerpts show CORS handling for /profile. Authentication, authorization and CSRF checks are omitted.

Before

javascript
const express = require("express");
const app = express();

// Reflect the request Origin or query value and always allow credentials
app.use((req, res, next) => {
  const o = req.get("Origin") || req.query.o || "null";
  res.set("Access-Control-Allow-Origin", o);
  res.set("Access-Control-Allow-Credentials", "true");
  next();
});

app.get("/profile", (req, res) => {
  res.json({ email: "user@example.com" });
});

After

javascript
const express = require("express");
const app = express();

// Apply an exact allow-list and Vary: Origin
const ALLOWLIST = new Set([
  "https://app.example.com",
  "https://admin.example.com",
]);

app.use((req, res, next) => {
  const origin = req.get("Origin");
  if (origin && ALLOWLIST.has(origin)) {
    res.set("Access-Control-Allow-Origin", origin);
    res.set("Access-Control-Allow-Credentials", "true");
    res.set("Vary", "Origin");
  } else {
    // Omit CORS permissions for unmatched origins, or return 403
    // res.status(403).end(); // Optional, according to policy
  }
  next();
});

// Preflight example
app.options("/profile", (req, res) => {
  const origin = req.get("Origin");
  if (origin && ALLOWLIST.has(origin)) {
    res.set("Access-Control-Allow-Origin", origin);
    res.set("Access-Control-Allow-Credentials", "true");
    res.set("Access-Control-Allow-Methods", "GET");
    res.set("Access-Control-Allow-Headers", "Content-Type");
    res.set("Vary", "Origin");
    return res.sendStatus(204);
  }
  return res.sendStatus(403);
});

app.get("/profile", (req, res) => {
  res.json({ email: "user@example.com" });
});

The middleware grants credentialed reads to the two listed origins. An allowed preflight receives 204 and advertises GET and Content-Type. A preflight from any other origin receives 403.

References