Skip to content

Commit f74741a

Browse files
spinscaleareek
authored andcommitted
CORS: Allowed to configure allow-credentials header to work via SSL
This adds support to return the "Access-Control-Allow-Credentials" header if needed, so CORS will work flawlessly with authenticated applications. Closes #6380
1 parent 3c280d5 commit f74741a

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

docs/reference/modules/http.asciidoc

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ be cached for. Defaults to `1728000` (20 days)
5757
|`http.cors.allow-headers` |Which headers to allow. Defaults to
5858
`X-Requested-With, Content-Type, Content-Length`.
5959

60+
|`http.cors.allow-credentials` | Whether the `Access-Control-Allow-Credentials`
61+
header should be returned. Note: This header is only returned, when the setting is
62+
set to `true`. Defaults to `false`
63+
6064

6165
|=======================================================================
6266

src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Set;
4444
import java.util.regex.Pattern;
4545

46+
import static org.elasticsearch.http.netty.NettyHttpServerTransport.*;
4647
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*;
4748

4849
/**
@@ -97,20 +98,24 @@ public void sendResponse(RestResponse response) {
9798
resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
9899
}
99100
if (RestUtils.isBrowser(nettyRequest.headers().get(USER_AGENT))) {
100-
if (transport.settings().getAsBoolean("http.cors.enabled", true)) {
101+
if (transport.settings().getAsBoolean(SETTING_CORS_ENABLED, true)) {
101102
String originHeader = request.header(ORIGIN);
102103
if (!Strings.isNullOrEmpty(originHeader)) {
103104
if (corsPattern == null) {
104-
resp.headers().add(ACCESS_CONTROL_ALLOW_ORIGIN, transport.settings().get("http.cors.allow-origin", "*"));
105+
resp.headers().add(ACCESS_CONTROL_ALLOW_ORIGIN, transport.settings().get(SETTING_CORS_ALLOW_ORIGIN, "*"));
105106
} else {
106107
resp.headers().add(ACCESS_CONTROL_ALLOW_ORIGIN, corsPattern.matcher(originHeader).matches() ? originHeader : "null");
107108
}
108109
}
109110
if (nettyRequest.getMethod() == HttpMethod.OPTIONS) {
110111
// Allow Ajax requests based on the CORS "preflight" request
111-
resp.headers().add(ACCESS_CONTROL_MAX_AGE, transport.settings().getAsInt("http.cors.max-age", 1728000));
112-
resp.headers().add(ACCESS_CONTROL_ALLOW_METHODS, transport.settings().get("http.cors.allow-methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE"));
113-
resp.headers().add(ACCESS_CONTROL_ALLOW_HEADERS, transport.settings().get("http.cors.allow-headers", "X-Requested-With, Content-Type, Content-Length"));
112+
resp.headers().add(ACCESS_CONTROL_MAX_AGE, transport.settings().getAsInt(SETTING_CORS_MAX_AGE, 1728000));
113+
resp.headers().add(ACCESS_CONTROL_ALLOW_METHODS, transport.settings().get(SETTING_CORS_ALLOW_METHODS, "OPTIONS, HEAD, GET, POST, PUT, DELETE"));
114+
resp.headers().add(ACCESS_CONTROL_ALLOW_HEADERS, transport.settings().get(SETTING_CORS_ALLOW_HEADERS, "X-Requested-With, Content-Type, Content-Length"));
115+
}
116+
117+
if (transport.settings().getAsBoolean(SETTING_CORS_ALLOW_CREDENTIALS, false)) {
118+
resp.headers().add(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
114119
}
115120
}
116121
}

src/main/java/org/elasticsearch/http/netty/NettyHttpServerTransport.java

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public class NettyHttpServerTransport extends AbstractLifecycleComponent<HttpSer
6565
NettyUtils.setup();
6666
}
6767

68+
public static final String SETTING_CORS_ENABLED = "http.cors.enabled";
69+
public static final String SETTING_CORS_ALLOW_ORIGIN = "http.cors.allow-origin";
70+
public static final String SETTING_CORS_MAX_AGE = "http.cors.max-age";
71+
public static final String SETTING_CORS_ALLOW_METHODS = "http.cors.allow-methods";
72+
public static final String SETTING_CORS_ALLOW_HEADERS = "http.cors.allow-headers";
73+
public static final String SETTING_CORS_ALLOW_CREDENTIALS = "http.cors.allow-credentials";
74+
6875
private final NetworkService networkService;
6976
final BigArrays bigArrays;
7077

src/test/java/org/elasticsearch/rest/CorsRegexDefaultTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public void testCorsSettingDefaultBehaviour() throws Exception {
3838
assertThat(response.getStatusCode(), is(200));
3939
assertThat(response.getHeaders(), hasKey("Access-Control-Allow-Origin"));
4040
assertThat(response.getHeaders().get("Access-Control-Allow-Origin"), is("*"));
41+
assertThat(response.getHeaders(), not(hasKey("Access-Control-Allow-Credentials")));
4142
}
4243

4344
@Test
@@ -46,5 +47,6 @@ public void testThatOmittingCorsHeaderDoesNotReturnAnything() throws Exception {
4647

4748
assertThat(response.getStatusCode(), is(200));
4849
assertThat(response.getHeaders(), not(hasKey("Access-Control-Allow-Origin")));
50+
assertThat(response.getHeaders(), not(hasKey("Access-Control-Allow-Credentials")));
4951
}
5052
}

src/test/java/org/elasticsearch/rest/CorsRegexTests.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@
3232

3333
import java.net.InetSocketAddress;
3434

35+
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ALLOW_ORIGIN;
36+
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ALLOW_CREDENTIALS;
3537
import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
3638
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope;
3739
import static org.hamcrest.Matchers.*;
40+
import static org.hamcrest.Matchers.is;
3841

3942
/**
4043
*
@@ -47,8 +50,8 @@ public class CorsRegexTests extends ElasticsearchIntegrationTest {
4750
@Override
4851
protected Settings nodeSettings(int nodeOrdinal) {
4952
return ImmutableSettings.settingsBuilder()
50-
.put("http.cors.allow-origin", "/https?:\\/\\/localhost(:[0-9]+)?/")
51-
.put("network.host", "127.0.0.1")
53+
.put(SETTING_CORS_ALLOW_ORIGIN, "/https?:\\/\\/localhost(:[0-9]+)?/")
54+
.put(SETTING_CORS_ALLOW_CREDENTIALS, "true")
5255
.put(super.nodeSettings(nodeOrdinal))
5356
.build();
5457
}
@@ -62,6 +65,8 @@ public void testThatRegularExpressionWorksOnMatch() throws Exception {
6265
corsValue = "https://localhost:9200";
6366
response = httpClient().method("GET").path("/").addHeader("User-Agent", "Mozilla Bar").addHeader("Origin", corsValue).execute();
6467
assertResponseWithOriginheader(response, corsValue);
68+
assertThat(response.getHeaders(), hasKey("Access-Control-Allow-Credentials"));
69+
assertThat(response.getHeaders().get("Access-Control-Allow-Credentials"), is("true"));
6570
}
6671

6772
@Test

0 commit comments

Comments
 (0)