1---
2name: cors
3description: Configure Cross-Origin Resource Sharing correctly to avoid security issues and debugging pain.
4metadata:
5 source: https://skillmd.com/skills/modbender/cors
6 clawdbot:
7 emoji: 🔀
8 os:
9 - linux
10 - darwin
11 - win32
12---
13
14## Preflight Triggers
15
16- Any header except: Accept, Accept-Language, Content-Language, Content-Type (with restrictions)
17- Content-Type other than: application/x-www-form-urlencoded, multipart/form-data, text/plain
18- Methods: PUT, DELETE, PATCH, or any custom method
19- ReadableStream in request body
20- Event listeners on XMLHttpRequest.upload
21- One trigger = preflight; simple requests skip OPTIONS entirely
22
23## Credentials Mode
24
25- `Access-Control-Allow-Origin: *` incompatible with credentials—must specify exact origin
26- `Access-Control-Allow-Credentials: true` required for cookies/auth headers
27- Fetch: `credentials: 'include'`; XHR: `withCredentials = true`
28- Without credentials mode, cookies not sent even to same origin for cross-origin requests
29
30## Wildcard Limitations
31
32- `*` doesn't match subdomains—`*.example.com` is invalid, not a pattern
33- Can't use `*` with credentials—specify origin dynamically from request
34- `Access-Control-Allow-Headers: *` works in most browsers but not all—list explicitly for compatibility
35- `Access-Control-Expose-Headers: *` same issue—list headers you need to expose
36
37## Origin Validation
38
39- Check Origin header against allowlist—don't reflect blindly (security risk)
40- Regex matching pitfall: `example.com` matches `evilexample.com`—anchor the pattern
41- `null` origin: sandboxed iframes, file:// URLs—usually reject, never allow as trusted
42- Missing Origin header: same-origin or non-browser client—handle explicitly
43
44## Vary Header (Critical)
45
46- Always include `Vary: Origin` when response depends on origin—even if you allow only one
47- Without Vary: CDN/proxy caches response for one origin, serves to others—breaks CORS
48- Add `Vary: Access-Control-Request-Headers, Access-Control-Request-Method` for preflight caching correctness
49
50## Exposed Headers
51
52- By default, JS can only read: Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma
53- Custom headers invisible to JS unless listed in `Access-Control-Expose-Headers`
54- `X-Request-ID`, `X-RateLimit-*`, etc. need explicit exposure—common oversight
55
56## Preflight Caching
57
58- `Access-Control-Max-Age: 86400` caches preflight for 24h—reduces OPTIONS traffic significantly
59- Chrome caps at 2 hours; Firefox at 24 hours—values above are silently reduced
60- Cached per origin + URL + request characteristics—not globally
61- Set to 0 or omit during development—caching hides config changes
62
63## Debugging
64
65- CORS error in browser = request reached server and came back—check server logs
66- Preflight failure: server must return 2xx with CORS headers on OPTIONS—404/500 = failure
67- Opaque response in fetch: `mode: 'no-cors'` succeeds but response is empty—usually not what you want
68- Network tab shows CORS errors; Console shows which header is missing
69
70## Common Server Mistakes
71
72- Only setting CORS headers on main handler, not OPTIONS—preflight fails
73- Setting headers after error response—CORS headers missing on 4xx/5xx breaks error handling
74- Proxy stripping headers—verify headers reach client, not just that server sets them
75- `Access-Control-Allow-Origin: "*", "https://example.com"`—must be single value, not list
76
77## Security
78
79- Don't reflect Origin header blindly—validate against allowlist first
80- Private Network Access: Chrome requires `Access-Control-Allow-Private-Network: true` for localhost access from public web
81- CORS doesn't prevent request from being sent—just blocks response reading; server still processes it
82- Sensitive endpoints: don't rely on CORS alone; use authentication + CSRF tokens