Add backup-auth-check rate limiter to CredentialsAnonymousGrpcService

This commit is contained in:
Chris Eager authored and Chris Eager committed 2026-10-13 10:38:17 -04:00
1 parent 55abc73cac
commit 8f394663b0
4 files changed
+45 -4

No files matched your search

@@ -1180,7 +1180,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
new ProfileAnonymousGrpcService(accountsManager, profilesManager, profileBadgeConverter, profileCdnPolicyGenerator, chatGenericZkSecretParams, groupZkSecretParams, rateLimiters, clock),
new MessagesAnonymousGrpcService(accountsManager, rateLimiters, messageSender, groupSendTokenUtil, messageByteLimitCardinalityEstimator, spamChecker, Clock.systemUTC()),
new BackupsAnonymousGrpcService(backupManager, backupMetrics, config.getAttachments().maxAttachmentUploadSizeInBytes(), config.getAttachments().maxMessageBackupUploadSizeInBytes()),
new CredentialsAnonymousGrpcService(accountsManager, ExternalServiceDefinitions.SVR.generatorFactory().apply(config, Clock.systemUTC())),
new CredentialsAnonymousGrpcService(accountsManager, ExternalServiceDefinitions.SVR.generatorFactory().apply(config, Clock.systemUTC()), rateLimiters),
new SubscriptionsGrpcService(clock, config.getSubscription(), subscriptionManager, donationPermitsManager,
stripeManager, braintreeManager, googlePlayBillingManager, appleAppStoreManager, bankMandateTranslator),
new OneTimeDonationsGrpcService(config.getOneTimeDonations(), stripeManager, braintreeManager,
@@ -12,6 +12,8 @@ import org.signal.chat.credentials.SimpleCredentialsAnonymousGrpc;
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentials;
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialsGenerator;
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialsSelector;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.AccountsManager;
import java.util.HashSet;
@@ -23,18 +25,25 @@ public class CredentialsAnonymousGrpcService extends SimpleCredentialsAnonymousG
private final AccountsManager accountsManager;
private final ExternalServiceCredentialsGenerator svrCredentialsGenerator;
private final RateLimiters rateLimiters;
private static final long MAX_SVR_PASSWORD_AGE_SECONDS = TimeUnit.DAYS.toSeconds(30);
public CredentialsAnonymousGrpcService(final AccountsManager accountsManager,
final ExternalServiceCredentialsGenerator svrCredentialsGenerator) {
final ExternalServiceCredentialsGenerator svrCredentialsGenerator,
final RateLimiters rateLimiters) {
this.svrCredentialsGenerator = svrCredentialsGenerator;
this.accountsManager = accountsManager;
this.rateLimiters = rateLimiters;
}
@Override
public CheckSvrCredentialsResponse checkSvrCredentials(final CheckSvrCredentialsRequest request) {
public CheckSvrCredentialsResponse checkSvrCredentials(final CheckSvrCredentialsRequest request)
throws RateLimitExceededException {
RateLimitUtil.rateLimitByRemoteAddress(rateLimiters.getBackupAuthCheckLimiter());
final List<String> tokens = request.getPasswordsList().stream().distinct().toList();
final List<ExternalServiceCredentialsSelector.CredentialInfo> credentials = ExternalServiceCredentialsSelector.check(
new HashSet<>(tokens),
@@ -128,6 +128,10 @@ public class RateLimiters extends BaseRateLimiters<RateLimiters.For> {
return forDescriptor(For.ATTACHMENT_BYTES);
}
public RateLimiter getBackupAuthCheckLimiter() {
return forDescriptor(For.BACKUP_AUTH_CHECK);
}
public RateLimiter getPinLimiter() {
return forDescriptor(For.PIN);
}
@@ -7,6 +7,8 @@ package org.whispersystems.textsecuregcm.grpc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -30,6 +32,9 @@ import org.signal.chat.credentials.CheckSvrCredentialsResponse;
import org.signal.chat.credentials.CredentialsAnonymousGrpc;
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentials;
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialsGenerator;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.AccountsManager;
import org.whispersystems.textsecuregcm.util.MockUtils;
@@ -60,9 +65,15 @@ class CredentialsAnonymousGrpcServiceTest extends
@Mock
private AccountsManager accountsManager;
@Mock
private RateLimiters rateLimiters;
@Mock
private RateLimiter rateLimiter;
@Override
protected CredentialsAnonymousGrpcService createServiceBeforeEachTest() {
return new CredentialsAnonymousGrpcService(accountsManager, SVR_CREDENTIALS_GENERATOR);
return new CredentialsAnonymousGrpcService(accountsManager, SVR_CREDENTIALS_GENERATOR, rateLimiters);
}
@BeforeEach
@@ -71,6 +82,7 @@ class CredentialsAnonymousGrpcServiceTest extends
when(account.getAccountIdentifier()).thenReturn(USER_UUID);
when(accountsManager.getByE164(USER_E164)).thenReturn(Optional.of(account));
when(rateLimiters.getBackupAuthCheckLimiter()).thenReturn(rateLimiter);
}
@Test
@@ -147,6 +159,22 @@ class CredentialsAnonymousGrpcServiceTest extends
assertEquals(Status.INVALID_ARGUMENT.getCode(), status.getStatus().getCode());
}
@Test
void checkSvrCredentialsRateLimited() throws RateLimitExceededException {
final Duration retryAfter = Duration.ofSeconds(11);
doThrow(new RateLimitExceededException(retryAfter))
.when(rateLimiter).validate(anyString());
//noinspection ResultOfMethodCallIgnored
GrpcTestUtils.assertRateLimitExceeded(retryAfter,
() -> unauthenticatedServiceStub().checkSvrCredentials(CheckSvrCredentialsRequest.newBuilder()
.setNumber(USER_E164)
.addPasswords(token(USER_UUID, day(1)))
.build()),
accountsManager);
}
private void assertExpectedCredentialCheckResponse(
final Map<String, AuthCheckResult> expected,
final long nowMillis) {