Strictly enforce ACI service identifier strings have no prefix

This commit is contained in:
Ravi Khadiwala 2024-01-19 16:12:18 -06:00 committed by ravi-signal
parent 408b065b9e
commit 3820a231ec
4 changed files with 10 additions and 9 deletions

View File

@ -25,8 +25,6 @@ import org.whispersystems.textsecuregcm.util.UUIDUtil;
description = "An identifier for an account based on the account's ACI"
)
public record AciServiceIdentifier(UUID uuid) implements ServiceIdentifier {
private static final String SERVICE_ID_STRING_COUNTER_NAME = MetricsUtil.name(AciServiceIdentifier.class, "serviceIdString");
private static final IdentityType IDENTITY_TYPE = IdentityType.ACI;
@Override
@ -61,10 +59,7 @@ public record AciServiceIdentifier(UUID uuid) implements ServiceIdentifier {
}
public static AciServiceIdentifier valueOf(final String string) {
final boolean valid = !string.startsWith(IDENTITY_TYPE.getStringPrefix());
final UUID uuid = UUID.fromString(valid ? string : string.substring(IDENTITY_TYPE.getStringPrefix().length()));
Metrics.counter(SERVICE_ID_STRING_COUNTER_NAME, "valid", String.valueOf(valid)).increment();
return new AciServiceIdentifier(uuid);
return new AciServiceIdentifier(UUID.fromString(string));
}
public static AciServiceIdentifier fromBytes(final byte[] bytes) {

View File

@ -56,6 +56,12 @@ public sealed interface ServiceIdentifier permits AciServiceIdentifier, PniServi
*/
byte[] toFixedWidthByteArray();
/**
* Parse a service identifier string, which should be a plain UUID string for ACIs and a prefixed UUID string for PNIs
*
* @param string A service identifier string
* @return The parsed {@link ServiceIdentifier}
*/
static ServiceIdentifier valueOf(final String string) {
try {
return AciServiceIdentifier.valueOf(string);

View File

@ -53,11 +53,12 @@ class AciServiceIdentifierTest {
final UUID uuid = UUID.randomUUID();
assertEquals(uuid, AciServiceIdentifier.valueOf(uuid.toString()).uuid());
assertEquals(uuid, AciServiceIdentifier.valueOf("ACI:" + uuid).uuid());
assertThrows(IllegalArgumentException.class, () -> AciServiceIdentifier.valueOf("Not a valid UUID"));
assertThrows(IllegalArgumentException.class, () -> AciServiceIdentifier.valueOf("PNI:" + uuid));
assertThrows(IllegalArgumentException.class, () -> AciServiceIdentifier.valueOf("ACI:" + uuid).uuid());
}
@Test
void fromBytes() {
final UUID uuid = UUID.randomUUID();

View File

@ -32,12 +32,11 @@ class ServiceIdentifierTest {
return Stream.of(
Arguments.of(uuid.toString(), IdentityType.ACI, uuid),
Arguments.of("ACI:" + uuid, IdentityType.ACI, uuid),
Arguments.of("PNI:" + uuid, IdentityType.PNI, uuid));
}
@ParameterizedTest
@ValueSource(strings = {"Not a valid UUID", "BAD:a9edc243-3e93-45d4-95c6-e3a84cd4a254"})
@ValueSource(strings = {"Not a valid UUID", "BAD:a9edc243-3e93-45d4-95c6-e3a84cd4a254", "ACI:a9edc243-3e93-45d4-95c6-e3a84cd4a254"})
void valueOfIllegalArgument(final String identifierString) {
assertThrows(IllegalArgumentException.class, () -> ServiceIdentifier.valueOf(identifierString));
}