Skip to content

Commit bd0eb32

Browse files
committed
CORS: Disable by default
In order to deliver a more secure out-of-the-box configuration this commit disables cross-origin resource sharing by default. Closes #7151
1 parent 789c0a9 commit bd0eb32

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

docs/reference/modules/http.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Defaults to `6`.
3939

4040
|`http.cors.enabled` |Enable or disable cross-origin resource sharing,
4141
i.e. whether a browser on another origin can do requests to
42-
Elasticsearch. Defaults to `true`.
42+
Elasticsearch. Defaults to `false`.
4343

4444
|`http.cors.allow-origin` |Which origins to allow. Defaults to `*`,
4545
i.e. any origin. If you prepend and append a `/` to the value, this will

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void sendResponse(RestResponse response) {
9696
resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
9797
}
9898
if (RestUtils.isBrowser(nettyRequest.headers().get(USER_AGENT))) {
99-
if (transport.settings().getAsBoolean(SETTING_CORS_ENABLED, true)) {
99+
if (transport.settings().getAsBoolean(SETTING_CORS_ENABLED, false)) {
100100
String originHeader = request.header(ORIGIN);
101101
if (!Strings.isNullOrEmpty(originHeader)) {
102102
if (corsPattern == null) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
public class CorsRegexDefaultTests extends ElasticsearchIntegrationTest {
3232

3333
@Test
34-
public void testCorsSettingDefaultBehaviour() throws Exception {
34+
public void testCorsSettingDefaultBehaviourDoesNotReturnAnything() throws Exception {
3535
String corsValue = "http://localhost:9200";
3636
HttpResponse response = httpClient().method("GET").path("/").addHeader("User-Agent", "Mozilla Bar").addHeader("Origin", corsValue).execute();
3737

3838
assertThat(response.getStatusCode(), is(200));
39-
assertThat(response.getHeaders(), hasKey("Access-Control-Allow-Origin"));
40-
assertThat(response.getHeaders().get("Access-Control-Allow-Origin"), is("*"));
39+
assertThat(response.getHeaders(), not(hasKey("Access-Control-Allow-Origin")));
4140
assertThat(response.getHeaders(), not(hasKey("Access-Control-Allow-Credentials")));
4241
}
4342

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ALLOW_ORIGIN;
3636
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ALLOW_CREDENTIALS;
37+
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ENABLED;
3738
import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
3839
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope;
3940
import static org.hamcrest.Matchers.*;
@@ -52,7 +53,8 @@ protected Settings nodeSettings(int nodeOrdinal) {
5253
return ImmutableSettings.settingsBuilder()
5354
.put(super.nodeSettings(nodeOrdinal))
5455
.put(SETTING_CORS_ALLOW_ORIGIN, "/https?:\\/\\/localhost(:[0-9]+)?/")
55-
.put(SETTING_CORS_ALLOW_CREDENTIALS, "true")
56+
.put(SETTING_CORS_ALLOW_CREDENTIALS, true)
57+
.put(SETTING_CORS_ENABLED, true)
5658
.build();
5759
}
5860

0 commit comments

Comments
 (0)