-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Handle Unauthenticated OPTIONS requests #96061
Merged
albertzaharovits
merged 8 commits into
elastic:main
from
albertzaharovits:fix-authn-for-options-request
May 12, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
908ef11
Handle OPTIONS requests
albertzaharovits 83262e0
Fix SecurityRestFilterTests
albertzaharovits 47577a0
Merge branch 'main' into fix-authn-for-options-request
albertzaharovits ef4b807
assert method != RestRequest.Method.OPTIONS
albertzaharovits 5ef6530
testNoAuthnForResourceOptionsMethod
albertzaharovits 05aaab7
RestControllerTests
albertzaharovits 4a288f2
HttOptionsNoAuthnIntegTests testNoAuthnForPreFlightRequest
albertzaharovits 036aaec
cleanup
albertzaharovits File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
99 changes: 99 additions & 0 deletions
99
...lClusterTest/java/org/elasticsearch/xpack/security/authc/HttOptionsNoAuthnIntegTests.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,99 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security.authc; | ||
|
||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.Response; | ||
import org.elasticsearch.common.settings.SecureString; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.http.CorsHandler; | ||
import org.elasticsearch.http.HttpTransportSettings; | ||
import org.elasticsearch.test.SecurityIntegTestCase; | ||
import org.elasticsearch.test.SecuritySettingsSource; | ||
import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
public class HttOptionsNoAuthnIntegTests extends SecurityIntegTestCase { | ||
|
||
@Override | ||
protected boolean addMockHttpTransport() { | ||
return false; // need real http | ||
} | ||
|
||
@Override | ||
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { | ||
final Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)); | ||
// needed to test preflight requests | ||
builder.put(HttpTransportSettings.SETTING_CORS_ENABLED.getKey(), "true") | ||
.put(HttpTransportSettings.SETTING_CORS_ALLOW_ORIGIN.getKey(), "*"); | ||
return builder.build(); | ||
} | ||
|
||
public void testNoAuthnForResourceOptionsMethod() throws Exception { | ||
Request requestNoCredentials = new Request( | ||
"OPTIONS", | ||
randomFrom("/", "/_cluster/stats", "/some-index", "/index/_stats", "/_stats/flush") | ||
); | ||
// no "Authorization" request header -> request is unauthenticated | ||
assertThat(requestNoCredentials.getOptions().getHeaders().isEmpty(), is(true)); | ||
// WRONG "Authorization" request header | ||
Request requestWrongCredentials = new Request( | ||
"OPTIONS", | ||
randomFrom("/", "/_cluster/stats", "/some-index", "/index/_stats", "/_stats/flush") | ||
); | ||
RequestOptions.Builder options = requestWrongCredentials.getOptions().toBuilder(); | ||
options.addHeader( | ||
"Authorization", | ||
UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, new SecureString("WRONG")) | ||
); | ||
requestWrongCredentials.setOptions(options); | ||
for (Request request : List.of(requestNoCredentials, requestWrongCredentials)) { | ||
Response response = getRestClient().performRequest(request); | ||
assertThat(response.getStatusLine().getStatusCode(), is(200)); | ||
assertThat(response.getHeader("Allow"), notNullValue()); | ||
assertThat(response.getHeader("X-elastic-product"), is("Elasticsearch")); | ||
assertThat(response.getHeader("content-length"), is("0")); | ||
} | ||
} | ||
|
||
public void testNoAuthnForPreFlightRequest() throws Exception { | ||
Request requestNoCredentials = new Request( | ||
"OPTIONS", | ||
randomFrom("/", "/_cluster/stats", "/some-index", "/index/_stats", "/_stats/flush") | ||
); | ||
RequestOptions.Builder options = requestNoCredentials.getOptions().toBuilder(); | ||
options.addHeader(CorsHandler.ORIGIN, "google.com"); | ||
options.addHeader(CorsHandler.ACCESS_CONTROL_REQUEST_METHOD, "GET"); | ||
requestNoCredentials.setOptions(options); | ||
// no "Authorization" request header -> request is unauthenticated | ||
Request requestWrongCredentials = new Request( | ||
"OPTIONS", | ||
randomFrom("/", "/_cluster/stats", "/some-index", "/index/_stats", "/_stats/flush") | ||
); | ||
options = requestWrongCredentials.getOptions().toBuilder(); | ||
// WRONG "Authorization" request header | ||
options.addHeader( | ||
"Authorization", | ||
UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, new SecureString("WRONG")) | ||
); | ||
options.addHeader(CorsHandler.ORIGIN, "google.com"); | ||
options.addHeader(CorsHandler.ACCESS_CONTROL_REQUEST_METHOD, "GET"); | ||
requestWrongCredentials.setOptions(options); | ||
for (Request request : List.of(requestWrongCredentials)) { | ||
Response response = getRestClient().performRequest(request); | ||
assertThat(response.getStatusLine().getStatusCode(), is(200)); | ||
assertThat(response.getHeader("content-length"), is("0")); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.util.Supplier; | ||
import org.elasticsearch.ElasticsearchSecurityException; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.client.internal.node.NodeClient; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
|
@@ -60,9 +61,14 @@ public RestHandler getConcreteRestHandler() { | |
|
||
@Override | ||
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception { | ||
// requests with the OPTIONS method should be handled elsewhere, and not by calling {@code RestHandler#handleRequest} | ||
// authn is bypassed for HTTP requests with the OPTIONS method, so this sanity check prevents dispatching unauthenticated requests | ||
if (request.method() == Method.OPTIONS) { | ||
// CORS - allow for preflight unauthenticated OPTIONS request | ||
restHandler.handleRequest(request, channel, client); | ||
handleException( | ||
request, | ||
channel, | ||
new ElasticsearchSecurityException("Cannot dispatch OPTIONS request, as they are not authenticated") | ||
); | ||
Comment on lines
+64
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the second most interesting part. |
||
return; | ||
} | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the interesting part. Allow requests with OPTIONS method to bypass authentication.