WSS
Web Specification Studio Home

Enterprise CORS & Access-Control Laboratory

Diagnose cross-origin request barriers, evaluate preflight requirements, and audit Private Network Access (PNA) compliance under WHATWG Fetch Standard §3.2.

1Client Request (Browser Context)

Originator

2Server Response Headers

Gateway / Edge
CORS Allowed

Request Meets Specification Requirements

Preflight Required

The browser will successfully dispatch preflight OPTIONS and expose the downstream response to client scripts.

Terminal Verification (cURL Reproducer)

curl -i -X OPTIONS \
  "https://api.example.com/v1/users" \
  -H "Origin: https://app.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: Content-Type"

Enterprise Configuration Exporter

// Cloudflare Worker CORS Middleware
const ALLOWED_ORIGINS = new Set([
  "https://app.example.com",
  "https://production.example.com",
]);

export default {
  async fetch(request, env, ctx) {
    const origin = request.headers.get("Origin");
    const isAllowed = origin && ALLOWED_ORIGINS.has(origin);

    // Handle Preflight OPTIONS
    if (request.method === "OPTIONS") {
      if (!isAllowed) {
        return new Response(null, { status: 403 });
      }
      return new Response(null, {
        status: 204,
        headers: {
          "Access-Control-Allow-Origin": origin,
          "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
          "Access-Control-Allow-Headers": "Content-Type, Authorization",
          "Access-Control-Allow-Credentials": "true",
          "Access-Control-Max-Age": "86400",
          "Vary": "Origin",
        },
      });
    }

    // Process downstream request
    const response = await fetch(request);
    const newHeaders = new Headers(response.headers);
    if (isAllowed) {
      newHeaders.set("Access-Control-Allow-Origin", origin);
      newHeaders.set("Access-Control-Allow-Credentials", "true");
      newHeaders.set("Vary", "Origin");
    }
    return new Response(response.body, {
      status: response.status,
      statusText: response.statusText,
      headers: newHeaders,
    });
  },
};

Enterprise CORS & Security Standards FAQ

Why does the browser say CORS error when the server returned HTTP 500 or 502?

When an upstream microservice fails or an API Gateway returns 502 Bad Gateway, reverse proxies frequently drop response headers. Because Access-Control-Allow-Origin is omitted from the error response, the browser discards the payload and reports a misleading CORS error instead of showing the server error.

Why is Access-Control-Allow-Origin: * rejected when credentials mode is include?

The WHATWG Fetch specification strictly prohibits wildcard origins with credentials to prevent universal session token exfiltration. If a server allowed wildcard with credentials, any malicious website could make authenticated background requests and read private user data.

What is W3C Private Network Access (PNA)?

Private Network Access is a security specification preventing public websites from dispatching unauthorized HTTP requests to local devices, intranet routers, or localhost (RFC 1918 addresses). Servers on internal networks must explicitly return Access-Control-Allow-Private-Network: true.

How does preflight caching with Access-Control-Max-Age work?

Access-Control-Max-Age informs the browser how many seconds it may cache the preflight OPTIONS response for that specific URL. Chromium caps this value at 7200 seconds (2 hours), while Firefox allows up to 86400 seconds (24 hours).

Try: