Skip to content

Commit e5bd814

Browse files
Fix NPE for missing Content Type header in OIDC Authenticator (#126191) (#126205)
* Fix NPE for missing Content Type header in OIDC Authenticator * Update docs/changelog/126191.yaml
1 parent 43827b9 commit e5bd814

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

docs/changelog/126191.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 126191
2+
summary: Fix NPE for missing Content Type header in OIDC Authenticator
3+
area: Authentication
4+
type: bug
5+
issues: []

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,18 +629,20 @@ public void cancelled() {
629629
/**
630630
* Handle the Token Response from the OpenID Connect Provider. If successful, extract the (yet not validated) Id Token
631631
* and access token and call the provided listener.
632+
* (Package private for testing purposes)
632633
*/
633-
private static void handleTokenResponse(HttpResponse httpResponse, ActionListener<Tuple<AccessToken, JWT>> tokensListener) {
634+
static void handleTokenResponse(HttpResponse httpResponse, ActionListener<Tuple<AccessToken, JWT>> tokensListener) {
634635
try {
635636
final HttpEntity entity = httpResponse.getEntity();
636637
final Header encodingHeader = entity.getContentEncoding();
637638
final Header contentHeader = entity.getContentType();
638-
if (ContentType.parse(contentHeader.getValue()).getMimeType().equals("application/json") == false) {
639+
final String contentHeaderValue = contentHeader == null ? null : ContentType.parse(contentHeader.getValue()).getMimeType();
640+
if (contentHeaderValue == null || contentHeaderValue.equals("application/json") == false) {
639641
tokensListener.onFailure(
640642
new IllegalStateException(
641643
"Unable to parse Token Response. Content type was expected to be "
642644
+ "[application/json] but was ["
643-
+ contentHeader.getValue()
645+
+ contentHeaderValue
644646
+ "]"
645647
)
646648
);
@@ -688,7 +690,7 @@ private static void handleTokenResponse(HttpResponse httpResponse, ActionListene
688690
} catch (Exception e) {
689691
tokensListener.onFailure(
690692
new ElasticsearchSecurityException(
691-
"Failed to exchange code for Id Token using the Token Endpoint. " + "Unable to parse Token Response",
693+
"Failed to exchange code for Id Token using the Token Endpoint. Unable to parse Token Response",
692694
e
693695
)
694696
);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticatorTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,23 @@ public void testHandleUserinfoResponseFailure() throws Exception {
968968
);
969969
}
970970

971+
public void testHandleTokenResponseNullContentType() {
972+
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, RestStatus.OK.getStatus(), "");
973+
final StringEntity entity = new StringEntity("", (ContentType) null);
974+
response.setEntity(entity);
975+
976+
final PlainActionFuture<Tuple<AccessToken, JWT>> future = new PlainActionFuture<>();
977+
OpenIdConnectAuthenticator.handleTokenResponse(response, future);
978+
final IllegalStateException exception = expectThrows(IllegalStateException.class, future::actionGet);
979+
980+
assertThat(
981+
exception,
982+
TestMatchers.throwableWithMessage(
983+
"Unable to parse Token Response. Content type was expected to be [application/json] but was [null]"
984+
)
985+
);
986+
}
987+
971988
public void testLogIdTokenAndNonce() throws URISyntaxException, BadJOSEException, JOSEException, IllegalAccessException {
972989
final Logger logger = LogManager.getLogger(OpenIdConnectAuthenticator.class);
973990
Loggers.setLevel(logger, Level.DEBUG);

0 commit comments

Comments
 (0)