What is the same-origin policy?
Useful first: URLs · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
YOUR GOALDecide whether two ordinary web addresses share an origin.
Recognize the browser's boundary
A page does not get access to every open tab
Opening two websites in one browser does not give one page's code permission to inspect the other. The same-origin policy helps keep their data apart. For example, a page on https://news.example cannot simply read the document of your webmail page on another origin.
This is a browser access rule, not encryption. HTTPS protects the connection; it does not make unrelated pages share an origin.
[1] [2]Compare three parts of the address
For ordinary HTTP and HTTPS pages, compare the scheme, host and port. Starting from https://learn.example/lesson:
https://learn.example/quiz?mode=basic#answerhas the same origin: only the path, query and fragment differ.http://learn.example/lessonhas a different scheme.https://api.learn.example/lessonhas a different host.https://learn.example:8443/lessonhas a different port.
A different path is not a new security boundary. Review the parts of a URL if these labels are unfamiliar.
[2] [8]For ordinary web pages, compare scheme, host and port before expecting same-origin access.
Two addresses owned by the same company always have the same origin.
Ownership does not determine an origin. A different host, scheme or port can separate addresses belonging to one company.
Show the explanation
https://learn.example/quiz Only the path changes in the first option. The other options change the host or the port.
YOUR GOALExplain how a cross-origin request can succeed at the server but remain unreadable to page code.
Separate sending from reading
Cross-origin does not mean no traffic
Links, form submissions and many embedded images can cross origin boundaries. That does not give the initiating page general access to the destination's document or response data. Other browser and server policies can impose additional restrictions.
For example, a page may display a remote image without receiving permission to read that image's pixels through a canvas. Seeing content and programmatically reading it are different capabilities.
[1] [3]CORS permits a particular kind of sharing
Suppose a page at https://learn.example uses browser fetch() to read a public JSON lesson from https://api.example/lesson. A plain GET without special request headers can be sent without a CORS preflight. If the response lacks suitable CORS permission, the browser will not expose it to the calling code even if the server returned HTTP 200.
The API can opt in with the response header Access-Control-Allow-Origin: https://learn.example. CORS is this server-controlled sharing mechanism. It does not make the two origins equal or grant access to another page's document.
A blocked read does not undo an action
A form submission can cause a server-side action without letting the submitting page read the result. That is why a cookie-authenticated application needs appropriate defenses against cross-site request forgery, such as validated CSRF tokens for state-changing requests.
For example, refusing to expose a settings-change response would not undo a change the server already accepted. The server must check authorization and the legitimacy of the action itself; a CORS error is not a rollback.
[7]Ask two separate questions: was the request sent, and may page code read its response?
A CORS error proves the server never received the request.
Some requests are sent before response-sharing checks fail. A failed preflight can prevent a later request, but a blocked response read alone proves no such thing.
Show the explanation
The request reached the API, but fetch() rejects because the response cannot be shared. An HTTP success status does not grant cross-origin read access. The server may have handled the request while the browser withholds its response from the calling code.
YOUR GOALDiagnose origin, preflight and cookie decisions without treating CORS as authorization.
Trace CORS and credentials separately
Use parsed origins for ordinary HTTPS URLs
The default HTTPS port is normalized when an origin is serialized. These addresses therefore share an origin:
const lesson = new URL("https://learn.example:443/lesson");
const quiz = new URL("https://learn.example/quiz");
lesson.origin; // https://learn.example
lesson.origin === quiz.origin; // trueThe path never enters this comparison. This example is limited to ordinary HTTPS URLs: opaque origins, such as those used in some sandboxed documents, are not safely compared by treating the string null as a shared identity.
A preflight checks a proposed request
A cross-origin fetch that sends JSON with Content-Type: application/json requires CORS preflight permission. When no usable preflight result is cached, the browser first sends an OPTIONS request identifying the proposed method and relevant headers. Only after permission passes can it send the actual request; the actual response still needs appropriate CORS headers.
A preflight cache can avoid another OPTIONS exchange. Changing the client to mode: "no-cors" is not a way to obtain readable JSON: a cross-origin response in that mode is opaque, hiding its body and headers from page code and reporting status 0.
Cookie scope is a different decision
Cookie Domain controls which hosts may receive a cookie; omitting it creates a host-only cookie. A valid parent-domain cookie can cover sibling subdomains, but that does not make those subdomains same-origin. SameSite uses a site boundary, which differs from an origin boundary: HTTPS sibling subdomains of the same registrable domain can be same-site while remaining cross-origin.
Browser fetch defaults to credentials: "same-origin". A page at https://learn.example.com fetching https://api.example.com/lesson therefore needs credentials: "include" to make eligible cookies available on that cross-origin request. Domain matching alone is insufficient; cookie attributes and browser privacy policies still apply. See cookies for those controls.
Permission to read is not permission to act
For that credentialed fetch's response to be readable, the API must return both Access-Control-Allow-Origin: https://learn.example.com and Access-Control-Allow-Credentials: true. With credentials mode set to include, an allowed origin of * does not work, even if no cookie happens to be available.
These headers express a response-sharing policy. They do not establish who a user is or what that user may do. The API still needs its own authentication, authorization and applicable CSRF defenses. Configure only origins intended to receive the data.
[4] [7]Check origin equality, preflight requirements, credential mode and response permission as separate steps.
A shared parent-domain cookie makes CORS unnecessary between sibling subdomains.
Cookie scope does not merge origins. A cross-origin credentialed fetch must satisfy both credential/cookie rules and CORS response-sharing rules.
Show the explanation
No; include mode requires a matching explicit allowed origin, not *. The wildcard is invalid for response sharing when the request's credentials mode is include. A matching explicit origin and Access-Control-Allow-Credentials: true are required, while cookie and server authorization rules remain separate.
Keep asking. Check the details.
Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.
No human review is claimed. AI review can miss errors; check the original sources below.
- MDN — Same-origin policy
- WHATWG HTML — Origins and sites
- MDN — Cross-Origin Resource Sharing (CORS)
- WHATWG Fetch — CORS protocol and credentials
- MDN — Using the Fetch API
- MDN — Set-Cookie header
- MDN — Cross-site request forgery (CSRF)
- MDN — URL.origin