From 7489c62fb7739184005bb847d82dcd0c870c21f8 Mon Sep 17 00:00:00 2001 From: Ravi Khadiwala Date: Wed, 2 Sep 2026 14:24:31 -0500 Subject: [PATCH] Avoid attribute modification errors on numberless accounts --- .../controllers/AccountController.java | 4 ++++ .../textsecuregcm/storage/Account.java | 5 +++-- .../controllers/AccountControllerTest.java | 13 +++++++++++++ .../textsecuregcm/storage/AccountTest.java | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java index 8bd8c6ed3..065cd30b4 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java @@ -239,6 +239,10 @@ public class AccountController { } }); + if (StringUtils.isNotEmpty(attributes.getRegistrationLock()) && a.getNumber().isEmpty()) { + throw new BadRequestException("account does not have a phone number"); + } + a.setRegistrationLockFromAttributes(attributes); a.setUnidentifiedAccessKey(attributes.getUnidentifiedAccessKey()); a.setUnrestrictedUnidentifiedAccess(attributes.isUnrestrictedUnidentifiedAccess()); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java index 45be8ae1c..af47cf58f 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java @@ -475,10 +475,11 @@ public class Account { } } - public void setRegistrationLock(final String registrationLock, final String registrationLockSalt) { + public void setRegistrationLock(@Nullable final String registrationLock, @Nullable final String registrationLockSalt) { requireNotStale(); - if (number == null) { + // Accounts without a phone number can't set a registration lock + if (number == null && (registrationLock != null || registrationLockSalt != null)) { throw new IllegalArgumentException("Cannot set registration lock on account with no phone number"); } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java index 1c86385f7..aa3e4f9ad 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java @@ -869,6 +869,19 @@ class AccountControllerTest { } } + @Test + void testSetAccountAttributesRegistrationLockWithNoNumber() { + try (final Response response = resources.getJerseyTest() + .target("/v1/accounts/attributes/") + .request() + .header(HttpHeaders.AUTHORIZATION, AuthHelper.getAuthHeader(AuthHelper.NUMBERLESS_UUID, AuthHelper.NUMBERLESS_PASSWORD)) + .put(Entity.json(new AccountAttributes(false, 2222, 3333, null, "1234", false, null, null) + .setUnidentifiedAccessKey(new byte[16])))) { + + assertThat(response.getStatus()).isEqualTo(422); + } + } + @Test void testSetAccountAttributesEnableDiscovery() { try (final Response response = resources.getJerseyTest() diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java index da1771b9e..7bb549ae7 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java @@ -37,6 +37,7 @@ 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; +import org.whispersystems.textsecuregcm.auth.StoredRegistrationLock; import org.whispersystems.textsecuregcm.tests.util.AccountsHelper; import org.whispersystems.textsecuregcm.util.SystemMapper; import org.whispersystems.textsecuregcm.util.TestClock; @@ -108,6 +109,20 @@ class AccountTest { } } + @Test + void testSetRegistrationLockWithNoPhoneNumber() { + final Account numberlessAccount = + AccountsHelper.generateTestAccount(null, UUID.randomUUID(), null, List.of(recentPrimaryDevice), null); + + assertThrows(IllegalArgumentException.class, () -> numberlessAccount.setRegistrationLock("hash", "salt"), + "Accounts without phone numbers should never have a registration lock"); + + assertDoesNotThrow(() -> numberlessAccount.setRegistrationLock(null, null), + "Clearing a registration lock an account never had should be a no-op"); + + assertEquals(StoredRegistrationLock.Status.ABSENT, numberlessAccount.getRegistrationLock().getStatus()); + } + @Test void testDiscoverableByPhoneNumber() { final Account account = AccountsHelper.generateTestAccount("+14152222222", UUID.randomUUID(), UUID.randomUUID(), List.of(recentPrimaryDevice),