Skip to content

Commit

Permalink
Fix NullPointerException for issuer without scheme in id token (#4)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
nisrulz and thestr4ng3r authored Feb 26, 2024
1 parent 9efe2d7 commit d5ea814
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/java/net/openid/appauth/IdToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ void validate(@NonNull TokenRequest tokenRequest,
// components.
Uri issuerUri = Uri.parse(this.issuer);

if (!skipIssuerHttpsCheck && !issuerUri.getScheme().equals("https")) {
String issuerScheme = issuerUri.getScheme();
if (!skipIssuerHttpsCheck && (issuerScheme == null || !issuerScheme.equals("https"))) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException("Issuer must be an https URL"));
}
Expand Down
28 changes: 28 additions & 0 deletions library/javatests/net/openid/appauth/IdTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,34 @@ public void testValidate_shouldFailOnIssuerWithFragment()
idToken.validate(tokenRequest, clock);
}

@Test(expected = AuthorizationException.class)
public void testValidate_shouldFailOnIssuerMissingScheme()
throws AuthorizationException, JSONException, MissingArgumentException {
Long nowInSeconds = SystemClock.INSTANCE.getCurrentTimeMillis() / 1000;
Long tenMinutesInSeconds = (long) (10 * 60);
IdToken idToken = new IdToken(
"some.issuer",
TEST_SUBJECT,
Collections.singletonList(TEST_CLIENT_ID),
nowInSeconds + tenMinutesInSeconds,
nowInSeconds
);

String serviceDocJsonWithIssuerMissingHost = getDiscoveryDocJsonWithIssuer("some.issuer");
AuthorizationServiceDiscovery discoveryDoc = new AuthorizationServiceDiscovery(
new JSONObject(serviceDocJsonWithIssuerMissingHost));
AuthorizationServiceConfiguration serviceConfiguration =
new AuthorizationServiceConfiguration(discoveryDoc);
TokenRequest tokenRequest = new TokenRequest.Builder(serviceConfiguration, TEST_CLIENT_ID)
.setAuthorizationCode(TEST_AUTH_CODE)
.setCodeVerifier(TEST_CODE_VERIFIER)
.setGrantType(GrantTypeValues.AUTHORIZATION_CODE)
.setRedirectUri(TEST_APP_REDIRECT_URI)
.build();
Clock clock = SystemClock.INSTANCE;
idToken.validate(tokenRequest, clock);
}

@Test
public void testValidate_audienceMatch() throws AuthorizationException {
Long nowInSeconds = SystemClock.INSTANCE.getCurrentTimeMillis() / 1000;
Expand Down

0 comments on commit d5ea814

Please sign in to comment.