On this page
If you have seen a browser block a cross-origin request, you may have wondered: what is an origin in a URL? For ordinary HTTP and HTTPS URLs, an origin is the combination of scheme, hostname, and port. This article explains how to compare origins and why that comparison matters for browser security and CORS.
Key Takeaways
- Two HTTP(S) URLs have the same origin only when their schemes, hosts, and effective ports match.
- Paths, query strings, and fragments do not change an HTTP(S) URL’s origin.
- An origin is more specific than a domain or site: changing a subdomain or port changes the origin.
- The same-origin policy restricts certain interactions between origins. CORS lets a server permit specific cross-origin browser access; neither replaces authentication or authorization.
The three parts of an origin
Consider this URL:
https://app.example.com:8443/dashboard?tab=account#details
\_____ \_____________/ \__/
scheme host port
Its origin is https://app.example.com:8443. The path, query, and fragment identify a resource or state within the URL, but they are not part of the origin.
Compare these URLs with https://example.com:
| URL | Same origin? | Reason |
|---|---|---|
https://example.com/products?sort=price | Yes | Only the path and query differ. |
http://example.com | No | The scheme differs. |
https://shop.example.com | No | The hostname differs. |
https://example.com:8443 | No | The port differs. |
https://example.com:443 | Yes | Port 443 is the default for HTTPS. |
Likewise, the default port for HTTP is 80. Browsers normalize default ports when determining HTTP(S) origins, so an explicit default port does not create a separate origin.
Advertisement
Origin vs. domain vs. site
A hostname such as shop.example.com identifies a host. People sometimes use “domain” loosely to mean either that hostname or a broader name such as example.com. Neither term by itself includes the scheme and port needed for an origin comparison.
A site is a different grouping used by some browser security and cookie rules. In the common schemeful-site model, it generally combines a scheme with the registrable domain. For example, https://app.example.com and https://shop.example.com are different origins but generally the same site. Ports distinguish origins but not sites in this model. Registrable domains depend on public-suffix rules, and special cases such as IP addresses need separate treatment.
Do not assume that being same-site makes two pages same-origin. Conversely, a cookie’s scope is not defined by origin: cookie domain, path, security, and SameSite rules apply, and cookies are not isolated by port.
Check an HTTP(S) origin in the browser
Open your browser’s developer tools console and use the built-in URL API:
const urls = [
'https://example.com/products',
'https://example.com:443/account',
'https://shop.example.com/',
'http://example.com/',
'https://example.com:8443/'
];
for (const value of urls) {
console.log(value, '→', new URL(value).origin);
}
The expected origin values, in order, are:
https://example.com
https://example.com
https://shop.example.com
http://example.com
https://example.com:8443
For these ordinary HTTP(S) URLs, compare the resulting origin strings to check whether they match. This snippet has not been executed for this article.
Opaque and inherited origins
Not every document has a scheme-host-port origin. Some documents have an opaque origin, such as a sandboxed iframe without the allow-same-origin permission. An opaque origin serializes as null, but two unrelated opaque origins are not necessarily the same origin. Do not treat two "null" strings as proof of shared origin.
Document origins can also be inherited in some situations, such as certain about:blank documents. Local file: URL handling is browser-dependent. Consequently, parsing a URL string with URL.origin is not a universal way to determine the origin of every document or execution context.
Advertisement
Why origins matter: same-origin policy and CORS
The same-origin policy restricts how a document or script from one origin can interact with resources from another. For example, JavaScript running at https://app.example.com generally cannot read a response from https://api.example.com unless the API permits that cross-origin access. The subdomain difference is enough to make them different origins.
There are important exceptions and details: browsers allow various cross-origin navigations and embeddings, and different APIs have their own rules. The policy is not a blanket prohibition on sending requests or loading resources across origins.
Cross-Origin Resource Sharing (CORS) is a mechanism for a server to tell a browser which cross-origin requests and response access it permits. For an allowed frontend origin, a response might include:
Access-Control-Allow-Origin: https://app.example.com
Vary: Origin
The allowed origin contains the scheme, host, and any non-default port—not a URL path. Vary: Origin is important when the server selects the allowed origin dynamically, so shared caches can distinguish responses for different requesting origins. Configure this only for origins you trust.
Some requests require a preflight: the browser first sends an OPTIONS request asking whether the intended method and headers are allowed. If the preflight fails, the browser does not send the intended request. Other cross-origin requests can reach the server even if CORS prevents JavaScript from reading the response. A CORS error therefore does not prove that no request reached the server.
For cross-origin requests with credentials, the client must opt in as appropriate—for example, with credentials: 'include' in fetch—and the server must allow credentials and specify the permitted origin rather than using * for Access-Control-Allow-Origin. Cookie settings and browser policies can still prevent cookies from being sent.
CORS is not an authentication system. It does not stop non-browser clients from calling an endpoint, and it is not a complete defense against cross-site request forgery. Authenticate and authorize sensitive operations, and apply CSRF protections where appropriate.
Common Mistakes
- Comparing only domain names.
http://example.comandhttps://example.comdiffer by scheme;https://example.com:8443differs by port. - Treating paths as origins.
/adminand/publicon the same scheme, host, and port share an origin. Use application authorization to protect sensitive paths. - Assuming sibling subdomains are same-origin. They may be same-site, but their hosts differ.
- Treating a CORS error as server-side access control. A browser may send a request even when it cannot expose the response to JavaScript.
- Comparing serialized
nullorigins as ordinary origins. Opaque origins require special care.
Best Practices
- Compare the full scheme, host, and effective port when debugging browser origin behavior.
- Use the browser console’s
URLAPI for ordinary HTTP(S) URL comparisons, but account separately for opaque or inherited document origins. - Allow only intended frontend origins in CORS configuration; avoid reflecting arbitrary
Originvalues. - Test both successful and rejected requests, including any preflight and credentialed request your application uses.
- Keep authentication, authorization, cookie policy, and CSRF protections separate from CORS decisions.
Conclusion
An HTTP(S) origin is the combination of scheme, host, and port. Comparing those three components explains many browser access errors, but secure applications also need server-side access controls and appropriate protections beyond CORS.
Browser documentation and standards could not be retrieved for this article. For further review, consult the WHATWG URL Standard and HTML Standard, plus MDN’s documentation on the same-origin policy and CORS.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.