Skip to content

Commit b8ca257

Browse files
committed
Fix NullPointerException for issuer without scheme in id token
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.
1 parent 5966cc7 commit b8ca257

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

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

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

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

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)