Skip to content

Fix NPE for missing Content Type header in OIDC Authenticator #126191

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

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/126191.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 126191
summary: Fix NPE for missing Content Type header in OIDC Authenticator
area: Authentication
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -629,18 +629,20 @@ public void cancelled() {
/**
* Handle the Token Response from the OpenID Connect Provider. If successful, extract the (yet not validated) Id Token
* and access token and call the provided listener.
* (Package private for testing purposes)
*/
private static void handleTokenResponse(HttpResponse httpResponse, ActionListener<Tuple<AccessToken, JWT>> tokensListener) {
static void handleTokenResponse(HttpResponse httpResponse, ActionListener<Tuple<AccessToken, JWT>> tokensListener) {
try {
final HttpEntity entity = httpResponse.getEntity();
final Header encodingHeader = entity.getContentEncoding();
final Header contentHeader = entity.getContentType();
if (ContentType.parse(contentHeader.getValue()).getMimeType().equals("application/json") == false) {
final String contentHeaderValue = contentHeader == null ? null : ContentType.parse(contentHeader.getValue()).getMimeType();
if (contentHeaderValue == null || contentHeaderValue.equals("application/json") == false) {
tokensListener.onFailure(
new IllegalStateException(
"Unable to parse Token Response. Content type was expected to be "
+ "[application/json] but was ["
+ contentHeader.getValue()
+ contentHeaderValue
+ "]"
)
);
Expand Down Expand Up @@ -688,7 +690,7 @@ private static void handleTokenResponse(HttpResponse httpResponse, ActionListene
} catch (Exception e) {
tokensListener.onFailure(
new ElasticsearchSecurityException(
"Failed to exchange code for Id Token using the Token Endpoint. " + "Unable to parse Token Response",
"Failed to exchange code for Id Token using the Token Endpoint. Unable to parse Token Response",
e
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,23 @@ public void testHandleUserinfoResponseFailure() throws Exception {
);
}

public void testHandleTokenResponseNullContentType() {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, RestStatus.OK.getStatus(), "");
final StringEntity entity = new StringEntity("", (ContentType) null);
response.setEntity(entity);

final PlainActionFuture<Tuple<AccessToken, JWT>> future = new PlainActionFuture<>();
OpenIdConnectAuthenticator.handleTokenResponse(response, future);
final IllegalStateException exception = expectThrows(IllegalStateException.class, future::actionGet);

assertThat(
exception,
TestMatchers.throwableWithMessage(
"Unable to parse Token Response. Content type was expected to be [application/json] but was [null]"
)
);
}

public void testLogIdTokenAndNonce() throws URISyntaxException, BadJOSEException, JOSEException, IllegalAccessException {
final Logger logger = LogManager.getLogger(OpenIdConnectAuthenticator.class);
Loggers.setLevel(logger, Level.DEBUG);
Expand Down
Loading