From de5cb3370d844f1a1154ae713f5c300891b4ab2e Mon Sep 17 00:00:00 2001 From: Jon Chambers <63609320+jon-signal@users.noreply.github.com> Date: Fri, 17 Apr 2026 14:51:07 -0400 Subject: [PATCH] Don't try to decode null session IDs --- .../controllers/AccountControllerV2.java | 3 ++- .../controllers/RegistrationController.java | 6 +++++- .../controllers/AccountControllerV2Test.java | 10 +++++++--- .../RegistrationControllerTest.java | 19 +++++++++++++------ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2.java index 5a1588175..0b9dec6ef 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2.java @@ -29,6 +29,7 @@ import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import java.time.Instant; import java.util.UUID; +import org.apache.commons.lang3.StringUtils; import org.whispersystems.textsecuregcm.auth.AuthenticatedDevice; import org.whispersystems.textsecuregcm.auth.ChangesPhoneNumber; import org.whispersystems.textsecuregcm.entities.AccountDataReportResponse; @@ -91,7 +92,7 @@ public class AccountControllerV2 { try { final Account updatedAccount = changeNumberManager.changeNumber( authenticatedDevice.accountIdentifier(), - request.decodeSessionId(), + StringUtils.isNotBlank(request.sessionId()) ? request.decodeSessionId() : null, request.recoveryPassword(), request.registrationLock(), request.number(), diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java index e277f4deb..bfc543388 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java @@ -34,6 +34,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.Collections; import java.util.Optional; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.whispersystems.textsecuregcm.auth.BasicAuthorizationHeader; @@ -134,7 +135,10 @@ public class RegistrationController { rateLimiters.getRegistrationLimiter().validate(number); final PhoneVerificationRequest.VerificationType verificationType = - phoneVerificationTokenManager.verify(requestContext, number, registrationRequest.decodeSessionId(), registrationRequest.recoveryPassword()); + phoneVerificationTokenManager.verify(requestContext, + number, + StringUtils.isNotBlank(registrationRequest.sessionId()) ? registrationRequest.decodeSessionId() : null, + registrationRequest.recoveryPassword()); // There can be at most one existing account for a set of numbers in the same equivalence class, so it's sufficient // to find the first one. diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2Test.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2Test.java index d900adb95..b72c62f7b 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2Test.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2Test.java @@ -59,6 +59,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.ArgumentCaptor; import org.mockito.stubbing.Answer; import org.signal.libsignal.protocol.IdentityKey; @@ -151,8 +152,11 @@ class AccountControllerV2Test { }); } - @Test - void changeNumberSuccess() throws Exception { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void changeNumberSuccess(final boolean useSessionVerification) throws Exception { + @Nullable final String sessionId = useSessionVerification ? encodeSessionId("session") : null; + @Nullable final byte[] recoveryPassword = useSessionVerification ? null : "recovery-password".getBytes(StandardCharsets.UTF_8); final AccountIdentityResponse accountIdentityResponse = resources.getJerseyTest() @@ -161,7 +165,7 @@ class AccountControllerV2Test { .header(HttpHeaders.AUTHORIZATION, AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD)) .put(Entity.entity( - new ChangeNumberRequest(encodeSessionId("session"), null, NEW_NUMBER, "123", IDENTITY_KEY, + new ChangeNumberRequest(sessionId, recoveryPassword, 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)), diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/RegistrationControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/RegistrationControllerTest.java index d16932aae..779eec0bd 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/RegistrationControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/RegistrationControllerTest.java @@ -369,8 +369,9 @@ class RegistrationControllerTest { } // this is functionally the same as deviceTransferAvailable(existingAccount=false) - @Test - void registrationSuccess() throws Exception { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void registrationSuccess(final boolean useSessionVerification) throws Exception { final Account account = mock(Account.class); when(account.getPrimaryDevice()).thenReturn(mock(Device.class)); @@ -382,13 +383,19 @@ class RegistrationControllerTest { .request() .header(HttpHeaders.AUTHORIZATION, AuthHelper.getProvisioningAuthHeader(NUMBER, PASSWORD)); - try (Response response = request.post(Entity.json(requestJson("sessionId")))) { + final String requestJson = useSessionVerification + ? requestJson("sessionId") + : requestJsonRecoveryPassword("recovery-password".getBytes(StandardCharsets.UTF_8)); + + try (Response response = request.post(Entity.json(requestJson))) { assertEquals(200, response.getStatus()); } - verify(registrationFraudChecker) - .handleVerificationCompleted(Base64.getEncoder().encodeToString("sessionId".getBytes(StandardCharsets.UTF_8)), - account); + if (useSessionVerification) { + verify(registrationFraudChecker) + .handleVerificationCompleted(Base64.getEncoder().encodeToString("sessionId".getBytes(StandardCharsets.UTF_8)), + account); + } } @ParameterizedTest