90

This question is related to Cross-Origin Resource Sharing (CORS, http://www.w3.org/TR/cors/).

If there is an error when making a CORS request, Chrome (and AFAIK other browsers as well) logs an error to the error console. An example message may look like this:

XMLHttpRequest cannot load http://domain2.example. Origin http://domain1.example is not allowed by Access-Control-Allow-Origin.

I'm wondering if there's a way to programmatically get this error message? I've tried wrapping my xhr.send() call in try/catch, I've also tried adding an onerror() event handler. Neither of which receives the error message.

1
  • 1
    I doubt if this is possible, since it is a browser feature, beyond the scope of javascript. Besides that, it could cause a security hole that can be used for sniffing. Jan 30, 2011 at 18:59

1 Answer 1

48

See:

...as well as notes in XHR Level 2 about CORS:

The information is intentionally filtered.

Edit many months later: A followup comment here asked for "why"; the anchor in the first link was missing a few characters which made it hard to see what part of the document I was referring to.

It's a security thing - an attempt to avoid exposing information in HTTP headers which might be sensitive. The W3C link about CORS says:

User agents must filter out all response headers other than those that are a simple response header or of which the field name is an ASCII case-insensitive match for one of the values of the Access-Control-Expose-Headers headers (if any), before exposing response headers to APIs defined in CORS API specifications.

That passage includes links for "simple response header", which lists Cache-Control, Content-Language, Content-Type, Expires, Last-Modified and Pragma. So those get passed. The "Access-Control-Expose-Headers headers" part lets the remote server expose other headers too by listing them in there. See the W3C documentation for more information.

Remember you have one origin - let's say that's the web page you've loaded in your browser, running some bit of JavaScript - and the script is making a request to another origin, which isn't ordinarily allowed because malware can do nasty things that way. So, the browser, running the script and performing the HTTP requests on its behalf, acts as gatekeeper.

The browser looks at the response from that "other origin" server and, if it doesn't seem to be "taking part" in CORS - the required headers are missing or malformed - then we're in a position of no trust. We can't be sure that the script running locally is acting in good faith, since it seems to be trying to contact servers that aren't expecting to be contacted in this way. The browser certainly shouldn't "leak" any sensitive information from that remote server by just passing its entire response to the script without filtering - that would basically be allowing a cross-origin request, of sorts. An information disclosure vulnerability would arise.

This can make debugging difficult, but it's a security vs usability tradeoff where, since the "user" is a developer in this context, security is given significant priority.

6
  • 8
    This answer could be a bit better if someone explained why this is the case — i.e. why the body for 4xx responses is intentionally filtered. A quick look through the linked articles didn't help. Sep 4, 2012 at 18:35
  • 1
    Is this really good and necessary? The script does not get the error message. From that it can of course conclude that it is a CORS setting. What can it be? Origin? Header field? I guess the bad guys know. It is just the rest of us that do not know.
    – Leo
    Dec 9, 2014 at 6:08
  • This similar question has some more information about why you don't even get the courtesy of an error message: stackoverflow.com/questions/19325314/…
    – contrebis
    Sep 29, 2015 at 21:28
  • 6
    If we can't catch them, how can we detect them?
    – Eddie
    Sep 7, 2018 at 3:13
  • 2
    Programmatically, you can't detect them. As a debugging engineer, you have to look in your browser's console. This is intentional. See @contrebis reply above and the link to another StackOverflow question, scroll down & there's even more information on "why" than here. If you control the target server in question you can change its server-side implementation to provide some kind of indication of the failure nature in an HTTP header, but this would be unwise from a security standpoint. Oct 2, 2018 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.