-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CORS: Allowed to configure allow-credentials header to work via SSL #6380
Comments
/cc @spinscale @rashidkpc |
Hey, first, thanks a lot for such a long and already very well described issue, makes it easy for me to followup. Regarding sending the Origin headers, I just added #6923 to the master and 1.4 branch. I think this should already suffice you, right? If you wanted to allow everything, you could simply use the new regex feature and allow all origin headers. Your second request is not yet solved, as far as I read it at https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS?#Requests_with_credentials - there is nothing more to do, than providing this with another configuration option, right (so basically incorporating your suggestion)? I do get it correctly, that there is no real possibility on the server side, that credentials are sent, right? .There might be a cookie and there might be an auth header. As this is not my area of expertise, feel free to correct and I would be happy to get feedback on the above PR and if it solves your first problem, which I think it should. |
Hey @spinscale, The regex change works great for the Access-Control-Allow-Origin header! I tested it out and it resolves to the caller's host. I then get the next CORS warning about credentials. Adding this code gets it completely working. src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java line 100 if (transport.settings().getAsBoolean("http.cors.enabled", true)) {
/// ... existing checks
// new check starting at line 115
if (transport.settings().getAsBoolean("http.cors.allow-credentials", false)) {
resp.headers().add(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
}
} Then I set up my
Now running this JavaScript succeeds. It actually returns a 404 but this is the correct behavior since a var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://user:passwd@localhost:9200', true);
xhr.withCredentials = true;
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send('{"query":{"match_all":{}}}'); |
This adds support to return the "Access-Control-Allow-Credentials" header if needed, so CORS will work flawlessly with authenticated applications. Closes elastic#6380
This adds support to return the "Access-Control-Allow-Credentials" header if needed, so CORS will work flawlessly with authenticated applications. Closes #6380
This adds support to return the "Access-Control-Allow-Credentials" header if needed, so CORS will work flawlessly with authenticated applications. Closes #6380
+1 Spent a whole day thinking it was my fault on how I setup cors on Grafana. Could you please release this ASAP. |
happened to me too. I'm using 2.0. It was working fine. today I couldn't search and got "Request header field X-CSRF-TOKEN is not allowed by Access-Control-Allow-Headers in preflight response" |
@rachellji you just need to update your config to list that header |
Elasticsearch supports CORS and Basic Authentication but not together. A browser sending a cross-origin request will omit credentials unless the XHR
withCredentials
field is set totrue
. IfwithCredentials = true
there are two additional checks the browser must perform on the preflight response before sending the real request.Access-Control-Allow-Origin
must be the Origin (host), not*
Access-Control-Allow-Credentials: true
The W3C spec http://www.w3.org/TR/access-control/#resource-sharing-check-0 explains the CORS withCredentials check.
These headers can be added with 2 lines of code in
org.elasticsearch.http.netty.NettyHttpChannel.java
at line 95.This would make the
Access-Control-Allow-Origin
header fall back to theOrigin
header before falling back to*
. It would also add the new headerAccess-Control-Allow-Credentials: true
.Reproduce steps
localhost:9200
localhost:9200
. I'm using an empty Chrome tab in my example.Preflight Request
Preflight Response
The browser throws an error at this point saying the
Access-Control-Allow-Origin
andAccess-Control-Allow-Credentials
headers are bad.Notes
After you make the code changes the browser will start to cache preflight requests. When this happens you will only see the real request in Fiddler. To clear the cache, close the window and open a new one.
The XHR must be a non-simple request. This is a rather vague spec but a POST with a content type of application/json should do it.
IE skips most of these checks so don't bother testing in IE.
The example preflight request header
Authorization: Basic YWRtaW46d2VzdA==
is a bug in Chrome and shouldn't be included until the real request. There is a bug open for this here https://code.google.com/p/chromium/issues/detail?id=377541. Firefox works correctly and doesn't include the credentials on the preflight.The text was updated successfully, but these errors were encountered: