mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 01:16:50 +00:00
Forbid idempotent numberless registration retries if MFA keys are present
This commit is contained in:
1 parent
77e3675fc9
commit
6b3f66ddc1
4 files changed
+79
-6
No files matched your search
+12
-2
@@ -18,6 +18,7 @@ import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import javax.annotation.Nullable;
|
||||
import jakarta.validation.constraints.PositiveOrZero;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.signal.libsignal.protocol.IdentityKey;
|
||||
import org.whispersystems.textsecuregcm.util.ByteArrayAdapter;
|
||||
import org.whispersystems.textsecuregcm.util.IdentityKeyAdapter;
|
||||
@@ -45,13 +46,15 @@ public record RegistrationRequest(@Schema(requiredMode = Schema.RequiredMode.NOT
|
||||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = """
|
||||
A base64-encoded receipt credential presentation that allows for redeeming a Signal Login.
|
||||
Must be provided if `sessionId` and `recoveryPassword` are not provided;
|
||||
must not be provided if `sessionId` or `recoveryPassword` is provided.
|
||||
must not be provided if `sessionId` or `recoveryPassword` is provided. Must not be
|
||||
provided if recovering a previously used account.
|
||||
""")
|
||||
@Nullable
|
||||
byte[] receiptCredentialPresentation,
|
||||
|
||||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = """
|
||||
A TOTP one-time password; required if recovering an account that has TOTP keys.
|
||||
A TOTP one-time password; required if recovering an account that has TOTP keys. Must
|
||||
not be provided if `receiptCredentialPresentation` is provided.
|
||||
""")
|
||||
@PositiveOrZero
|
||||
@Nullable
|
||||
@@ -142,4 +145,11 @@ public record RegistrationRequest(@Schema(requiredMode = Schema.RequiredMode.NOT
|
||||
deviceActivationRequest().pniPqLastResortPreKey().isPresent())
|
||||
.allMatch(present -> present == expectPresence);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@AssertTrue
|
||||
@Schema(hidden = true)
|
||||
boolean isMfaAbsentWhenUsingReceipt() {
|
||||
return ArrayUtils.isEmpty(receiptCredentialPresentation) || totp == null;
|
||||
}
|
||||
}
|
||||
@@ -338,12 +338,18 @@ public class Accounts {
|
||||
// The account was already deleted, and we don't allow re-registering with the same receipt
|
||||
.orElseThrow(ReceiptAlreadyRedeemedException::new);
|
||||
|
||||
// If the account recovery password in the request matches the existing account, this is likely a client retry,
|
||||
// and we continue to allow for idempotency, otherwise this is an attempt to double-redeem a receipt
|
||||
final boolean isRetry = existingAccount.getAccountRecoveryPassword()
|
||||
final boolean accountRecoveryPasswordMatches = existingAccount.getAccountRecoveryPassword()
|
||||
.map(arp -> PhoneNumberRecoveryPasswordsManager.verify(arp, accountRecoveryPasswordInRequest))
|
||||
.orElse(false);
|
||||
|
||||
// Determine if this request is safe to idempotently succeed. It's safe if:
|
||||
// - The account recovery password in the request matches the existing account's
|
||||
// - The existing account does not have any MFA keys. If MFA keys were added to the account the caller should
|
||||
// have used numberless account recovery rather than using their original receipt
|
||||
//
|
||||
// If either of these conditions are not met, the caller is incorrectly using an already redeemed receipt.
|
||||
final boolean isRetry = accountRecoveryPasswordMatches && existingAccount.getMfaKeys().isEmpty();
|
||||
|
||||
if (!isRetry) {
|
||||
throw new ReceiptAlreadyRedeemedException();
|
||||
}
|
||||
|
||||
+24
@@ -18,6 +18,7 @@ import org.junitpioneer.jupiter.cartesian.CartesianTest;
|
||||
import org.signal.libsignal.protocol.IdentityKey;
|
||||
import org.signal.libsignal.protocol.ecc.ECKeyPair;
|
||||
import org.whispersystems.textsecuregcm.tests.util.KeysHelper;
|
||||
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
|
||||
|
||||
class RegistrationRequestTest {
|
||||
|
||||
@@ -179,4 +180,27 @@ class RegistrationRequestTest {
|
||||
|
||||
assertEquals(expectAllPresentOrAbsent, registrationRequest.isAllOrNoPhoneNumberInformationProvided());
|
||||
}
|
||||
|
||||
@CartesianTest
|
||||
void isMfaAbsentWhenUsingReceipt(
|
||||
@CartesianTest.Values(booleans = {false, true}) boolean hasTotp,
|
||||
@CartesianTest.Values(booleans = {false, true}) boolean hasReceipt) {
|
||||
final RegistrationRequest registrationRequest = new RegistrationRequest(null,
|
||||
null,
|
||||
hasReceipt ? TestRandomUtil.nextBytes(32) : null,
|
||||
hasTotp ? 12345 : null,
|
||||
new AccountAttributes(true, 1, null, null, null, false, Collections.emptySet(), null),
|
||||
true,
|
||||
null,
|
||||
null,
|
||||
new DeviceActivationRequest(null,
|
||||
Optional.empty(),
|
||||
null,
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty()));
|
||||
|
||||
final boolean expectFailure = hasReceipt && hasTotp;
|
||||
assertEquals(!expectFailure, registrationRequest.isMfaAbsentWhenUsingReceipt());
|
||||
}
|
||||
}
|
||||
@@ -556,6 +556,31 @@ class AccountsTest {
|
||||
assertThat(reclaimed.getZkCredentialKey()).hasValue(existingAccount.getZkCredentialKey().orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIdempotentCreationWithMfaFails() throws Exception {
|
||||
final UUID existingUuid = UUID.randomUUID();
|
||||
final byte[] accountRecoveryPassword = TestRandomUtil.nextBytes(16);
|
||||
final Account existingAccount =
|
||||
generateAccount(null, existingUuid, null, List.of(generateDevice(DEVICE_ID_1)), accountRecoveryPassword);
|
||||
|
||||
existingAccount.setMfaKeys(Map.of((byte) 1, new AnnotatedTotpKey(new TotpKey(
|
||||
new TotpParameters(
|
||||
TimeBasedOneTimePasswordGenerator.TOTP_ALGORITHM_HMAC_SHA1,
|
||||
HmacOneTimePasswordGenerator.DEFAULT_PASSWORD_LENGTH,
|
||||
TimeBasedOneTimePasswordGenerator.DEFAULT_TIME_STEP),
|
||||
TestRandomUtil.nextBytes(16)),
|
||||
TestRandomUtil.nextBytes(16))));
|
||||
|
||||
final ReceiptCredentialPresentation receiptCredentialPresentation = receiptPresentation();
|
||||
createNumberlessAccount(existingAccount, receiptCredentialPresentation, accountRecoveryPassword);
|
||||
|
||||
final Account secondAccount =
|
||||
generateAccount(null, UUID.randomUUID(), null, List.of(generateDevice(DEVICE_ID_1)), accountRecoveryPassword);
|
||||
assertThrows(ReceiptAlreadyRedeemedException.class,
|
||||
() -> accounts.create(secondAccount, receiptCredentialPresentation, accountRecoveryPassword,
|
||||
Collections.emptyList()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"+14151112222"})
|
||||
@NullSource
|
||||
@@ -588,7 +613,15 @@ class AccountsTest {
|
||||
if (number != null) {
|
||||
reclaimAccount(secondAccount);
|
||||
} else {
|
||||
reclaimNumberlessAccount(secondAccount, receiptCredentialPresentation, accountRecoveryPassword);
|
||||
// There are essentially two modes of 'reclamation' for a numberless account:
|
||||
// - Try to create an account and then reclaim it on conflict to allow for idempotent retries of creation
|
||||
// - Explicitly recover the account, since we know from the API if the client is trying to recover
|
||||
// Only the second case works here, because we forbid idempotent retries if the account has already set an
|
||||
// MFA key.
|
||||
secondAccount.setAccountIdentifier(existingAccount.getAccountIdentifier());
|
||||
accounts.reclaimAccount(existingAccount,
|
||||
secondAccount,
|
||||
Collections.emptyList()).toCompletableFuture().join();
|
||||
}
|
||||
|
||||
final Account reclaimed = accounts.getByAccountIdentifier(existingUuid).orElseThrow();
|
||||
|
||||
Reference in new issue
Block a user