-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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 With Basic Authentication #9063
Comments
@spinscale @bleskes what are your thoughts on this? |
@erikringsmuth the w3c spec for CORS are a bit ambiguous about whether the authorization header needs to be specified in the A.C.R.H. header. Because it is a header, one would assume it needs to be there, but on the other hand the explicitly say that Access-Control-Allow-Credentials header controls whether user credentials are allowed and it works for me with Chrome (which you seems to be using) on windows without it. Can you elaborate a bit more about what you did to make it fail without it? a demo html page which fails will be great. |
Here are the steps to reproduce what I'm seeing. You don't even need the server checking basic auth credentials. Simply adding them to the request will cause the browser (Chrome) to fail the request.
http.cors.enabled: true
http.cors.allow-origin: "/.*/"
http.cors.allow-credentials: true
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:9200/_search', true);
xhr.withCredentials = true;
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('test:test'));
xhr.send('{"query":{"match_all":{}}}'); It should fail with an error like this.
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"
Going back to my initial suggestion, parsing |
I see. If you indeed manually add the authorization header it doesn't work any more (remove it and the browser will respond to the 401 response code and popup a credentials window, after which every thing works without the authorization header). To make sure I understand what you are suggesting changing the semantics of http.cors.allow-headers to be a whitelist rather then a complete fixed list, so that ES will only echo requested headers, if allowed, but not output unneeded ones. Correct? |
The credentials window will pop up as long as you're on the same domain but a cross-origin AJAX request won't create the popup. I think there are two options for the change. The simplest change would just be adding The other option would be to parse the incoming Either way works. I don't think there are that many headers elasticsearch cares about so the simplest solution may be the best one for now. |
for what it's worth, it does pop up for me.
Do you actually use the autherization header in ES through a plugin? It feels a bit odd for me to add a header we don't use nor care about. If you have some proxy in front of ES, I would think that that is proper place for adding the header. |
Yeah, I'm using Basic Auth through a plugin. Maybe it's best to keep the headers as is and configure them with |
OK. Thanks for clarifying! |
wouldn't it be easier to just include this in the headers? i solved my issue by finding this thread. having them built in kinda just makes more sense. is there a downside im not seeing? |
pre-flight request is giving: 200 |
@ashutoshningot, Could you please ask you question on our discuss forum where we can provide better support. Those forums are designed to support the kinds of conversations that are necessary for this sort of troubleshooting and problem diagnosis. |
Elasticsearch populates the
Access-Control-Allow-Headers
header on CORS requests here in the NettyHttpChannel. This works as long as you don't send Basic Authentication credentials in theAuthorization
header.A CORS preflight request with Basic Authentication credentials looks like this.
response
The browser blocks the actual request from happening because
Authorization
is not in theAccess-Control-Allow-Headers
header.There is a workaround by setting this in
elasticsearch.yml
.This works but isn't ideal. I think the best solution would be for elasticsearch to look at the request's
Access-Control-Request-Headers
header and dynamically populate the responseAccess-Control-Allow-Headers
with a whitelisted set of headers.In this case, the request header
Access-Control-Request-Headers: authorization
should create a response headerAccess-Control-Allow-Headers: authorization
.If you think this is overkill I'll just stick with setting
http.cors.allow-headers
inelasticsearch.yml
.The text was updated successfully, but these errors were encountered: