Skip to content

Commit d5ea814

Browse files
nisrulzthestr4ng3r
andauthored
Fix NullPointerException for issuer without scheme in id token (#4)
Uri.getScheme() may return null if no scheme is contained in the given string. This could cause a crash during id token validation when this was the case for the contained "iss" claim. Co-authored-by: Florian Märkl <[email protected]>
1 parent 9efe2d7 commit d5ea814

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

library/java/net/openid/appauth/IdToken.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ void validate(@NonNull TokenRequest tokenRequest,
231231
// components.
232232
Uri issuerUri = Uri.parse(this.issuer);
233233

234-
if (!skipIssuerHttpsCheck && !issuerUri.getScheme().equals("https")) {
234+
String issuerScheme = issuerUri.getScheme();
235+
if (!skipIssuerHttpsCheck && (issuerScheme == null || !issuerScheme.equals("https"))) {
235236
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
236237
new IdTokenException("Issuer must be an https URL"));
237238
}

library/javatests/net/openid/appauth/IdTokenTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,34 @@ public void testValidate_shouldFailOnIssuerWithFragment()
361361
idToken.validate(tokenRequest, clock);
362362
}
363363

364+
@Test(expected = AuthorizationException.class)
365+
public void testValidate_shouldFailOnIssuerMissingScheme()
366+
throws AuthorizationException, JSONException, MissingArgumentException {
367+
Long nowInSeconds = SystemClock.INSTANCE.getCurrentTimeMillis() / 1000;
368+
Long tenMinutesInSeconds = (long) (10 * 60);
369+
IdToken idToken = new IdToken(
370+
"some.issuer",
371+
TEST_SUBJECT,
372+
Collections.singletonList(TEST_CLIENT_ID),
373+
nowInSeconds + tenMinutesInSeconds,
374+
nowInSeconds
375+
);
376+
377+
String serviceDocJsonWithIssuerMissingHost = getDiscoveryDocJsonWithIssuer("some.issuer");
378+
AuthorizationServiceDiscovery discoveryDoc = new AuthorizationServiceDiscovery(
379+
new JSONObject(serviceDocJsonWithIssuerMissingHost));
380+
AuthorizationServiceConfiguration serviceConfiguration =
381+
new AuthorizationServiceConfiguration(discoveryDoc);
382+
TokenRequest tokenRequest = new TokenRequest.Builder(serviceConfiguration, TEST_CLIENT_ID)
383+
.setAuthorizationCode(TEST_AUTH_CODE)
384+
.setCodeVerifier(TEST_CODE_VERIFIER)
385+
.setGrantType(GrantTypeValues.AUTHORIZATION_CODE)
386+
.setRedirectUri(TEST_APP_REDIRECT_URI)
387+
.build();
388+
Clock clock = SystemClock.INSTANCE;
389+
idToken.validate(tokenRequest, clock);
390+
}
391+
364392
@Test
365393
public void testValidate_audienceMatch() throws AuthorizationException {
366394
Long nowInSeconds = SystemClock.INSTANCE.getCurrentTimeMillis() / 1000;

0 commit comments

Comments
 (0)