A page cannot freely read resources from another origin. The browser’s Same-Origin Policy provides that isolation; Cross-Origin Resource Sharing (CORS) lets a server declare which other origins may access its resources. CORS is a server-side permission expressed through HTTP response headers, not a switch a page can turn on for itself. section 2.1.3
Check the request before changing client code:
- For an API or asset exposed to another origin, configure the server’s CORS response headers. Access-Control-Allow-Origin identifies the allowed origin; other headers govern allowed methods, headers, and credentials. section 2.1.3
- For an ECMAScript module loaded from another origin, the server must send appropriate CORS headers or the browser blocks the request. section 5.5
- For a cross-origin fetch that needs cookies or HTTP credentials, use credentials: "include" only when the server supports it. The response needs a specific Access-Control-Allow-Origin value and Access-Control-Allow-Credentials: true; a wildcard origin will not work. section 12.1.3
Choose credentials deliberately
A request to /api/account can use fetch('/api/account', { credentials: 'same-origin' }). If an account API is on another origin and requires cookies, use fetch('https://api.example.com/account', { credentials: 'include' }) only with the required server headers. Use credentials: 'omit' when no credentials should be sent. section 12.1.3
Table 2.1 Security specification overview
| Mechanism | What it enforces / solves | When to use | How it’s set | Gotchas / notes |
SOP |
Isolates origins so one site can’t freely read/modify another site’s data. |
Always, it’s the browser’s default isolation model. |
Built-in (no config). |
Core to web security; use postMessage or server-mediated APIs for safe cross-origin comms. |
CORS |
Allows controlled cross-origin reads/writes from JS (fetch/XHR). |
Exposing APIs/assets to other origins. |
HTTP response headers: Access-Control-Allow-Origin, -Methods, -Headers, -Credentials. |
Must be set on the server. Wildcard |
CSP |
Restricts where scripts/styles/images/etc. can load from; mitigates XSS/code injection. |
Any app loading third-party content or handling user input. |
HTTP header Content-Security-Policy (or <meta http-equiv>). |
Prefer nonces/hashes over host allowlists; test in report-only mode first. |
HTTPS (TLS) |
Encrypts transport; required for powerful APIs and secure cookies. |
Always in production (and most dev flows). |
Server certificate + redirect HTTP→HTTPS; use HSTS where appropriate (a spec that forces your domain into HTTPS for every request). |
Needed for Service Workers, Geolocation, etc.; localhost is treated as secure for dev. |
Permissions |
User-mediated access to camera, mic, location, notifications, etc. |
Any feature gated by user consent. |
JS APIs trigger prompts; query via navigator.permissions. |
Request lazily with context; handle denial gracefully; users can revoke anytime. |
Compare CORS with the browser’s default same-origin isolation and with Content Security Policy: they address different security decisions. section 2.1.3







