Don't try to decode null session IDs

This commit is contained in:
Jon Chambers authored and GitHub committed 2026-04-17 14:51:07 -04:00
1 parent 52b4e0b87f
commit 32c11f4047
4 files changed
+27 -11

No files matched your search

@@ -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(),
@@ -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.
@@ -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)),
@@ -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