Reject attempts to change phone numbers if the account doesn't have a number in the first place

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-07-29 14:44:58 -04:00
1 parent 9426d0bd9e
commit fad055aa44
4 files changed
+91 -25

No files matched your search

@@ -37,7 +37,6 @@ import org.whispersystems.textsecuregcm.entities.KEMSignedPreKey;
import org.whispersystems.textsecuregcm.entities.MessageProtos.Envelope;
import org.whispersystems.textsecuregcm.entities.PhoneVerificationRequest;
import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.metrics.UserAgentTagUtil;
import org.whispersystems.textsecuregcm.push.MessageSender;
@@ -102,8 +101,12 @@ public class ChangeNumberManager {
final Account account = accountAndMaybeExistingAccount.first();
if (account.getNumberOptional().isEmpty()) {
throw new IllegalArgumentException("account does not have a phone number");
}
// Only verify and check reglock if there's a data change to be made...
if (!account.getNumber().equals(number)) {
if (!account.getNumberOptional().get().equals(number)) {
final Optional<Duration> waitingPeriodRemaining = changeNumberWaitingPeriodManager.getWaitingPeriodRemaining(account.getAccountIdentifier());
if (waitingPeriodRemaining.isPresent()) {
@@ -130,7 +133,7 @@ public class ChangeNumberManager {
// ...but always attempt to make the change in case a client retries and needs to re-send messages
final long serverTimestamp = clock.millis();
final AciServiceIdentifier serviceIdentifier = new AciServiceIdentifier(account.getIdentifier(IdentityType.ACI));
final AciServiceIdentifier serviceIdentifier = new AciServiceIdentifier(account.getAccountIdentifier());
// Note that these for-validation envelopes do NOT have the "updated PNI" field set, and we'll need to populate that
// after actually changing the account's number.
@@ -160,13 +163,14 @@ public class ChangeNumberManager {
}
final Account updatedAccount = accountsManager.changeNumber(
account.getIdentifier(IdentityType.ACI), number, pniIdentityKey, deviceSignedPreKeys, devicePqLastResortPreKeys, pniRegistrationIds);
account.getAccountIdentifier(), number, pniIdentityKey, deviceSignedPreKeys, devicePqLastResortPreKeys, pniRegistrationIds);
if (!messagesByDeviceId.isEmpty()) {
try {
// Now that we've actually updated the account, populate the "updated PNI" field on all envelopes
messagesByDeviceId.replaceAll((_, envelope) ->
envelope.toBuilder().setUpdatedPni(UUIDUtil.toByteString(updatedAccount.getIdentifier(IdentityType.PNI)))
envelope.toBuilder().setUpdatedPni(UUIDUtil.toByteString(updatedAccount.getPhoneNumberIdentifierOptional()
.orElseThrow(() -> new AssertionError("Account does not have a PNI after changing number"))))
.build());
messageSender.sendMessages(updatedAccount,
@@ -178,11 +182,11 @@ public class ChangeNumberManager {
} catch (MismatchedDevicesException | MessageTooLargeException e) {
ImpossibleEvents.logImpossible(logger,
"Changed number but could not send device messages after preliminary validation succeeded {}",
account.getIdentifier(IdentityType.ACI), e);
account.getAccountIdentifier(), e);
throw e;
} catch (final RuntimeException e) {
logger.warn("Changed number but could not send all device messages for {}",
account.getIdentifier(IdentityType.ACI), e);
account.getAccountIdentifier(), e);
throw e;
}
}
@@ -358,6 +358,28 @@ class AccountControllerV2Test {
}
}
@Test
void accountHasNoNumber() throws Exception {
doThrow(IllegalArgumentException.class)
.when(changeNumberManager).changeNumber(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any());
try (final Response response = resources.getJerseyTest()
.target("/v2/accounts/number")
.request()
.header(HttpHeaders.AUTHORIZATION,
AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD))
.put(Entity.entity(
new ChangeNumberRequest(encodeSessionId("session"), null, NEW_NUMBER, "123", IDENTITY_KEY,
Collections.emptyList(),
Map.of(Device.PRIMARY_ID, KeysHelper.signedECPreKey(1, IDENTITY_KEY_PAIR)),
Map.of(Device.PRIMARY_ID, KeysHelper.signedKEMPreKey(2, IDENTITY_KEY_PAIR)),
Map.of(Device.PRIMARY_ID, 17)),
MediaType.APPLICATION_JSON_TYPE))) {
assertEquals(400, response.getStatus());
}
}
/**
* Valid request JSON with the give session ID and recovery password
*/
@@ -903,6 +903,17 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
.build()));
}
@Test
void changeNumberIllegalArgumentException() throws Exception {
final String message = "Horrors";
when(changeNumberManager.changeNumber(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
.thenThrow(new IllegalArgumentException(message));
GrpcTestUtils.assertStatusException(Status.INVALID_ARGUMENT.withDescription(message),
() -> authenticatedServiceStub().changeNumber(createChangeNumberRequest()));
}
@ParameterizedTest
@MethodSource
void changeNumberUnavailable(final Exception exceptionToThrow) throws Exception {
@@ -111,7 +111,7 @@ public class ChangeNumberManagerTest {
final Account account =
accountsManager.getAccountsForChangeNumber(accountIdentifier, number).first();
final UUID uuid = account.getIdentifier(IdentityType.ACI);
final UUID uuid = account.getAccountIdentifier();
final List<Device> devices = account.getDevices();
final Device primaryDevice = account.getPrimaryDevice();
@@ -119,12 +119,12 @@ public class ChangeNumberManagerTest {
updatedPhoneNumberIdentifiersByAccount.put(account, updatedPni);
final Account updatedAccount = mock(Account.class);
when(updatedAccount.getIdentifier(IdentityType.ACI)).thenReturn(uuid);
when(updatedAccount.getIdentifier(IdentityType.PNI)).thenReturn(updatedPni);
when(updatedAccount.getAccountIdentifier()).thenReturn(uuid);
when(updatedAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(updatedPni));
when(updatedAccount.isIdentifiedBy(any())).thenReturn(false);
when(updatedAccount.isIdentifiedBy(new AciServiceIdentifier(uuid))).thenReturn(true);
when(updatedAccount.isIdentifiedBy(new PniServiceIdentifier(updatedPni))).thenReturn(true);
when(updatedAccount.getNumber()).thenReturn(number);
when(updatedAccount.getNumberOptional()).thenReturn(Optional.of(number));
when(updatedAccount.getDevices()).thenReturn(devices);
when(updatedAccount.getDevice(anyByte())).thenReturn(Optional.empty());
when(updatedAccount.getPrimaryDevice()).thenReturn(primaryDevice);
@@ -156,8 +156,8 @@ public class ChangeNumberManagerTest {
final UUID accountIdentifier = UUID.randomUUID();
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn(originalNumber);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(accountIdentifier);
when(account.getNumberOptional()).thenReturn(Optional.of(originalNumber));
when(account.getAccountIdentifier()).thenReturn(accountIdentifier);
when(account.isIdentifiedBy(any())).thenReturn(false);
when(account.isIdentifiedBy(new AciServiceIdentifier(accountIdentifier))).thenReturn(true);
@@ -194,8 +194,8 @@ public class ChangeNumberManagerTest {
when(linkedDevice.getRegistrationId(IdentityType.ACI)).thenReturn(linkedDeviceRegistrationId);
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn(originalNumber);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(aci);
when(account.getNumberOptional()).thenReturn(Optional.of(originalNumber));
when(account.getAccountIdentifier()).thenReturn(aci);
when(account.isIdentifiedBy(any())).thenReturn(false);
when(account.isIdentifiedBy(new AciServiceIdentifier(aci))).thenReturn(true);
when(account.getDevice(anyByte())).thenReturn(Optional.empty());
@@ -255,7 +255,7 @@ public class ChangeNumberManagerTest {
.setEphemeral(false)
.build();
verify(messageSender).sendMessages(argThat(a -> a.getIdentifier(IdentityType.ACI).equals(aci)),
verify(messageSender).sendMessages(argThat(a -> a.getAccountIdentifier().equals(aci)),
eq(new AciServiceIdentifier(aci)),
eq(Map.of(linkedDeviceId, expectedEnvelope)),
eq(Map.of(linkedDeviceId, linkedDeviceRegistrationId)),
@@ -280,8 +280,8 @@ public class ChangeNumberManagerTest {
final UUID accountIdentifier = UUID.randomUUID();
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn(targetNumber);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(accountIdentifier);
when(account.getNumberOptional()).thenReturn(Optional.of(targetNumber));
when(account.getAccountIdentifier()).thenReturn(accountIdentifier);
when(account.isIdentifiedBy(any())).thenReturn(false);
when(account.isIdentifiedBy(new AciServiceIdentifier(accountIdentifier))).thenReturn(true);
@@ -319,8 +319,8 @@ public class ChangeNumberManagerTest {
final UUID accountIdentifier = UUID.randomUUID();
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn(originalNumber);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(accountIdentifier);
when(account.getNumberOptional()).thenReturn(Optional.of(originalNumber));
when(account.getAccountIdentifier()).thenReturn(accountIdentifier);
when(account.isIdentifiedBy(any())).thenReturn(false);
when(account.isIdentifiedBy(new AciServiceIdentifier(accountIdentifier))).thenReturn(true);
@@ -367,13 +367,13 @@ public class ChangeNumberManagerTest {
final UUID accountIdentifier = UUID.randomUUID();
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn(originalNumber);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(accountIdentifier);
when(account.getNumberOptional()).thenReturn(Optional.of(originalNumber));
when(account.getAccountIdentifier()).thenReturn(accountIdentifier);
when(account.isIdentifiedBy(any())).thenReturn(false);
when(account.isIdentifiedBy(new AciServiceIdentifier(accountIdentifier))).thenReturn(true);
final Account existingAccount = mock(Account.class);
when(existingAccount.getNumber()).thenReturn(targetNumber);
when(existingAccount.getNumberOptional()).thenReturn(Optional.of(targetNumber));
when(accountsManager.getAccountsForChangeNumber(eq(accountIdentifier), any()))
.thenReturn(new Pair<>(account, Optional.of(existingAccount)));
@@ -397,6 +397,35 @@ public class ChangeNumberManagerTest {
verify(messageSender, never()).sendMessages(any(), any(), any(), any(), any(), any());
}
@Test
void changeNumberAccountHasNoNumber() throws Exception {
final String targetNumber = PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164);
final ECKeyPair pniIdentityKeyPair = ECKeyPair.generate();
final IdentityKey pniIdentityKey = new IdentityKey(ECKeyPair.generate().getPublicKey());
final Map<Byte, ECSignedPreKey> ecSignedPreKeys =
Map.of(Device.PRIMARY_ID, KeysHelper.signedECPreKey(1, pniIdentityKeyPair));
final Map<Byte, KEMSignedPreKey> kemLastResortPreKeys =
Map.of(Device.PRIMARY_ID, KeysHelper.signedKEMPreKey(2, pniIdentityKeyPair));
final UUID accountIdentifier = UUID.randomUUID();
final Account account = mock(Account.class);
when(account.getNumberOptional()).thenReturn(Optional.empty());
when(accountsManager.getAccountsForChangeNumber(eq(accountIdentifier), any()))
.thenReturn(new Pair<>(account, Optional.empty()));
assertThrows(IllegalArgumentException.class,
() -> changeNumberManager.changeNumber(accountIdentifier, null, null, null, targetNumber, pniIdentityKey, ecSignedPreKeys, kemLastResortPreKeys, Collections.emptyList(), Collections.emptyMap(), null, null, null));
verify(accountsManager, never()).changeNumber(any(), any(), any(), any(), any(), any());
verify(messageSender, never()).sendMessages(any(), any(), any(), any(), any(), any());
}
@ParameterizedTest
@MethodSource
void testRecentRegistration(final boolean expectRateLimited, final boolean sameNumber, final boolean waitingPeriodMet) throws Throwable {
@@ -427,8 +456,8 @@ public class ChangeNumberManagerTest {
final UUID accountIdentifier = UUID.randomUUID();
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn(originalNumber);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(accountIdentifier);
when(account.getNumberOptional()).thenReturn(Optional.of(originalNumber));
when(account.getAccountIdentifier()).thenReturn(accountIdentifier);
when(account.isIdentifiedBy(any())).thenReturn(false);
when(account.isIdentifiedBy(new AciServiceIdentifier(accountIdentifier))).thenReturn(true);