Origins, CORS and credentials
Quick answer
The browser same-origin policy restricts script access to cross-origin resources. CORS is a server opt-in protocol for sharing certain responses, not a client-side security bypass.
How it works
An origin includes scheme, host and port. Some cross-origin requests require an OPTIONS preflight. The server decides permitted origins, methods and headers. With credentials included, a credentialed CORS response needs an explicit allowed origin and permission for credentials; wildcard is not sufficient. no-cors yields an opaque response for applicable cross-origin fetches, not readable JSON. These examples inspect local objects only; actual CORS negotiation is NOT RUN.
Local examples
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {const a=new URL("https://example.test/a"),b=new URL("https://example.test:8443/b");console.log(a.origin===b.origin,b.port);})()Expected console output:
false 8443Different ports produce different origins.
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {const r=new Request("https://example.test/items",{mode:"cors",credentials:"include"});console.log(r.mode,r.credentials);})()Expected console output:
cors includeThis inspects intent, not a successful cross-origin exchange.
Common mistakes
Do not try to fix CORS by adding Access-Control-Allow-Origin to client request headers. Do not mistake CORS for authentication or CSRF protection.
Guided practice
Compare origins that differ by scheme or port, then document which server configuration would be needed for an authorized cross-origin API.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Origins, CORS and credentials — Knowledge Check
Apply the concept and reason about the example output.
Who grants CORS sharing permission?
Does no-cors make a cross-origin response readable as JSON?
What is included in an ordinary HTTPS origin?
What is the expected console output of Origins, CORS and credentials example 1?
(async () => {const a=new URL("https://example.test/a"),b=new URL("https://example.test:8443/b");console.log(a.origin===b.origin,b.port);})()What is the expected console output of Origins, CORS and credentials example 2?
(async () => {const r=new Request("https://example.test/items",{mode:"cors",credentials:"include"});console.log(r.mode,r.credentials);})()