diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 16b19c7e1..fe262b700 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -181,6 +181,7 @@ import org.whispersystems.textsecuregcm.grpc.PaymentsGrpcService; import org.whispersystems.textsecuregcm.grpc.ProductConfigurationGrpcService; import org.whispersystems.textsecuregcm.grpc.ProfileAnonymousGrpcService; import org.whispersystems.textsecuregcm.grpc.ProfileGrpcService; +import org.whispersystems.textsecuregcm.grpc.RemoteConfigurationGrpcService; import org.whispersystems.textsecuregcm.grpc.RequestAttributesInterceptor; import org.whispersystems.textsecuregcm.grpc.SubscriptionsGrpcService; import org.whispersystems.textsecuregcm.grpc.ValidatingInterceptor; @@ -1103,7 +1104,8 @@ public class WhisperServerService extends Application ServerInterceptors.intercept(bindableService, // Note: interceptors run in the reverse order they are added; the remote deprecation filter // depends on the user-agent context so it has to come first here! diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/RemoteConfigurationGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/RemoteConfigurationGrpcService.java new file mode 100644 index 000000000..1aded5662 --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/RemoteConfigurationGrpcService.java @@ -0,0 +1,71 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ +package org.whispersystems.textsecuregcm.grpc; + +import com.google.protobuf.ByteString; +import com.google.protobuf.CodedOutputStream; +import com.google.protobuf.Message; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; +import java.util.UUID; +import org.signal.chat.remoteconfiguration.Configuration; +import org.signal.chat.remoteconfiguration.GetConfigurationRequest; +import org.signal.chat.remoteconfiguration.GetConfigurationResponse; +import org.signal.chat.remoteconfiguration.SimpleRemoteConfigurationGrpc; +import org.signal.chat.remoteconfiguration.TaggedConfiguration; +import org.whispersystems.textsecuregcm.auth.grpc.AuthenticationUtil; +import org.whispersystems.textsecuregcm.storage.RemoteConfigsManager; + +public class RemoteConfigurationGrpcService extends SimpleRemoteConfigurationGrpc.RemoteConfigurationImplBase { + + private static final GetConfigurationResponse CONFIGURATION_ETAG_MATCHED = + GetConfigurationResponse.newBuilder().setEtagMatched(true).build(); + + private final RemoteConfigsManager remoteConfigsManager; + + public RemoteConfigurationGrpcService(final RemoteConfigsManager remoteConfigsManager) { + this.remoteConfigsManager = remoteConfigsManager; + } + + @Override + public GetConfigurationResponse getConfiguration(final GetConfigurationRequest request) { + final UUID accountIdentifier = AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier(); + final String userAgent = RequestAttributesUtil.getUserAgent().orElse(null); + final Map configForAccount = remoteConfigsManager.getConfigForAccount(accountIdentifier, userAgent); + + final Configuration configuration = Configuration.newBuilder() + .putAllConfiguration(configForAccount) + .build(); + + final ByteString etagByteString = etag(configuration); + if (etagByteString.equals(request.getEtag())) { + return CONFIGURATION_ETAG_MATCHED; + } + return GetConfigurationResponse.newBuilder() + .setTaggedConfiguration(TaggedConfiguration.newBuilder() + .setEtag(etagByteString) + .setConfiguration(configuration)) + .build(); + } + + private static ByteString etag(final Message message) { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(message.getSerializedSize()); + final CodedOutputStream cos = CodedOutputStream.newInstance(baos); + cos.useDeterministicSerialization(); + try { + message.writeTo(cos); + cos.flush(); + return ByteString.copyFrom(MessageDigest.getInstance("SHA-256").digest(baos.toByteArray())); + } catch (final IOException e) { + throw new UncheckedIOException(e); + } catch (final NoSuchAlgorithmException e) { + throw new AssertionError(e); + } + } +} diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/RemoteConfigsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/RemoteConfigsManager.java index fc0fa677d..ef51d84ff 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/RemoteConfigsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/RemoteConfigsManager.java @@ -45,7 +45,7 @@ public class RemoteConfigsManager { } /** - * Returns all remote configuration (client and global) for the specified account and client. + * Returns all remote configuration (per-platform and global) for the specified account and client. * * @param accountIdentifier the identifier of the authenticated account * @param userAgent the requester's user agent, used to select the client platform @@ -61,10 +61,10 @@ public class RemoteConfigsManager { throw new AssertionError(e); } - final List clientConfig = remoteConfigSupplier.get(); + final List perPlatformConfig = remoteConfigSupplier.get(); return Stream.concat( - clientConfig.stream() + perPlatformConfig.stream() .filter(config -> { final String firstNameComponent = config.getName().split("\\.", 2)[0]; return firstNameComponent.equals(platformPrefix) || !PLATFORM_PREFIXES.contains(firstNameComponent); diff --git a/service/src/main/proto/org/signal/chat/remote_configuration.proto b/service/src/main/proto/org/signal/chat/remote_configuration.proto new file mode 100644 index 000000000..efaf565a0 --- /dev/null +++ b/service/src/main/proto/org/signal/chat/remote_configuration.proto @@ -0,0 +1,56 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +syntax = "proto3"; + +option java_multiple_files = true; + +package org.signal.chat.remoteconfiguration; + +import "org/signal/chat/require.proto"; +import "org/signal/chat/tag.proto"; + +// Provides remote configuration applicable to the authenticated user +service RemoteConfiguration { + option (require.auth) = AUTH_ONLY_AUTHENTICATED; + + // Fetches the configuration for the authenticated user. Returned + // values are based on the authenticated account and caller's client platform, + // which is derived from the "User-Agent" header. + rpc GetConfiguration(GetConfigurationRequest) returns (GetConfigurationResponse) {} +} + +message GetConfigurationRequest { + // If present, the etag from a prior GetConfigurationResponse. If the + // provided etag matches the current configuration etag, the server may elide + // the configuration response. + bytes etag = 1; +} + +message GetConfigurationResponse { + oneof response { + // The full configuration and corresponding etag. + TaggedConfiguration tagged_configuration = 1; + + // The etag in the request matched the current configuration etag. + bool etag_matched = 2 [(tag.reason) = "etag_match"]; + } +} + +message Configuration { + // A map of namespaced configuration keys to their resolved values. All + // configuration values are represented as strings. boolean values are + // represented as the strings "true" or "false". + map configuration = 1; +} + +message TaggedConfiguration { + Configuration configuration = 1; + + // An entity tag for `configuration`. This may be supplied in a + // subsequent request to optimize bandwidth when the configuration has not + // changed. + bytes etag = 2; +} diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/RemoteConfigurationGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/RemoteConfigurationGrpcServiceTest.java new file mode 100644 index 000000000..43956247a --- /dev/null +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/RemoteConfigurationGrpcServiceTest.java @@ -0,0 +1,152 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.grpc; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +import com.google.common.net.InetAddresses; +import com.google.protobuf.ByteString; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.UUID; +import java.util.stream.Collectors; +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.mockito.Mock; +import org.signal.chat.remoteconfiguration.GetConfigurationRequest; +import org.signal.chat.remoteconfiguration.GetConfigurationResponse; +import org.signal.chat.remoteconfiguration.RemoteConfigurationGrpc; +import org.whispersystems.textsecuregcm.storage.RemoteConfigsManager; + +class RemoteConfigurationGrpcServiceTest extends + SimpleBaseGrpcTest { + + @Mock + private RemoteConfigsManager remoteConfigsManager; + + @Override + protected RemoteConfigurationGrpcService createServiceBeforeEachTest() { + getMockRequestAttributesInterceptor().setRequestAttributes( + new RequestAttributes(InetAddresses.forString("127.0.0.1"), null, null)); + + return new RemoteConfigurationGrpcService(remoteConfigsManager); + } + + @Test + void getConfigurationUnchanged() { + final String userAgent = "Signal-Android/7.6.2 Android/34 libsignal/0.46.0"; + getMockRequestAttributesInterceptor().setRequestAttributes( + new RequestAttributes(InetAddresses.forString("127.0.0.1"), userAgent, null)); + when(remoteConfigsManager.getConfigForAccount(AUTHENTICATED_ACI, userAgent)) + .thenReturn(Map.of("test.test", "bar", "global.test", "false")); + + final GetConfigurationResponse response = authenticatedServiceStub() + .getConfiguration(GetConfigurationRequest.newBuilder().build()); + + assertTrue(response.hasTaggedConfiguration()); + final ByteString etag = response.getTaggedConfiguration().getEtag(); + + final GetConfigurationResponse cachedResponse = authenticatedServiceStub() + .getConfiguration(GetConfigurationRequest.newBuilder().setEtag(etag).build()); + + assertTrue(cachedResponse.getEtagMatched()); + assertFalse(cachedResponse.hasTaggedConfiguration()); + } + + @Test + void getConfigurationChanged() { + final String userAgent = "Signal-Android/7.6.2 Android/34 libsignal/0.46.0"; + getMockRequestAttributesInterceptor().setRequestAttributes( + new RequestAttributes(InetAddresses.forString("127.0.0.1"), userAgent, null)); + + final Map config1 = Map.of( + "android.stickers", "foo", + "test.test", "bar", + "global.test", "false"); + + when(remoteConfigsManager.getConfigForAccount(AUTHENTICATED_ACI, userAgent)).thenReturn(config1); + + final GetConfigurationResponse response = authenticatedServiceStub() + .getConfiguration(GetConfigurationRequest.newBuilder().build()); + + assertTrue(response.hasTaggedConfiguration()); + final ByteString etag = response.getTaggedConfiguration().getEtag(); + + final Map config2 = new HashMap<>(config1); + config2.put("android.new.config", "true"); + when(remoteConfigsManager.getConfigForAccount(AUTHENTICATED_ACI, userAgent)).thenReturn(config2); + + final GetConfigurationResponse afterChange = authenticatedServiceStub() + .getConfiguration(GetConfigurationRequest.newBuilder().setEtag(etag).build()); + + assertFalse(afterChange.getEtagMatched()); + assertTrue(afterChange.hasTaggedConfiguration()); + } + + @ParameterizedTest + @MethodSource + void getConfigurationEtag(final boolean expectMatched, final Map config1, + final Map config2) { + + final UUID user1 = UUID.randomUUID(); + final String ua1 = "user-agent-1"; + final UUID user2 = UUID.randomUUID(); + final String ua2 = "user-agent-2"; + + when(remoteConfigsManager.getConfigForAccount(user1, ua1)).thenReturn(config1); + when(remoteConfigsManager.getConfigForAccount(user2, ua2)).thenReturn(config2); + + getMockAuthenticationInterceptor().setAuthenticatedDevice(user1, AUTHENTICATED_DEVICE_ID); + getMockRequestAttributesInterceptor().setRequestAttributes( + new RequestAttributes(InetAddresses.forString("127.0.0.1"), ua1, null)); + final GetConfigurationResponse response = authenticatedServiceStub() + .getConfiguration(GetConfigurationRequest.newBuilder().build()); + + assertTrue(response.hasTaggedConfiguration()); + final ByteString etag = response.getTaggedConfiguration().getEtag(); + + getMockAuthenticationInterceptor().setAuthenticatedDevice(user2, AUTHENTICATED_DEVICE_ID); + getMockRequestAttributesInterceptor().setRequestAttributes( + new RequestAttributes(InetAddresses.forString("127.0.0.1"), ua2, null)); + final GetConfigurationResponse secondResponse = authenticatedServiceStub() + .getConfiguration(GetConfigurationRequest.newBuilder().setEtag(etag).build()); + + if (expectMatched) { + assertTrue(secondResponse.getEtagMatched()); + assertFalse(secondResponse.hasTaggedConfiguration()); + } else { + assertFalse(secondResponse.getEtagMatched()); + assertTrue(secondResponse.hasTaggedConfiguration()); + } + } + + static List getConfigurationEtag() { + final Map config1 = Map.of( + "android.stickers", "foo", + "test.test", "bar", + "global.test", "false"); + + final Map config2 = Map.of( + "android.stickers", "foo", + "test.test", "bar", + "global.test", "true"); + + return List.of( + Arguments.argumentSet("same", true, config1, config1), + Arguments.argumentSet("change", false, config1, config2), + Arguments.argumentSet("different sort", true, + new TreeMap<>(config1), + config1.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, _) -> a, () -> new TreeMap(Comparator.reverseOrder())))) + ); + } +}