From 0281756c018b43807664cd0b86d04f61211d7460 Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Mon, 13 Apr 2026 12:25:02 -0500 Subject: [PATCH] Add metrics for invalid unrestricted UAK --- .../entities/AccountAttributes.java | 24 ++++++++++++++++--- .../controllers/AccountControllerTest.java | 4 ++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/entities/AccountAttributes.java b/service/src/main/java/org/whispersystems/textsecuregcm/entities/AccountAttributes.java index 787ac9a37..f1550a2b6 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/entities/AccountAttributes.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/entities/AccountAttributes.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.google.common.annotations.VisibleForTesting; +import io.micrometer.core.instrument.Metrics; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.Valid; import jakarta.validation.constraints.AssertTrue; @@ -15,12 +16,15 @@ import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; import org.whispersystems.textsecuregcm.auth.UnidentifiedAccessUtil; +import org.whispersystems.textsecuregcm.metrics.MetricsUtil; import org.whispersystems.textsecuregcm.storage.DeviceCapability; import org.whispersystems.textsecuregcm.util.ByteArrayAdapter; import org.whispersystems.textsecuregcm.util.ExactlySize; public class AccountAttributes { + private static final String UAK_VALIDATION_COUNTER_NAME = MetricsUtil.name(AccountAttributes.class, "uakValidation"); + @JsonUnwrapped @Valid private DeviceAttributes deviceAttributes; @@ -128,10 +132,24 @@ public class AccountAttributes { return this; } + @VisibleForTesting + public static final boolean ENFORCE_VALID_UNRESTRICTED_UAK = false; + @AssertTrue @Schema(hidden = true) - public boolean isUakValid() { - return (unrestrictedUnidentifiedAccess && (unidentifiedAccessKey == null || unidentifiedAccessKey.length == 0)) - || (!unrestrictedUnidentifiedAccess && (unidentifiedAccessKey != null && unidentifiedAccessKey.length == 16)); + public boolean isUnrestrictedUakValid() { + + final boolean valid = + (unrestrictedUnidentifiedAccess && (unidentifiedAccessKey == null || unidentifiedAccessKey.length == 0)) + || (!unrestrictedUnidentifiedAccess && (unidentifiedAccessKey != null + && unidentifiedAccessKey.length == 16)); + + Metrics.counter(UAK_VALIDATION_COUNTER_NAME, + "valid", String.valueOf(valid), + "unrestricted", String.valueOf(unrestrictedUnidentifiedAccess) + ).increment(); + + // initially, only gather metrics + return true; } } 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 d04a087a9..f239a3c99 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AccountControllerTest.java @@ -48,6 +48,7 @@ import java.util.stream.Stream; import org.glassfish.jersey.server.ServerProperties; import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -781,6 +782,9 @@ class AccountControllerTest { @MethodSource void testSetAccountAttributesUnrestrictedUnidentifiedAccess(final boolean unrestrictedUnidentifiedAccess, final byte[] unidentifiedAccessKey, final int expectedStatus) { + // This test is only valid when validations are enforced + Assumptions.assumeTrue(AccountAttributes.ENFORCE_VALID_UNRESTRICTED_UAK); + try (final Response response = resources.getJerseyTest() .target("/v1/accounts/attributes/") .request()