Description
Distinct opaque origins all appear as null in the Origin header. Returning Access-Control-Allow-Origin: null with Access-Control-Allow-Credentials: true can let documents from those origins read sensitive responses if the client and cookie policy allow credentials to be sent.
CORS controls whether a browser exposes the response. The request may still reach the server.
Potential impact
A response to a request carrying session cookies may disclose account information, personal data or CSRF tokens. Internal and administrative APIs that trust null are particularly exposed when their authorization or CSRF checks are also weak.
Remediation
- For routes that require credentialed CORS, set
Access-Control-Allow-Originto an exact match from your list of trusted origins. Excludenull. The wildcard*cannot grant credentialed access. ReturnVary: Originwhen the response depends on the origin. - For other origins, omit both CORS permission headers and reject
OPTIONSpreflights with403. 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
The excerpts show CORS headers and preflights for /me. Authentication, authorization and CSRF checks are omitted.
Before
const express = require("express");
const app = express();
// Allow a null origin together with credentials
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "null"); // BAD
res.header("Access-Control-Allow-Credentials", "true"); // BAD
next();
});
app.get("/me", (req, res) => {
res.json({ user: "alice", email: "alice@example.com" });
});
After
const express = require("express");
const app = express();
const ALLOWLIST = new Set([
"https://app.example.com",
"https://admin.example.com",
]);
app.use((req, res, next) => {
const origin = req.headers.origin;
if (origin && ALLOWLIST.has(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Vary", "Origin");
} else {
// Unapproved origin: omit CORS permission and disallow credentialed reading
res.removeHeader("Access-Control-Allow-Credentials");
}
next();
});
// Preflight handling example
app.options("/me", (req, res) => {
const origin = req.headers.origin;
if (origin && ALLOWLIST.has(origin)) {
res.set({
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
Vary: "Origin",
});
return res.sendStatus(204);
}
return res.sendStatus(403);
});
app.get("/me", (req, res) => {
res.json({ user: "alice", email: "alice@example.com" });
});
/me grants credentialed reads to the two origins in ALLOWLIST. A preflight from null or any other unlisted origin receives 403.