forked from elastic/elasticsearch
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORS: Support regular expressions for origin to match against
This commit adds regular expression support for the allow-origin header depending on the value of the request `Origin` header. The existing HttpRequestBuilder is also extended to support the OPTIONS HTTP method. Relates elastic#5601 Closes elastic#6891
- Loading branch information
Showing
8 changed files
with
248 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/org/elasticsearch/rest/CorsRegexDefaultTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.rest; | ||
|
||
import org.elasticsearch.test.ElasticsearchIntegrationTest; | ||
import org.elasticsearch.test.rest.client.http.HttpResponse; | ||
import org.junit.Test; | ||
|
||
import static org.elasticsearch.rest.CorsRegexTests.httpClient; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
/** | ||
* | ||
*/ | ||
public class CorsRegexDefaultTests extends ElasticsearchIntegrationTest { | ||
|
||
@Test | ||
public void testCorsSettingDefaultBehaviour() throws Exception { | ||
String corsValue = "http://localhost:9200"; | ||
HttpResponse response = httpClient().method("GET").path("/").addHeader("User-Agent", "Mozilla Bar").addHeader("Origin", corsValue).execute(); | ||
|
||
assertThat(response.getStatusCode(), is(200)); | ||
assertThat(response.getHeaders(), hasKey("Access-Control-Allow-Origin")); | ||
assertThat(response.getHeaders().get("Access-Control-Allow-Origin"), is("*")); | ||
} | ||
|
||
@Test | ||
public void testThatOmittingCorsHeaderDoesNotReturnAnything() throws Exception { | ||
HttpResponse response = httpClient().method("GET").path("/").execute(); | ||
|
||
assertThat(response.getStatusCode(), is(200)); | ||
assertThat(response.getHeaders(), not(hasKey("Access-Control-Allow-Origin"))); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/test/java/org/elasticsearch/rest/CorsRegexTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.rest; | ||
|
||
import org.apache.http.impl.client.HttpClients; | ||
import org.elasticsearch.common.logging.ESLogger; | ||
import org.elasticsearch.common.logging.Loggers; | ||
import org.elasticsearch.common.settings.ImmutableSettings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.transport.InetSocketTransportAddress; | ||
import org.elasticsearch.http.HttpServerTransport; | ||
import org.elasticsearch.test.ElasticsearchIntegrationTest; | ||
import org.elasticsearch.test.rest.client.http.HttpRequestBuilder; | ||
import org.elasticsearch.test.rest.client.http.HttpResponse; | ||
import org.junit.Test; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; | ||
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
/** | ||
* | ||
*/ | ||
@ClusterScope(scope = Scope.SUITE, numDataNodes = 1) | ||
public class CorsRegexTests extends ElasticsearchIntegrationTest { | ||
|
||
protected static final ESLogger logger = Loggers.getLogger(CorsRegexTests.class); | ||
|
||
@Override | ||
protected Settings nodeSettings(int nodeOrdinal) { | ||
return ImmutableSettings.settingsBuilder() | ||
.put("http.cors.allow-origin", "/https?:\\/\\/localhost(:[0-9]+)?/") | ||
.put("network.host", "127.0.0.1") | ||
.put(super.nodeSettings(nodeOrdinal)) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void testThatRegularExpressionWorksOnMatch() throws Exception { | ||
String corsValue = "http://localhost:9200"; | ||
HttpResponse response = httpClient().method("GET").path("/").addHeader("User-Agent", "Mozilla Bar").addHeader("Origin", corsValue).execute(); | ||
assertResponseWithOriginheader(response, corsValue); | ||
|
||
corsValue = "https://localhost:9200"; | ||
response = httpClient().method("GET").path("/").addHeader("User-Agent", "Mozilla Bar").addHeader("Origin", corsValue).execute(); | ||
assertResponseWithOriginheader(response, corsValue); | ||
} | ||
|
||
@Test | ||
public void testThatRegularExpressionReturnsNullOnNonMatch() throws Exception { | ||
HttpResponse response = httpClient().method("GET").path("/").addHeader("User-Agent", "Mozilla Bar").addHeader("Origin", "http://evil-host:9200").execute(); | ||
assertResponseWithOriginheader(response, "null"); | ||
} | ||
|
||
@Test | ||
public void testThatSendingNoOriginHeaderReturnsNoAccessControlHeader() throws Exception { | ||
HttpResponse response = httpClient().method("GET").path("/").addHeader("User-Agent", "Mozilla Bar").execute(); | ||
assertThat(response.getStatusCode(), is(200)); | ||
assertThat(response.getHeaders(), not(hasKey("Access-Control-Allow-Origin"))); | ||
} | ||
|
||
@Test | ||
public void testThatRegularExpressionIsNotAppliedWithoutCorrectBrowserOnMatch() throws Exception { | ||
HttpResponse response = httpClient().method("GET").path("/").execute(); | ||
assertThat(response.getStatusCode(), is(200)); | ||
assertThat(response.getHeaders(), not(hasKey("Access-Control-Allow-Origin"))); | ||
} | ||
|
||
@Test | ||
public void testThatPreFlightRequestWorksOnMatch() throws Exception { | ||
String corsValue = "http://localhost:9200"; | ||
HttpResponse response = httpClient().method("OPTIONS").path("/").addHeader("User-Agent", "Mozilla Bar").addHeader("Origin", corsValue).execute(); | ||
assertResponseWithOriginheader(response, corsValue); | ||
} | ||
|
||
@Test | ||
public void testThatPreFlightRequestReturnsNullOnNonMatch() throws Exception { | ||
HttpResponse response = httpClient().method("OPTIONS").path("/").addHeader("User-Agent", "Mozilla Bar").addHeader("Origin", "http://evil-host:9200").execute(); | ||
assertResponseWithOriginheader(response, "null"); | ||
} | ||
|
||
public static HttpRequestBuilder httpClient() { | ||
HttpServerTransport httpServerTransport = internalCluster().getDataNodeInstance(HttpServerTransport.class); | ||
InetSocketAddress address = ((InetSocketTransportAddress) httpServerTransport.boundAddress().publishAddress()).address(); | ||
return new HttpRequestBuilder(HttpClients.createDefault()).host(address.getHostName()).port(address.getPort()); | ||
} | ||
|
||
public static void assertResponseWithOriginheader(HttpResponse response, String expectedCorsHeader) { | ||
assertThat(response.getStatusCode(), is(200)); | ||
assertThat(response.getHeaders(), hasKey("Access-Control-Allow-Origin")); | ||
assertThat(response.getHeaders().get("Access-Control-Allow-Origin"), is(expectedCorsHeader)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters