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);