From 30a7652ecbe8a665dd8f5373483a1e11ae658731 Mon Sep 17 00:00:00 2001 From: Ben McIlwain Date: Wed, 18 Nov 2026 14:33:28 -0500 Subject: [PATCH] Reload one-time-use tokens to prevent cache reuse Update AllocationTokenFlowUtils.loadAndValidateToken() to check tokenEntity.getTokenType().isOneTimeUse() instead of checking only TokenType.SINGLE_USE when reloading tokens from the database. PR #3157 added an uncached database reload for SINGLE_USE tokens to prevent double-redemption within the 10-minute ALLOCATION_TOKENS_CACHE TTL, but omitted TokenType.REGISTER_BSA (which also has isOneTimeUse() == true). As a result, a REGISTER_BSA token already redeemed in Cloud SQL remained unredeemed in JVM heap cache for up to 10 minutes and could be redeemed a second time without recording a PostgreSQL SERIALIZABLE read dependency. - Replace equals(TokenType.SINGLE_USE) with isOneTimeUse() in AllocationTokenFlowUtils.loadAndValidateToken() so all one-time-use token types (SINGLE_USE and REGISTER_BSA) reload fresh database state inside the active transaction before validation. - Add unit tests in AllocationTokenFlowUtilsTest and DomainCreateFlowTest verifying that a cached REGISTER_BSA token already redeemed in the database is rejected with AlreadyRedeemedAllocationTokenException. BUG= http://b/534931178 --- .../token/AllocationTokenFlowUtils.java | 2 +- .../flows/domain/DomainCreateFlowTest.java | 38 +++++++++++++++++++ .../token/AllocationTokenFlowUtilsTest.java | 24 ++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/google/registry/flows/domain/token/AllocationTokenFlowUtils.java b/core/src/main/java/google/registry/flows/domain/token/AllocationTokenFlowUtils.java index af48dc2e196..592254100d7 100644 --- a/core/src/main/java/google/registry/flows/domain/token/AllocationTokenFlowUtils.java +++ b/core/src/main/java/google/registry/flows/domain/token/AllocationTokenFlowUtils.java @@ -280,7 +280,7 @@ private static AllocationToken loadAndValidateToken( VKey tokenKey = VKey.create(AllocationToken.class, token); AllocationToken tokenEntity = AllocationToken.get(tokenKey).orElseThrow(NonexistentAllocationTokenException::new); - if (tokenEntity.getTokenType().equals(AllocationToken.TokenType.SINGLE_USE)) { + if (tokenEntity.getTokenType().isOneTimeUse()) { // Reload the token to avoid possible cache race conditions where the token may have already // been redeemed tokenEntity = tm().loadByKey(tokenKey); diff --git a/core/src/test/java/google/registry/flows/domain/DomainCreateFlowTest.java b/core/src/test/java/google/registry/flows/domain/DomainCreateFlowTest.java index 69a7ea8fe20..d0cd1b3aa86 100644 --- a/core/src/test/java/google/registry/flows/domain/DomainCreateFlowTest.java +++ b/core/src/test/java/google/registry/flows/domain/DomainCreateFlowTest.java @@ -2137,6 +2137,44 @@ void testSuccess_blockedByBsa_hasRegisterBsaToken() throws Exception { assertSuccessfulCreate("tld", ImmutableSet.of(), allocationToken); } + @Test + void testFailure_blockedByBsa_registerBsaTokenAlreadyRedeemedInDatabaseWithStaleCache() + throws Exception { + AllocationToken.setCacheForTest(Optional.of(Duration.ofMinutes(10))); + try { + enrollTldInBsa(); + AllocationToken token = + persistResource( + new AllocationToken.Builder() + .setToken("abc123") + .setTokenType(REGISTER_BSA) + .setDomainName("example.tld") + .build()); + persistBsaLabel("example"); + persistHosts(); + setEppInput( + "domain_create_allocationtoken.xml", + ImmutableMap.of("DOMAIN", "example.tld", "YEARS", "2")); + runFlow(); + assertSuccessfulCreate("tld", ImmutableSet.of(), token); + assertAllocationTokenWasRedeemed("abc123"); + // Cache still holds the unredeemed REGISTER_BSA token within its 10-minute TTL. + assertThat(AllocationToken.get(token.createVKey()).get().isRedeemed()).isFalse(); + + // Delete the created domain so a second registration of example.tld can be attempted. + Instant deletionTime = Instant.parse("1999-04-03T22:01:00.000Z"); + persistResource( + reloadResourceByForeignKey().asBuilder().setDeletionTime(deletionTime).build()); + clock.setTo(deletionTime.plusSeconds(1)); + + EppException thrown = + assertThrows(AlreadyRedeemedAllocationTokenException.class, this::runFlow); + assertAboutEppExceptions().that(thrown).marshalsToXml(); + } finally { + AllocationToken.setCacheForTest(Optional.empty()); + } + } + @Test void testSuccess_blockedByBsa_reservedDomain_viaAllocationTokenExtension() throws Exception { enrollTldInBsa(); diff --git a/core/src/test/java/google/registry/flows/domain/token/AllocationTokenFlowUtilsTest.java b/core/src/test/java/google/registry/flows/domain/token/AllocationTokenFlowUtilsTest.java index df8ab722bdd..1417cacf43c 100644 --- a/core/src/test/java/google/registry/flows/domain/token/AllocationTokenFlowUtilsTest.java +++ b/core/src/test/java/google/registry/flows/domain/token/AllocationTokenFlowUtilsTest.java @@ -20,6 +20,7 @@ import static google.registry.model.domain.token.AllocationToken.TokenStatus.NOT_STARTED; import static google.registry.model.domain.token.AllocationToken.TokenStatus.VALID; import static google.registry.model.domain.token.AllocationToken.TokenType.DEFAULT_PROMO; +import static google.registry.model.domain.token.AllocationToken.TokenType.REGISTER_BSA; import static google.registry.model.domain.token.AllocationToken.TokenType.SINGLE_USE; import static google.registry.model.domain.token.AllocationToken.TokenType.UNLIMITED_USE; import static google.registry.persistence.transaction.TransactionManagerFactory.tm; @@ -307,6 +308,29 @@ void testFailure_loadFromExtension_singleUseTokenStaleCacheReloadsFromDatabase() assertLoadTokenFromExtensionThrowsException(AlreadyRedeemedAllocationTokenException.class); } + @Test + void testFailure_loadFromExtension_registerBsaTokenStaleCacheReloadsFromDatabase() { + when(allocationTokenExtension.getAllocationToken()).thenReturn("tokeN"); + AllocationToken token = + persistResource( + new AllocationToken.Builder() + .setToken("tokeN") + .setTokenType(REGISTER_BSA) + .setDomainName("example.tld") + .build()); + assertThat(AllocationToken.get(token.createVKey())).hasValue(token); + tm().transact( + () -> + tm().put( + token + .asBuilder() + .setRedemptionHistoryId(new HistoryEntryId("repoId", 1L)) + .build())); + // Cache still returns the old unredeemed REGISTER_BSA token. + assertThat(AllocationToken.get(token.createVKey()).get().isRedeemed()).isFalse(); + assertLoadTokenFromExtensionThrowsException(AlreadyRedeemedAllocationTokenException.class); + } + @Test void testFailure_tokenInvalidForRegistrar() { persistResource(