Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS and Origin header?

When we need to invoke an Ajax request we do :

if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest();
else
{
    var versions = ["Microsoft.XmlHttp",
            "MSXML2.XmlHttp",
            "MSXML2.XmlHttp.3.0",
            "MSXML2.XmlHttp.4.0",
            "MSXML2.XmlHttp.5.0"
    ];

I already know that using XMLHttpRequest-2 ,we can make a cross origin request AND that the ORIGIN header is added.

Question:

  • When does this header added ?

    • Is it added when a browser (that support CORS) is performing a request ? ( cross domain or non-cross-domain?)
    • Or is it added automatically when the browser "sees" that the request target origin is different from the current origin...

I mean : what the He** does the bold line mean ?

Cross-origin HTTP requests have an Origin header. This header provides the server with the request’s origin. This header is protected by the browser and cannot be changed from application code. In essence, it is the network equivalent of the origin property found on message events used in Cross Document Messaging. The origin header differs from the older referer [sic] header in that the referer is a complete URL including the path. Because the path may contain sensitive information, the referer is sometimes not sent by browsers attempting to protect user privacy. However, the browser will always send the required Origin headers when necessary.

like image 476
Royi Namir Avatar asked Apr 13 '13 13:04

Royi Namir


People also ask

Does CORS require a header?

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.

What is Origin header?

The Origin request header indicates the origin (scheme, hostname, and port) that caused the request. For example, if a user agent needs to request resources included in a page, or fetched by scripts that it executes, then the origin of the page may be included in the request.

What does Access-Control allow Origin header do?

What is the Access-Control-Allow-Origin response header? The Access-Control-Allow-Origin header is included in the response from one website to a request originating from another website, and identifies the permitted origin of the request.

What are the Cors headers in Cors?

The CORS standard has been updated with multiple headers to control resource sharing policies across multiple domains. Examples of these headers are included below: The Origin request header indicates where a fetch originates from. It doesn’t include any path information, only the server name.

What is Cors (Access Control Allow Origin header)?

Modern browsers use CORSin an API container- such as XMLHttpRequestor Fetch- to mitigate risks of cross-origin HTTP requests. How CORS works (Access-Control-Allow-Originheader) Wikipedia: The CORS standard describes new HTTP headers which provide browsers and servers a way to request remote URLs only when they have permission.

What is a “origin” Cors request?

A CORS request can be triggered by providing an additional header called “Origin” in the http request. Figure 1: Travel Website integrates data from various other applications. A misconfigured CORS policy comes with possible security vulnerabilities. Threat actors have been able to use it to obtain sensitive user data and steal bitcoin wallets.

What is the Cors header 1673?

1673 Access-Control-Allow-Originis a CORS (Cross-Origin Resource Sharing) header. When Site A tries to fetch content from Site B, Site B can send an Access-Control-Allow-Originresponse header to tell the browser that the content of this page is accessible to certain origins.


2 Answers

The Origin header

When this header is added ?

During the header's stage, before the document's body is sent (after open, before send).

Is it added when a browser (that support CORS) is doing a request ? ( cross domain or non-cross-domain?)

It is added when the origin doesn't match the page from which the XMLHttpRequest is created, but may also be sent in a same-origin request.

Or does it added automatically when the browser "sees" that the request target origin is different from the current origin...

Yes.

However, the browser will always send the required Origin headers when necessary.

This is part of the XMLHttpRequest spec; if you're making a cross-domain request, in the request headers an extra header is sent. This header is e.g. Origin: http://www.stackoverflow.com and is appended by a standards-following browser without user interaction.


You can read more on the specification in MozillaWiki's Security section, WHATWG and html5.org. It is implemented by (that I know of) FireFox and Google Chrome. I don't believe it is part of W3C yet. Further do not assume the origin header is true, as it can be set manually by modified borwsers or other software.

like image 66
Paul S. Avatar answered Oct 23 '22 12:10

Paul S.


The origin header is added automatically (generally) when you do a cross domain request.

To test it, I opened the console on this page and made two different requests: one for another domain and one for '/' and just the first got the origin header added.

BTW, I'm using JQuery for it and I'd really advise you to use it too in order to have the same behavior cross-browser.

For complementary info on the subject, check this:

The first thing to note is that a valid CORS request always contains an Origin header. This Origin header is added by the browser, and can not be controlled by the user. The value of this header is the scheme (e.g. http), domain (e.g. bob.com) and port (included only if it is not a default port, e.g. 81) from which the request originates; for example: http://api.alice.com.

The presence of the Origin header does not necessarily mean that the request is a cross-origin request. While all cross-origin requests will contain an Origin header, some same-origin requests might have one as well. For example, Firefox doesn't include an Origin header on same-origin requests. But Chrome and Safari include an Origin header on same-origin POST/PUT/DELETE requests (same-origin GET requests will not have an Origin header).

Source

like image 9
Robyflc Avatar answered Oct 23 '22 13:10

Robyflc