liveBook · Manning

CORS

As explained in Vanilla Web

Cross-origin resource sharing is a web mechanism through which servers use HTTP headers to specify which other origins may access their resources.

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 * + credentials is not allowed.

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

How Vanilla Web covers it

How deep it goes
Defines the server-header requirement and explains credentialed fetch behavior, without a step-by-step server configuration.
Examples
Contrasts same-origin and cross-origin account fetches, and identifies a cross-origin module request blocked without CORS headers.
What it assumes
Uses origins, HTTP headers, fetch(), and ECMAScript modules without a from-scratch introduction to those APIs.
What you can do after
Operate/configure it by setting server response headers and selecting an appropriate fetch credentials mode for cross-origin requests.
How it connects
Connects the Same-Origin Policy and HTTP response headers to cross-origin ECMAScript module loading.

Also explained in

Compare how 8 books explain CORS →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.