Fix OIDC ID-token nonce against session - #4012
Open
joemahady-comm wants to merge 1 commit into
Open
Conversation
…ession ai-assisted=yes Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR strengthens the external OIDC login flow by binding the ID token’s nonce claim to a per-session value, reducing replay risk during authentication against OIDC identity providers.
Changes:
- Added a session attribute key helper for per-IdP
noncestorage. - Generated a per-request OIDC
nonce, stored it in the HTTP session, and added it to the authorization request. - Validated the incoming ID token
nonceagainst the session value and cleared it on successful validation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
server/src/main/java/org/cloudfoundry/identity/uaa/util/SessionUtils.java |
Adds a dedicated session attribute key prefix/helper for external OAuth/OIDC nonce storage. |
server/src/main/java/org/cloudfoundry/identity/uaa/provider/oauth/ExternalOAuthProviderConfigurator.java |
Generates an OIDC nonce, stores it in the user session, and includes it in the authz redirect URL. |
server/src/main/java/org/cloudfoundry/identity/uaa/provider/oauth/ExternalOAuthAuthenticationManager.java |
Extracts and compares the ID token’s nonce claim against the session-stored nonce; clears the session value on success. |
Comment on lines
+719
to
+728
| if (config instanceof OIDCIdentityProviderDefinition) { | ||
| String expectedNonce = getSessionValue(SessionUtils.nonceParameterAttributeKeyForIdp(identityProvider.getOriginKey())); | ||
| if (StringUtils.hasText(expectedNonce)) { | ||
| String tokenNonce = (String) claims.get("nonce"); | ||
| if (!expectedNonce.equals(tokenNonce)) { | ||
| throw new InvalidTokenException("ID token nonce does not match session nonce"); | ||
| } | ||
| clearSessionValue(SessionUtils.nonceParameterAttributeKeyForIdp(identityProvider.getOriginKey())); | ||
| } | ||
| } |
Comment on lines
121
to
+125
| if (OIDCIdentityProviderDefinition.class.equals(definition.getParameterizedClass())) { | ||
| var nonceGenerator = new RandomValueStringGenerator(22); | ||
| uriBuilder.queryParam("nonce", nonceGenerator.generate()); | ||
| String nonce = nonceGenerator.generate(); | ||
| SessionUtils.setStateParam(request.getSession(), SessionUtils.nonceParameterAttributeKeyForIdp(idpOriginKey), nonce); | ||
| uriBuilder.queryParam("nonce", nonce); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Implemented generation and validation of OIDC nonce parameters to prevent replay attacks during authentication.
Details: Modified ExternalOAuthProviderConfigurator to generate a random 22-character nonce and store it in the user's HTTP session before initiating an OIDC authorization request. ExternalOAuthAuthenticationManager now extracts the nonce claim from the incoming ID token, compares it against the session-stored value, and throws an InvalidTokenException if they mismatch. It properly clears the session value upon successful validation.