[java-idp-oidc] 02/03: JOIDC-314 - Exploit replay cache for one-time refresh tokens

Codeberg noreply at shibboleth.net
Mon Nov 23 17:21:44 UTC 2026


This is an automated email from the git hooks/post-receive script.

codeberg pushed a commit to branch maint-4.4
in repository java-idp-oidc.

View the commit online:
https://codeberg.org/Shibboleth/java-idp-oidc/commit/1c2bfaf6f825c9d40af43191619094a8517cb9fc

commit 1c2bfaf6f825c9d40af43191619094a8517cb9fc
Author: Henri Mikkonen <henri.mikkonen at iki.fi>
AuthorDate: Mon Nov 23 18:55:42 2026 +0200

    JOIDC-314 - Exploit replay cache for one-time refresh tokens
    
    https://shibboleth.atlassian.net/browse/JOIDC-314
    
    - Wire EnforceRefreshTokenRotationPredicate: if it returns true, check for replay (similarly to authorization codes) and revoke chain if replay is detected
    - Did not include the test change from original commit as the test class structure has changed in the codebase
---
 .../plugin/oidc/op/profile/impl/ValidateGrant.java | 65 +++++++++++++++++++++-
 .../oidc/op/profile/impl/ValidateGrantTest.java    |  2 +-
 2 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrant.java b/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrant.java
index 2dd55d38..c6d8b64a 100644
--- a/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrant.java
+++ b/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrant.java
@@ -51,6 +51,7 @@ import net.shibboleth.idp.plugin.oidc.op.token.support.RefreshTokenClaimsSet;
 import net.shibboleth.idp.plugin.oidc.op.token.support.TokenClaimsSet;
 import net.shibboleth.idp.profile.IdPEventIds;
 import net.shibboleth.oidc.metadata.context.OIDCMetadataContext;
+import net.shibboleth.oidc.profile.config.logic.EnforceRefreshTokenRotationPredicate;
 import net.shibboleth.oidc.profile.config.logic.RefreshTokensEnabledPredicate;
 import net.shibboleth.oidc.profile.config.navigate.RefreshTokenChainLifetimeLookupFunction;
 import net.shibboleth.oidc.profile.config.navigate.RevocationLifetimeLookupFunction;
@@ -117,6 +118,9 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
     /** Predicate used to indicate whether the authorization code or refresh token is revoked. */
     @Nullable private BiPredicate<ProfileRequestContext,JWTClaimsSet> tokenRevocationCondition;
 
+    /** Strategy used to determine whether to revoke refresh tokens once they're used. */
+    @Nonnull private Predicate<ProfileRequestContext> enforceRefreshTokenRotationCondition;
+
     /** The RelyingPartyContext to operate on. */
     @Nullable private RelyingPartyContext rpCtx;
 
@@ -137,6 +141,7 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
         refreshTokenChainLifetimeLookupStrategy = new RefreshTokenChainLifetimeLookupFunction();
         refreshTokenDeserializers = CollectionSupport.emptyList();
         dpopProofThumbprintLookupStrategy = new DefaultDPoPProofThumbprintLookupFunction();
+        enforceRefreshTokenRotationCondition = new EnforceRefreshTokenRotationPredicate();
     }
 
     /**
@@ -244,6 +249,18 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
         tokenRevocationCondition = condition;
     }
 
+    /**
+     * Set the condition used to determine whether to revoke refresh tokens once they're used.
+     * 
+     * @param condition condition to apply
+     * @since 4.4.0
+     */
+    public void setEnforceRefreshTokenRotationCondition(@Nonnull final Predicate<ProfileRequestContext> condition) {
+        checkSetterPreconditions();
+
+        enforceRefreshTokenRotationCondition = Constraint.isNotNull(condition, "Condition cannot be null");
+    }
+
     /** {@inheritDoc} */
     @Override
     protected void doInitialize() throws ComponentInitializationException {
@@ -470,6 +487,13 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
         } else {
             oidcResponseContext.setDpopProofJwkThumbprint(proofThumbprint);
         }
+
+        if (GrantType.REFRESH_TOKEN.equals(grant.getType()) &&
+                enforceRefreshTokenRotationCondition.test(profileRequestContext) &&
+                !checkRefreshTokenReplay(tokenClaimsSet, profileRequestContext)) {
+            ActionSupport.buildEvent(profileRequestContext, OidcEventIds.INVALID_GRANT);
+            return;
+        }
     }
 // Checkstyle: CyclomaticComplexity|MethodLength|ReturnCount ON
 
@@ -535,5 +559,44 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
         }
         return revocationCache.revoke(RevocationCacheContexts.AUTHORIZATION_CODE, id, lifetime);
     }
-    
+
+    /**
+     * Checks that the refresh token has not been used before by exploiting {@link #replayCache}.
+     * 
+     * @param tokenClaimsSet the claims set used for fetching identifiers
+     * @param profileRequestContext profile request context
+     * @return true if the replay-check passed, false otherwise
+     * @since 4.4.0
+     */
+    protected boolean checkRefreshTokenReplay(@Nonnull final TokenClaimsSet tokenClaimsSet,
+            @Nonnull final ProfileRequestContext profileRequestContext) {
+        final String cacheContext = getClass().getName() + ".refreshTokens";
+        final String rootJti = tokenClaimsSet.getRootTokenIdentifier();
+
+        final String claimsSetJti = tokenClaimsSet.getID();
+        assert claimsSetJti != null;
+        assert cacheContext != null;
+        if (!replayCache.check(cacheContext, claimsSetJti, tokenClaimsSet.getExp())) {
+            log.warn("{} Replay detected of refresh token {} when rotation is enabled: revoking the chain",
+                    getLogPrefix(), claimsSetJti);
+            final String rootJtiToUse;
+            if (StringSupport.trimOrNull(rootJti) == null) {
+                log.warn("{} No root token identifier returned, using JWT id for checking revocation status",
+                        getLogPrefix());
+                rootJtiToUse = tokenClaimsSet.getID();
+            } else {
+                rootJtiToUse = rootJti;
+            }
+            assert rootJtiToUse != null;
+
+            if (!revokeChain(rootJtiToUse,
+                    chainRevocationLifetimeLookupStrategy.apply(profileRequestContext))) {
+                log.error("{} Fatal error, unable to save replayed refresh token to revocation cache",
+                        getLogPrefix());
+            }
+            return false;
+        }
+        log.debug("{} Refresh token replay check successful", getLogPrefix());
+        return true;
+    }
 }
diff --git a/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrantTest.java b/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrantTest.java
index e3083841..e5f2f3b7 100644
--- a/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrantTest.java
+++ b/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrantTest.java
@@ -464,4 +464,4 @@ public class ValidateGrantTest extends BaseOIDCResponseActionTest {
             super(claims);
         }
     }
-}
\ No newline at end of file
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list