Don't assume presence of phone numbers when checking for prohibited payment regions

This commit is contained in:
Jon Chambers authored and GitHub committed 2026-07-29 15:41:51 -07:00
1 parent 78e7e992da
commit 2f1843d5d2
11 files changed
+242 -60

No files matched your search

@@ -252,7 +252,7 @@ class DynamicConfigurationTest {
final DynamicConfiguration emptyConfig =
DynamicConfigurationManager.parseConfiguration(emptyConfigYaml, DynamicConfiguration.class).orElseThrow();
assertTrue(emptyConfig.getPaymentsConfiguration().getDisallowedPrefixes().isEmpty());
assertTrue(emptyConfig.getPaymentsConfiguration().disallowedPrefixes().isEmpty());
}
{
@@ -266,7 +266,7 @@ class DynamicConfigurationTest {
DynamicConfigurationManager.parseConfiguration(paymentsConfigYaml, DynamicConfiguration.class).orElseThrow()
.getPaymentsConfiguration();
assertEquals(List.of("+44"), config.getDisallowedPrefixes());
assertEquals(List.of("+44"), config.disallowedPrefixes());
}
}
@@ -79,6 +79,7 @@ import org.signal.libsignal.zkgroup.profiles.ProfileKeyCommitment;
import org.signal.libsignal.zkgroup.profiles.ProfileKeyCredentialRequest;
import org.signal.libsignal.zkgroup.profiles.ProfileKeyCredentialRequestContext;
import org.signal.libsignal.zkgroup.profiles.ServerZkProfileOperations;
import org.whispersystems.textsecuregcm.asn.AsnInfoProvider;
import org.whispersystems.textsecuregcm.auth.AuthenticatedDevice;
import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessUtil;
import org.whispersystems.textsecuregcm.badges.ProfileBadgeConverter;
@@ -161,6 +162,7 @@ class ProfileControllerTest {
rateLimiters,
accountsManager,
profilesManager,
() -> AsnInfoProvider.EMPTY,
dynamicConfigurationManager,
new ProfileBadgeConverter() {
@Override
@@ -202,7 +204,7 @@ class ProfileControllerTest {
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
when(dynamicConfiguration.getPaymentsConfiguration()).thenReturn(dynamicPaymentsConfiguration);
when(dynamicPaymentsConfiguration.getDisallowedPrefixes()).thenReturn(Collections.emptyList());
when(dynamicPaymentsConfiguration.disallowedPrefixes()).thenReturn(Collections.emptyList());
when(rateLimiters.getProfileLimiter()).thenReturn(rateLimiter);
when(rateLimiters.getUsernameLookupLimiter()).thenReturn(usernameRateLimiter);
@@ -212,7 +214,6 @@ class ProfileControllerTest {
when(profileAccount.getIdentityKey(IdentityType.ACI)).thenReturn(ACCOUNT_TWO_IDENTITY_KEY);
when(profileAccount.getIdentityKey(IdentityType.PNI)).thenReturn(ACCOUNT_TWO_PHONE_NUMBER_IDENTITY_KEY);
when(profileAccount.getAccountIdentifier()).thenReturn(AuthHelper.VALID_UUID_TWO);
when(profileAccount.getIdentifier(IdentityType.ACI)).thenReturn(AuthHelper.VALID_UUID_TWO);
when(profileAccount.getPhoneNumberIdentifier()).thenReturn(AuthHelper.VALID_PNI_TWO);
when(profileAccount.getCurrentProfileVersion()).thenReturn(Optional.empty());
when(profileAccount.getUsernameHash()).thenReturn(Optional.of(USERNAME_HASH));
@@ -223,7 +224,6 @@ class ProfileControllerTest {
capabilitiesAccount = mock(Account.class);
when(capabilitiesAccount.getAccountIdentifier()).thenReturn(AuthHelper.VALID_UUID);
when(capabilitiesAccount.getIdentifier(IdentityType.ACI)).thenReturn(AuthHelper.VALID_UUID);
when(capabilitiesAccount.getIdentityKey(IdentityType.ACI)).thenReturn(ACCOUNT_IDENTITY_KEY);
when(capabilitiesAccount.getIdentityKey(IdentityType.PNI)).thenReturn(ACCOUNT_PHONE_NUMBER_IDENTITY_KEY);
@@ -806,7 +806,7 @@ class ProfileControllerTest {
@Test
void testSetProfilePaymentAddressCountryNotAllowed() throws InvalidInputException {
when(dynamicPaymentsConfiguration.getDisallowedPrefixes())
when(dynamicPaymentsConfiguration.disallowedPrefixes())
.thenReturn(List.of(AuthHelper.VALID_NUMBER_TWO.substring(0, 3)));
final ProfileKeyCommitment commitment = new ProfileKey(new byte[32]).getCommitment(new ServiceId.Aci(AuthHelper.VALID_UUID));
@@ -836,7 +836,7 @@ class ProfileControllerTest {
@MethodSource
void testSetProfilePaymentAddressCountryNotAllowedExistingPaymentAddress(
@Nullable final byte[] existingPaymentAddressOnProfile, final byte[] requestPaymentAddress, final boolean expectAllowed) throws InvalidInputException {
when(dynamicPaymentsConfiguration.getDisallowedPrefixes())
when(dynamicPaymentsConfiguration.disallowedPrefixes())
.thenReturn(List.of(AuthHelper.VALID_NUMBER_TWO.substring(0, 3)));
final ProfileKeyCommitment commitment = new ProfileKey(new byte[32]).getCommitment(new ServiceId.Aci(AuthHelper.VALID_UUID));
@@ -81,6 +81,7 @@ import org.signal.libsignal.zkgroup.avatars.AvatarUploadCredentialRequest;
import org.signal.libsignal.zkgroup.avatars.AvatarUploadCredentialRequestContext;
import org.signal.libsignal.zkgroup.avatars.AvatarUploadCredentialResponse;
import org.signal.libsignal.zkgroup.profiles.ProfileKey;
import org.whispersystems.textsecuregcm.asn.AsnInfoProvider;
import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessChecksum;
import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessUtil;
import org.whispersystems.textsecuregcm.badges.ProfileBadgeConverter;
@@ -183,8 +184,7 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
when(dynamicConfiguration.getPaymentsConfiguration()).thenReturn(dynamicPaymentsConfiguration);
when(account.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI);
when(account.getIdentifier(org.whispersystems.textsecuregcm.identity.IdentityType.ACI)).thenReturn(AUTHENTICATED_ACI);
when(account.getNumber()).thenReturn(phoneNumber);
when(account.getNumberOptional()).thenReturn(Optional.of(phoneNumber));
when(account.getBadges()).thenReturn(Collections.emptyList());
when(account.hasCapability(DeviceCapability.PROFILES_V2)).thenReturn(true);
@@ -198,7 +198,8 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
when(dynamicConfiguration.getPaymentsConfiguration()).thenReturn(dynamicPaymentsConfiguration);
when(dynamicPaymentsConfiguration.getDisallowedPrefixes()).thenReturn(Collections.emptyList());
when(dynamicPaymentsConfiguration.disallowedPrefixes()).thenReturn(Collections.emptyList());
when(dynamicPaymentsConfiguration.disallowedAsnRegions()).thenReturn(Collections.emptyList());
when(profilesManager.deleteAvatar(anyString())).thenReturn(CompletableFuture.completedFuture(null));
@@ -206,6 +207,7 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
clock,
accountsManager,
profilesManager,
() -> AsnInfoProvider.EMPTY,
dynamicConfigurationManager,
badgesConfiguration,
policyGenerator,
@@ -510,10 +512,9 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
.setV1Request(V1_REQUEST)
.build();
final String disallowedCountryCode = String.format("+%d", disallowedPhoneNumber.getCountryCode());
when(dynamicPaymentsConfiguration.getDisallowedPrefixes()).thenReturn(List.of(disallowedCountryCode));
when(account.getNumber()).thenReturn(PhoneNumberUtil.getInstance().format(
disallowedPhoneNumber,
PhoneNumberUtil.PhoneNumberFormat.E164));
when(dynamicPaymentsConfiguration.disallowedPrefixes()).thenReturn(List.of(disallowedCountryCode));
when(account.getNumberOptional()).thenReturn(Optional.of(PhoneNumberUtil.getInstance().format(
disallowedPhoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164)));
when(profilesManager.getV1(any(), anyString())).thenReturn(Optional.of(profile));
final SetProfileResponse response = authenticatedServiceStub().setProfile(request);
@@ -0,0 +1,145 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.util;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.whispersystems.textsecuregcm.asn.AsnInfo;
import org.whispersystems.textsecuregcm.asn.AsnInfoProvider;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicPaymentsConfiguration;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
import org.whispersystems.textsecuregcm.storage.VersionedProfile;
import org.whispersystems.textsecuregcm.storage.VersionedProfileV1;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class ProfileHelperTest {
private enum PhoneNumberType {
ALLOWED,
FORBIDDEN,
NONE
}
private enum IpAddressType {
ALLOWED,
FORBIDDEN,
UNRECOGNIZED
}
private enum ExistingPaymentAddressType {
CURRENT_PROFILE,
LEGACY_PROFILE,
NONE
}
@ParameterizedTest
@MethodSource
void isPaymentAddressUpdateForbidden(final PhoneNumberType phoneNumberType,
final IpAddressType ipAddressType,
final ExistingPaymentAddressType existingPaymentAddressType,
final boolean expectForbidden) {
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
when(dynamicConfiguration.getPaymentsConfiguration())
.thenReturn(new DynamicPaymentsConfiguration(List.of("+1"), List.of("US")));
@SuppressWarnings("unchecked") final DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager =
mock(DynamicConfigurationManager.class);
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
final Account account = mock(Account.class);
switch (phoneNumberType) {
case ALLOWED -> when(account.getNumberOptional())
.thenReturn(Optional.of(PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164)));
case FORBIDDEN -> when(account.getNumberOptional())
.thenReturn(Optional.of(PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164)));
case NONE -> when(account.getNumberOptional()).thenReturn(Optional.empty());
}
final AsnInfoProvider asnInfoProvider = mock(AsnInfoProvider.class);
switch (ipAddressType) {
case ALLOWED -> when(asnInfoProvider.lookup(anyString())).thenReturn(Optional.of(new AsnInfo(123, "DE")));
case FORBIDDEN -> when(asnInfoProvider.lookup(anyString())).thenReturn(Optional.of(new AsnInfo(123, "US")));
case UNRECOGNIZED -> when(asnInfoProvider.lookup(anyString())).thenReturn(Optional.empty());
}
final Optional<VersionedProfile> maybeProfile = switch (existingPaymentAddressType) {
case CURRENT_PROFILE -> Optional.of(new VersionedProfile(TestRandomUtil.nextBytes(16),
TestRandomUtil.nextBytes(16),
TestRandomUtil.nextBytes(16),
TestRandomUtil.nextBytes(16)));
case LEGACY_PROFILE, NONE -> Optional.empty();
};
final Optional<VersionedProfileV1> maybeLegacyProfile = switch (existingPaymentAddressType) {
case LEGACY_PROFILE -> Optional.of(new VersionedProfileV1("version",
TestRandomUtil.nextBytes(16),
"avatar",
TestRandomUtil.nextBytes(16),
TestRandomUtil.nextBytes(16),
TestRandomUtil.nextBytes(16),
TestRandomUtil.nextBytes(16),
TestRandomUtil.nextBytes(16)));
case CURRENT_PROFILE, NONE -> Optional.empty();
};
assertEquals(expectForbidden, ProfileHelper.isPaymentAddressUpdateForbidden(account,
maybeProfile,
maybeLegacyProfile,
"127.0.0.1",
asnInfoProvider,
dynamicConfigurationManager));
}
private static List<Arguments> isPaymentAddressUpdateForbidden() {
return List.of(
Arguments.argumentSet("Permitted phone number",
PhoneNumberType.ALLOWED, IpAddressType.ALLOWED, ExistingPaymentAddressType.NONE, false),
Arguments.argumentSet("Forbidden phone number",
PhoneNumberType.FORBIDDEN, IpAddressType.ALLOWED, ExistingPaymentAddressType.NONE, true),
Arguments.argumentSet("Forbidden phone number, has existing address",
PhoneNumberType.FORBIDDEN, IpAddressType.ALLOWED, ExistingPaymentAddressType.CURRENT_PROFILE, false),
Arguments.argumentSet("Forbidden phone number, has existing address in legacy profile",
PhoneNumberType.FORBIDDEN, IpAddressType.ALLOWED, ExistingPaymentAddressType.LEGACY_PROFILE, false),
Arguments.argumentSet("No phone number, permitted ASN region",
PhoneNumberType.NONE, IpAddressType.ALLOWED, ExistingPaymentAddressType.NONE, false),
Arguments.argumentSet("No phone number, unrecognized ASN region",
PhoneNumberType.NONE, IpAddressType.UNRECOGNIZED, ExistingPaymentAddressType.NONE, false),
Arguments.argumentSet("No phone number, forbidden ASN region",
PhoneNumberType.NONE, IpAddressType.FORBIDDEN, ExistingPaymentAddressType.NONE, true),
Arguments.argumentSet("No phone number, forbidden ASN region, has existing address",
PhoneNumberType.NONE, IpAddressType.FORBIDDEN, ExistingPaymentAddressType.CURRENT_PROFILE, false),
Arguments.argumentSet("No phone number, forbidden ASN region, has existing address in legacy profile",
PhoneNumberType.NONE, IpAddressType.FORBIDDEN, ExistingPaymentAddressType.LEGACY_PROFILE, false)
);
}
}