Don't allow or set registration lock on accounts with no phone number

This commit is contained in:
ameya-signal authored and GitHub committed 2026-07-29 15:41:22 -07:00
1 parent fc44fb1235
commit ef2d25704e
12 files changed
+95 -13

No files matched your search

@@ -86,6 +86,9 @@ public class RegistrationLockVerificationManager {
final PhoneVerificationRequest.VerificationType phoneVerificationType
) throws RateLimitExceededException, RegistrationLockFailureException {
final String phoneNumber = account.getNumberOptional()
.orElseThrow(() -> new IllegalArgumentException("account does not have a phone number"));
final Tags expiredTags = Tags.of(UserAgentTagUtil.getPlatformTag(userAgent),
Tag.of(REGISTRATION_LOCK_VERIFICATION_FLOW_TAG_NAME, flow.name()),
Tag.of(PHONE_VERIFICATION_TYPE_TAG_NAME, phoneVerificationType.name())
@@ -106,10 +109,9 @@ public class RegistrationLockVerificationManager {
}
if (StringUtils.isNotEmpty(clientRegistrationLock)) {
rateLimiters.getPinLimiter().validate(account.getNumber());
rateLimiters.getPinLimiter().validate(phoneNumber);
}
final String phoneNumber = account.getNumber();
final boolean registrationLockMatches = existingRegistrationLock.verify(clientRegistrationLock);
final boolean alreadyLocked = account.hasLockedCredentials();
@@ -166,8 +166,12 @@ public class AccountController {
public void setRegistrationLock(@Auth AuthenticatedDevice auth, @NotNull @Valid RegistrationLock accountLock) {
final SaltedTokenHash credentials = SaltedTokenHash.generateFor(accountLock.registrationLock());
accounts.update(auth.accountIdentifier(),
a -> a.setRegistrationLock(credentials.hash(), credentials.salt()));
try {
accounts.update(auth.accountIdentifier(),
a -> a.setRegistrationLock(credentials.hash(), credentials.salt()));
} catch (IllegalArgumentException _) {
throw new BadRequestException();
}
}
@DELETE
@@ -186,6 +186,7 @@ public class RegistrationController {
}
if (existingAccount.isPresent()) {
assert existingAccount.get().getNumberOptional().isPresent();
registrationLockVerificationManager.verifyRegistrationLock(existingAccount.get(),
registrationRequest.accountAttributes().getRegistrationLock(),
userAgent, RegistrationLockVerificationManager.Flow.REGISTRATION, verificationType);
@@ -171,10 +171,14 @@ public class AccountsGrpcService extends SimpleAccountsGrpc.AccountsImplBase {
final SaltedTokenHash credentials =
SaltedTokenHash.generateFor(formatRegistrationLock(request.getRegistrationLock().toByteArray()));
accountsManager.update(AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier(),
account -> account.setRegistrationLock(credentials.hash(), credentials.salt()));
try {
accountsManager.update(AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier(),
account -> account.setRegistrationLock(credentials.hash(), credentials.salt()));
return SetRegistrationLockResponse.getDefaultInstance();
return SetRegistrationLockResponse.getDefaultInstance();
} catch (IllegalArgumentException _) {
throw GrpcExceptions.invalidArguments(null);
}
}
@Override
@@ -530,6 +530,10 @@ public class Account {
public void setRegistrationLock(final String registrationLock, final String registrationLockSalt) {
requireNotStale();
if (number == null) {
throw new IllegalArgumentException("Cannot set registration lock on account with no phone number");
}
this.registrationLock = registrationLock;
this.registrationLockSalt = registrationLockSalt;
}
@@ -122,6 +122,7 @@ public class ChangeNumberManager {
final Optional<Account> existingAccount = accountAndMaybeExistingAccount.second();
if (existingAccount.isPresent()) {
assert existingAccount.get().getNumberOptional().isPresent();
registrationLockVerificationManager.verifyRegistrationLock(existingAccount.get(), registrationLock,
userAgent, RegistrationLockVerificationManager.Flow.CHANGE_NUMBER, verificationType);
}
@@ -19,11 +19,13 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@@ -76,7 +78,7 @@ class RegistrationLockVerificationManagerTest {
final UUID accountIdentifier = UUID.randomUUID();
when(account.getAccountIdentifier()).thenReturn(accountIdentifier);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(accountIdentifier);
when(account.getNumber()).thenReturn("+18005551212");
when(account.getNumberOptional()).thenReturn(Optional.of("+18005551212"));
when(account.getDevices()).thenReturn(List.of(device));
AccountsHelper.setupMockGet(accountsManager, account);
@@ -185,4 +187,12 @@ class RegistrationLockVerificationManagerTest {
);
}
@Test
void testAccountWithNoPhoneNumber() {
when(account.getNumberOptional()).thenReturn(Optional.empty());
assertThrows(IllegalArgumentException.class,
() -> registrationLockVerificationManager.verifyRegistrationLock(account, null, null,
RegistrationLockVerificationManager.Flow.REGISTRATION, PhoneVerificationRequest.VerificationType.SESSION));
}
}
@@ -214,6 +214,7 @@ class AccountControllerTest {
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT));
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID_TWO)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT_TWO));
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID_3)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT_3));
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID_4)).thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT_4));
when(accountsManager.getByAccountIdentifier(AuthHelper.UNDISCOVERABLE_UUID)).thenReturn(Optional.of(AuthHelper.UNDISCOVERABLE_ACCOUNT));
doAnswer(invocation -> {
@@ -268,6 +269,19 @@ class AccountControllerTest {
}
}
@Test
void testSetRegistrationLockAccountHasNoNumber() {
doThrow(new IllegalArgumentException()).when(AuthHelper.VALID_ACCOUNT_4).setRegistrationLock(any(), any());
try (final Response response = resources.getJerseyTest()
.target("/v1/accounts/registration_lock/")
.request()
.header(HttpHeaders.AUTHORIZATION, AuthHelper.getAuthHeader(AuthHelper.VALID_UUID_4, AuthHelper.VALID_PASSWORD_4))
.put(Entity.json(new RegistrationLock("1234567890123456789012345678901234567890123456789012345678901234")))) {
assertThat(response.getStatus()).isEqualTo(400);
}
}
@Test
void testSetShortRegistrationLock() {
try (final Response response = resources.getJerseyTest()
@@ -266,6 +266,7 @@ class RegistrationControllerTest {
final Account account = mock(Account.class);
when(accountsManager.getByE164(any())).thenReturn(Optional.of(account));
when(account.getNumberOptional()).thenReturn(Optional.of(NUMBER));
when(account.hasCapability(DeviceCapability.TRANSFER)).thenReturn(deviceTransferSupported);
final int expectedStatus;
@@ -317,6 +318,7 @@ class RegistrationControllerTest {
final Account account = mock(Account.class);
when(accountsManager.getByE164(oldFormatBeninNumber)).thenReturn(Optional.of(account));
when(account.getNumberOptional()).thenReturn(Optional.of(oldFormatBeninNumber));
when(accountsManager.getByE164(newFormatBeninNumber)).thenReturn(Optional.empty());
doThrow(new WebApplicationException(RegistrationLockError.MISMATCH.getExpectedStatus()))
@@ -347,6 +349,7 @@ class RegistrationControllerTest {
if (existingAccount) {
final Account account = mock(Account.class);
when(account.hasCapability(DeviceCapability.TRANSFER)).thenReturn(transferSupported);
when(account.getNumberOptional()).thenReturn(Optional.of(NUMBER));
maybeAccount = Optional.of(account);
} else {
maybeAccount = Optional.empty();
@@ -650,9 +653,10 @@ class RegistrationControllerTest {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void reregistrationFlag(final boolean existingAccount) throws InterruptedException {
final Optional<Account> maybeAccount = Optional.ofNullable(existingAccount ? mock(Account.class) : null);
when(accountsManager.getByE164(any())).thenReturn(maybeAccount);
void reregistrationFlag(final boolean accountExists) throws InterruptedException {
final Account existingAccount = mock(Account.class);
when(existingAccount.getNumberOptional()).thenReturn(Optional.of(NUMBER));
when(accountsManager.getByE164(any())).thenReturn(accountExists ? Optional.of(existingAccount) : Optional.empty());
final Account account = mock(Account.class);
when(account.getPrimaryDevice()).thenReturn(mock(Device.class));
@@ -668,7 +672,7 @@ class RegistrationControllerTest {
try (Response response = request.post(Entity.json(requestJson("sessionId")))) {
assertEquals(200, response.getStatus());
final AccountCreationResponse creationResponse = response.readEntity(AccountCreationResponse.class);
assertEquals(existingAccount, creationResponse.reregistration());
assertEquals(accountExists, creationResponse.reregistration());
}
}
@@ -216,7 +216,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
@ParameterizedTest
@ValueSource(bytes = { Device.PRIMARY_ID, Device.PRIMARY_ID + 1 })
void setRegistrationLock(final byte deviceId) {
getMockAuthenticationInterceptor().setAuthenticatedDevice(AUTHENTICATED_ACI, (byte) (deviceId));
getMockAuthenticationInterceptor().setAuthenticatedDevice(AUTHENTICATED_ACI, deviceId);
final Account account = mock(Account.class);
@@ -248,6 +248,25 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
verify(accountsManager, never()).update(any(UUID.class), any());
}
@Test
void setRegistrationLockAccountHasNoNumber() {
getMockAuthenticationInterceptor().setAuthenticatedDevice(AUTHENTICATED_ACI, Device.PRIMARY_ID);
final Account account = mock(Account.class);
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI))
.thenReturn(Optional.of(account));
final String message = "no phone number";
doThrow(new IllegalArgumentException(message)).when(account).setRegistrationLock(any(), any());
final byte[] registrationLockSecret = TestRandomUtil.nextBytes(32);
GrpcTestUtils.assertStatusException(Status.INVALID_ARGUMENT,
() -> authenticatedServiceStub().setRegistrationLock(SetRegistrationLockRequest.newBuilder()
.setRegistrationLock(ByteString.copyFrom(registrationLockSecret))
.build()));
}
@ParameterizedTest
@ValueSource(bytes = { Device.PRIMARY_ID, Device.PRIMARY_ID + 1 })
void clearRegistrationLock(final byte deviceId) {
@@ -224,6 +224,7 @@ public class AccountsHelper {
case "getIdentifier" -> when(updatedAccount.getIdentifier(stubbing.getInvocation().getArgument(0))).thenAnswer(stubbing);
case "isIdentifiedBy" -> when(updatedAccount.isIdentifiedBy(stubbing.getInvocation().getArgument(0))).thenAnswer(stubbing);
case "getNumber" -> when(updatedAccount.getNumber()).thenAnswer(stubbing);
case "getNumberOptional" -> when(updatedAccount.getNumberOptional()).thenAnswer(stubbing);
case "getUsername" -> when(updatedAccount.getUsernameHash()).thenAnswer(stubbing);
case "getUsernameHash" -> when(updatedAccount.getUsernameHash()).thenAnswer(stubbing);
case "getUsernameLinkHandle" -> when(updatedAccount.getUsernameLinkHandle()).thenAnswer(stubbing);
@@ -79,6 +79,9 @@ public class AuthHelper {
public static final String VALID_PASSWORD_3_PRIMARY = "3primary";
public static final String VALID_PASSWORD_3_LINKED = "3linked";
public static final UUID VALID_UUID_4 = UUID.randomUUID();
public static final String VALID_PASSWORD_4 = "blah";
public static final UUID INVALID_UUID = UUID.randomUUID();
public static final String INVALID_PASSWORD = "bar";
@@ -98,12 +101,14 @@ public class AuthHelper {
public static Account VALID_ACCOUNT_TWO = mock(Account.class );
public static Account UNDISCOVERABLE_ACCOUNT = mock(Account.class );
public static Account VALID_ACCOUNT_3 = mock(Account.class );
public static Account VALID_ACCOUNT_4 = mock(Account.class );
public static Device VALID_DEVICE = mock(Device.class);
public static Device VALID_DEVICE_TWO = mock(Device.class);
public static Device UNDISCOVERABLE_DEVICE = mock(Device.class);
public static Device VALID_DEVICE_3_PRIMARY = mock(Device.class);
public static Device VALID_DEVICE_3_LINKED = mock(Device.class);
public static Device VALID_DEVICE_4 = mock(Device.class);
public static final byte VALID_DEVICE_3_LINKED_ID = Device.PRIMARY_ID + 1;
@@ -111,6 +116,7 @@ public class AuthHelper {
private static SaltedTokenHash VALID_CREDENTIALS_TWO = mock(SaltedTokenHash.class);
private static SaltedTokenHash VALID_CREDENTIALS_3_PRIMARY = mock(SaltedTokenHash.class);
private static SaltedTokenHash VALID_CREDENTIALS_3_LINKED = mock(SaltedTokenHash.class);
private static SaltedTokenHash VALID_CREDENTIALS_4 = mock(SaltedTokenHash.class);
private static SaltedTokenHash UNDISCOVERABLE_CREDENTIALS = mock(SaltedTokenHash.class);
private static final Collection<TestAccount> EXTENSION_TEST_ACCOUNTS = new HashSet<>();
@@ -120,12 +126,14 @@ public class AuthHelper {
when(VALID_CREDENTIALS_TWO.verify("baz")).thenReturn(true);
when(VALID_CREDENTIALS_3_PRIMARY.verify(VALID_PASSWORD_3_PRIMARY)).thenReturn(true);
when(VALID_CREDENTIALS_3_LINKED.verify(VALID_PASSWORD_3_LINKED)).thenReturn(true);
when(VALID_CREDENTIALS_4.verify(VALID_PASSWORD_4)).thenReturn(true);
when(UNDISCOVERABLE_CREDENTIALS.verify(UNDISCOVERABLE_PASSWORD)).thenReturn(true);
when(VALID_DEVICE.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS);
when(VALID_DEVICE_TWO.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS_TWO);
when(VALID_DEVICE_3_PRIMARY.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS_3_PRIMARY);
when(VALID_DEVICE_3_LINKED.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS_3_LINKED);
when(VALID_DEVICE_4.getAuthTokenHash()).thenReturn(VALID_CREDENTIALS_4);
when(UNDISCOVERABLE_DEVICE.getAuthTokenHash()).thenReturn(UNDISCOVERABLE_CREDENTIALS);
when(VALID_DEVICE.isPrimary()).thenReturn(true);
@@ -133,12 +141,14 @@ public class AuthHelper {
when(UNDISCOVERABLE_DEVICE.isPrimary()).thenReturn(true);
when(VALID_DEVICE_3_PRIMARY.isPrimary()).thenReturn(true);
when(VALID_DEVICE_3_LINKED.isPrimary()).thenReturn(false);
when(VALID_DEVICE_4.isPrimary()).thenReturn(true);
when(VALID_DEVICE.getId()).thenReturn(Device.PRIMARY_ID);
when(VALID_DEVICE_TWO.getId()).thenReturn(Device.PRIMARY_ID);
when(UNDISCOVERABLE_DEVICE.getId()).thenReturn(Device.PRIMARY_ID);
when(VALID_DEVICE_3_PRIMARY.getId()).thenReturn(Device.PRIMARY_ID);
when(VALID_DEVICE_3_LINKED.getId()).thenReturn(VALID_DEVICE_3_LINKED_ID);
when(VALID_DEVICE_4.getId()).thenReturn(Device.PRIMARY_ID);
when(UNDISCOVERABLE_DEVICE.isPrimary()).thenReturn(true);
@@ -151,11 +161,14 @@ public class AuthHelper {
when(VALID_ACCOUNT_3.getDevice(Device.PRIMARY_ID)).thenReturn(Optional.of(VALID_DEVICE_3_PRIMARY));
when(VALID_ACCOUNT_3.getPrimaryDevice()).thenReturn(VALID_DEVICE_3_PRIMARY);
when(VALID_ACCOUNT_3.getDevice((byte) 2)).thenReturn(Optional.of(VALID_DEVICE_3_LINKED));
when(VALID_ACCOUNT_4.getDevice(Device.PRIMARY_ID)).thenReturn(Optional.of(VALID_DEVICE_4));
when(VALID_ACCOUNT_4.getPrimaryDevice()).thenReturn(VALID_DEVICE_4);
when(VALID_ACCOUNT.getDevices()).thenReturn(List.of(VALID_DEVICE));
when(VALID_ACCOUNT_TWO.getDevices()).thenReturn(List.of(VALID_DEVICE_TWO));
when(UNDISCOVERABLE_ACCOUNT.getDevices()).thenReturn(List.of(UNDISCOVERABLE_DEVICE));
when(VALID_ACCOUNT_3.getDevices()).thenReturn(List.of(VALID_DEVICE_3_PRIMARY, VALID_DEVICE_3_LINKED));
when(VALID_ACCOUNT_4.getDevices()).thenReturn(List.of(VALID_DEVICE_4));
when(VALID_ACCOUNT.getNumber()).thenReturn(VALID_NUMBER);
when(VALID_ACCOUNT.getNumberOptional()).thenReturn(Optional.of(VALID_NUMBER));
@@ -202,6 +215,9 @@ public class AuthHelper {
when(VALID_ACCOUNT.getIdentityKey(IdentityType.ACI)).thenReturn(VALID_IDENTITY);
when(VALID_ACCOUNT.getIdentityKey(IdentityType.PNI)).thenReturn(VALID_PNI_IDENTITY);
when(VALID_ACCOUNT_4.getAccountIdentifier()).thenReturn(VALID_UUID_4);
when(VALID_ACCOUNT_4.getIdentifier(IdentityType.ACI)).thenReturn(VALID_UUID_4);
reset(ACCOUNTS_MANAGER);
when(ACCOUNTS_MANAGER.getByE164(VALID_NUMBER)).thenReturn(Optional.of(VALID_ACCOUNT));
@@ -219,6 +235,8 @@ public class AuthHelper {
when(ACCOUNTS_MANAGER.getByAccountIdentifier(VALID_UUID_3)).thenReturn(Optional.of(VALID_ACCOUNT_3));
when(ACCOUNTS_MANAGER.getByPhoneNumberIdentifier(VALID_PNI_3)).thenReturn(Optional.of(VALID_ACCOUNT_3));
when(ACCOUNTS_MANAGER.getByAccountIdentifier(VALID_UUID_4)).thenReturn(Optional.of(VALID_ACCOUNT_4));
AccountsHelper.setupMockUpdateForAuthHelper(ACCOUNTS_MANAGER);
for (TestAccount testAccount : TEST_ACCOUNTS) {