mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 01:06:50 +00:00
Introduce ReportMessageHelper
This commit is contained in:
1 parent
ce8cc80f66
commit
f66bd0858d
5 files changed
+92
-46
No files matched your search
+10
-24
@@ -91,9 +91,9 @@ import org.whispersystems.textsecuregcm.spam.SpamChecker;
|
||||
import org.whispersystems.textsecuregcm.storage.Account;
|
||||
import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||
import org.whispersystems.textsecuregcm.storage.PhoneNumberIdentifiers;
|
||||
import org.whispersystems.textsecuregcm.storage.ReportMessageHelper;
|
||||
import org.whispersystems.textsecuregcm.storage.ReportMessageManager;
|
||||
import org.whispersystems.textsecuregcm.util.HeaderUtils;
|
||||
import org.whispersystems.textsecuregcm.util.Util;
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
@Path("/v1/messages")
|
||||
@@ -605,7 +605,7 @@ public class MessageController {
|
||||
|
||||
CompletableFuture.allOf(resolvedRecipients.values()
|
||||
.stream()
|
||||
.map(account -> account.getAccountIdentifier())
|
||||
.map(Account::getAccountIdentifier)
|
||||
.map(accountIdentifier ->
|
||||
rateLimiters.getStoriesLimiter().validateAsync(accountIdentifier).toCompletableFuture())
|
||||
.toList()
|
||||
@@ -735,27 +735,6 @@ public class MessageController {
|
||||
@PathParam("messageGuid") UUID messageGuid,
|
||||
@Nullable SpamReport spamReport,
|
||||
@HeaderParam(HttpHeaders.USER_AGENT) String userAgent) {
|
||||
final Optional<UUID> sourceAci = Optional.of(source);
|
||||
|
||||
final Optional<String> sourceNumber;
|
||||
final Optional<UUID> sourcePni;
|
||||
final boolean sourceAccountDeleted;
|
||||
|
||||
final Optional<Account> sourceAccount = accountsManager.getByAccountIdentifier(sourceAci.get());
|
||||
|
||||
if (sourceAccount.isEmpty()) {
|
||||
logger.warn("Could not find source: {}", sourceAci.get());
|
||||
sourcePni = accountsManager.findRecentlyDeletedPhoneNumberIdentifier(sourceAci.get());
|
||||
sourceNumber = sourcePni.flatMap(pni ->
|
||||
Util.getCanonicalNumber(phoneNumberIdentifiers.getPhoneNumber(pni).join()));
|
||||
sourceAccountDeleted = true;
|
||||
} else {
|
||||
sourceNumber = sourceAccount.flatMap(Account::getNumber);
|
||||
sourcePni = sourceAccount.flatMap(Account::getPhoneNumberIdentifier);
|
||||
sourceAccountDeleted = false;
|
||||
}
|
||||
|
||||
final UUID spamReporterUuid = auth.accountIdentifier();
|
||||
|
||||
// spam report token is optional, but if provided ensure it is non-empty.
|
||||
final Optional<byte[]> maybeSpamReportToken =
|
||||
@@ -763,7 +742,14 @@ public class MessageController {
|
||||
.flatMap(r -> Optional.ofNullable(r.token()))
|
||||
.filter(t -> t.length > 0);
|
||||
|
||||
reportMessageManager.report(sourceNumber, sourceAci, sourcePni, messageGuid, spamReporterUuid, maybeSpamReportToken, userAgent, sourceAccountDeleted);
|
||||
ReportMessageHelper.reportMessage(new AciServiceIdentifier(source),
|
||||
new AciServiceIdentifier(auth.accountIdentifier()),
|
||||
messageGuid,
|
||||
maybeSpamReportToken.orElse(null),
|
||||
userAgent,
|
||||
accountsManager,
|
||||
phoneNumberIdentifiers,
|
||||
reportMessageManager);
|
||||
|
||||
return Response.status(Status.ACCEPTED)
|
||||
.build();
|
||||
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import javax.annotation.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier;
|
||||
import org.whispersystems.textsecuregcm.identity.ServiceIdentifier;
|
||||
import org.whispersystems.textsecuregcm.util.Util;
|
||||
|
||||
public class ReportMessageHelper {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ReportMessageHelper.class);
|
||||
|
||||
public static void reportMessage(final ServiceIdentifier sourceServiceIdentifier,
|
||||
final AciServiceIdentifier reporterServiceIdentifier,
|
||||
final UUID messageGuid,
|
||||
@Nullable final byte[] reportSpamToken,
|
||||
@Nullable final String userAgent,
|
||||
final AccountsManager accountsManager,
|
||||
final PhoneNumberIdentifiers phoneNumberIdentifiers,
|
||||
final ReportMessageManager reportMessageManager) {
|
||||
|
||||
final Optional<String> maybeSourceNumber;
|
||||
final Optional<UUID> maybeSourcePni;
|
||||
final boolean sourceAccountDeleted;
|
||||
|
||||
final Optional<Account> sourceAccount = accountsManager.getByServiceIdentifier(sourceServiceIdentifier);
|
||||
|
||||
if (sourceAccount.isEmpty()) {
|
||||
logger.warn("Could not find source: {}", sourceServiceIdentifier);
|
||||
maybeSourcePni = accountsManager.findRecentlyDeletedPhoneNumberIdentifier(sourceServiceIdentifier.uuid());
|
||||
maybeSourceNumber = maybeSourcePni.flatMap(pni ->
|
||||
Util.getCanonicalNumber(phoneNumberIdentifiers.getPhoneNumber(pni).join()));
|
||||
sourceAccountDeleted = true;
|
||||
} else {
|
||||
maybeSourceNumber = sourceAccount.flatMap(Account::getNumber);
|
||||
maybeSourcePni = sourceAccount.flatMap(Account::getPhoneNumberIdentifier);
|
||||
sourceAccountDeleted = false;
|
||||
}
|
||||
|
||||
reportMessageManager.report(maybeSourceNumber,
|
||||
sourceServiceIdentifier.uuid(),
|
||||
maybeSourcePni,
|
||||
messageGuid,
|
||||
reporterServiceIdentifier.uuid(),
|
||||
Optional.ofNullable(reportSpamToken),
|
||||
userAgent,
|
||||
sourceAccountDeleted);
|
||||
}
|
||||
}
|
||||
+8
-7
@@ -23,6 +23,7 @@ import org.whispersystems.textsecuregcm.metrics.MetricsUtil;
|
||||
import org.whispersystems.textsecuregcm.metrics.UserAgentTagUtil;
|
||||
import org.whispersystems.textsecuregcm.redis.FaultTolerantRedisClusterClient;
|
||||
import org.whispersystems.textsecuregcm.util.UUIDUtil;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class ReportMessageManager {
|
||||
|
||||
@@ -61,17 +62,17 @@ public class ReportMessageManager {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
public void report(final Optional<String> sourceNumber,
|
||||
final Optional<UUID> sourceAci,
|
||||
final UUID sourceAci,
|
||||
final Optional<UUID> sourcePni,
|
||||
final UUID messageGuid,
|
||||
final UUID reporterUuid,
|
||||
final Optional<byte[]> reportSpamToken,
|
||||
final String reporterUserAgent,
|
||||
@Nullable final String reporterUserAgent,
|
||||
final boolean sourceAccountDeleted) {
|
||||
|
||||
final boolean found = sourceAci.map(uuid -> reportMessageDynamoDb.remove(hash(messageGuid, uuid.toString())))
|
||||
.orElse(false);
|
||||
final boolean found = reportMessageDynamoDb.remove(hash(messageGuid, sourceAci.toString()));
|
||||
|
||||
Metrics.counter(REPORT_MESSAGE_COUNTER_NAME,
|
||||
Tags.of(FOUND_MESSAGE_TAG, String.valueOf(found),
|
||||
@@ -87,11 +88,11 @@ public class ReportMessageManager {
|
||||
connection.sync().expire(reportedSenderKey, counterTtl.toSeconds());
|
||||
});
|
||||
|
||||
sourceAci.ifPresent(aci -> {
|
||||
final String reportedSenderKey = getReportedSenderAciKey(aci);
|
||||
{
|
||||
final String reportedSenderKey = getReportedSenderAciKey(sourceAci);
|
||||
connection.sync().pfadd(reportedSenderKey, reporterUuid.toString());
|
||||
connection.sync().expire(reportedSenderKey, counterTtl.toSeconds());
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
reportedMessageListeners.forEach(listener -> {
|
||||
|
||||
+11
-9
@@ -725,7 +725,7 @@ class MessageControllerTest {
|
||||
when(account.getNumber()).thenReturn(Optional.of(senderNumber));
|
||||
when(account.getPhoneNumberIdentifier()).thenReturn(Optional.of(senderPni));
|
||||
|
||||
when(accountsManager.getByAccountIdentifier(senderAci)).thenReturn(Optional.of(account));
|
||||
when(accountsManager.getByServiceIdentifier(new AciServiceIdentifier(senderAci))).thenReturn(Optional.of(account));
|
||||
when(phoneNumberIdentifiers.getPhoneNumber(senderPni)).thenReturn(CompletableFuture.completedFuture(List.of(senderNumber)));
|
||||
|
||||
try (final Response response =
|
||||
@@ -738,7 +738,7 @@ class MessageControllerTest {
|
||||
|
||||
assertThat(response.getStatus(), is(equalTo(202)));
|
||||
|
||||
verify(reportMessageManager).report(Optional.of(senderNumber), Optional.of(senderAci), Optional.of(senderPni),
|
||||
verify(reportMessageManager).report(Optional.of(senderNumber), senderAci, Optional.of(senderPni),
|
||||
messageGuid, AuthHelper.VALID_UUID, Optional.empty(), userAgent, false);
|
||||
verify(accountsManager, never()).findRecentlyDeletedPhoneNumberIdentifier(any(UUID.class));
|
||||
verify(phoneNumberIdentifiers, never()).getPhoneNumber(any());
|
||||
@@ -772,7 +772,7 @@ class MessageControllerTest {
|
||||
|
||||
assertThat(response.getStatus(), is(equalTo(202)));
|
||||
|
||||
verify(reportMessageManager).report(Optional.of(senderNumber), Optional.of(senderAci), Optional.of(senderPni),
|
||||
verify(reportMessageManager).report(Optional.of(senderNumber), senderAci, Optional.of(senderPni),
|
||||
messageGuid, AuthHelper.VALID_UUID, Optional.empty(), userAgent, true);
|
||||
}
|
||||
}
|
||||
@@ -790,7 +790,7 @@ class MessageControllerTest {
|
||||
when(account.getNumber()).thenReturn(Optional.of(senderNumber));
|
||||
when(account.getPhoneNumberIdentifier()).thenReturn(Optional.of(senderPni));
|
||||
|
||||
when(accountsManager.getByAccountIdentifier(senderAci)).thenReturn(Optional.of(account));
|
||||
when(accountsManager.getByServiceIdentifier(new AciServiceIdentifier(senderAci))).thenReturn(Optional.of(account));
|
||||
when(accountsManager.findRecentlyDeletedPhoneNumberIdentifier(senderAci)).thenReturn(Optional.of(senderPni));
|
||||
when(phoneNumberIdentifiers.getPhoneNumber(senderPni)).thenReturn(CompletableFuture.completedFuture(List.of(senderNumber)));
|
||||
|
||||
@@ -805,7 +805,7 @@ class MessageControllerTest {
|
||||
|
||||
assertThat(response.getStatus(), is(equalTo(202)));
|
||||
verify(reportMessageManager).report(eq(Optional.of(senderNumber)),
|
||||
eq(Optional.of(senderAci)),
|
||||
eq(senderAci),
|
||||
eq(Optional.of(senderPni)),
|
||||
eq(messageGuid),
|
||||
eq(AuthHelper.VALID_UUID),
|
||||
@@ -816,11 +816,13 @@ class MessageControllerTest {
|
||||
verify(phoneNumberIdentifiers, never()).getPhoneNumber(any());
|
||||
}
|
||||
|
||||
when(accountsManager.getByAccountIdentifier(senderAci)).thenReturn(Optional.empty());
|
||||
when(accountsManager.getByServiceIdentifier(new AciServiceIdentifier(senderAci))).thenReturn(Optional.empty());
|
||||
|
||||
messageGuid = UUID.randomUUID();
|
||||
|
||||
entity = Entity.entity(new SpamReport(new byte[5]), "application/json");
|
||||
final byte[] reportSpamToken = new byte[5];
|
||||
|
||||
entity = Entity.entity(new SpamReport(reportSpamToken), "application/json");
|
||||
|
||||
try (final Response response =
|
||||
resources.getJerseyTest()
|
||||
@@ -831,11 +833,11 @@ class MessageControllerTest {
|
||||
|
||||
assertThat(response.getStatus(), is(equalTo(202)));
|
||||
verify(reportMessageManager).report(eq(Optional.of(senderNumber)),
|
||||
eq(Optional.of(senderAci)),
|
||||
eq(senderAci),
|
||||
eq(Optional.of(senderPni)),
|
||||
eq(messageGuid),
|
||||
eq(AuthHelper.VALID_UUID),
|
||||
argThat(maybeBytes -> maybeBytes.map(bytes -> Arrays.equals(bytes, new byte[5])).orElse(false)),
|
||||
argThat(maybeBytes -> maybeBytes.map(bytes -> Arrays.equals(bytes, reportSpamToken)).orElse(false)),
|
||||
any(),
|
||||
eq(true));
|
||||
}
|
||||
|
||||
+6
-6
@@ -79,13 +79,13 @@ class ReportMessageManagerTest {
|
||||
reportMessageManager.addListener(listener);
|
||||
|
||||
when(reportMessageDynamoDb.remove(any())).thenReturn(false);
|
||||
reportMessageManager.report(Optional.of(sourceNumber), Optional.of(sourceAci), Optional.of(sourcePni), messageGuid,
|
||||
reportMessageManager.report(Optional.of(sourceNumber), sourceAci, Optional.of(sourcePni), messageGuid,
|
||||
reporterUuid, Optional.empty(), "user-agent", false);
|
||||
|
||||
assertEquals(0, reportMessageManager.getRecentReportCount(sourceAccount));
|
||||
|
||||
when(reportMessageDynamoDb.remove(any())).thenReturn(true);
|
||||
reportMessageManager.report(Optional.of(sourceNumber), Optional.of(sourceAci), Optional.of(sourcePni), messageGuid,
|
||||
reportMessageManager.report(Optional.of(sourceNumber), sourceAci, Optional.of(sourcePni), messageGuid,
|
||||
reporterUuid, Optional.empty(), "user-agent", false);
|
||||
|
||||
assertEquals(1, reportMessageManager.getRecentReportCount(sourceAccount));
|
||||
@@ -98,7 +98,7 @@ class ReportMessageManagerTest {
|
||||
assertEquals(0, reportMessageManager.getRecentReportCount(sourceAccount));
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
reportMessageManager.report(Optional.of(sourceNumber), Optional.of(sourceAci), Optional.of(sourcePni),
|
||||
reportMessageManager.report(Optional.of(sourceNumber), sourceAci, Optional.of(sourcePni),
|
||||
messageGuid, UUID.randomUUID(), Optional.empty(), "user-agent", false);
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ class ReportMessageManagerTest {
|
||||
assertEquals(0, reportMessageManager.getRecentReportCount(sourceAccount));
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
reportMessageManager.report(Optional.of(sourceNumber), Optional.of(sourceAci), Optional.of(sourcePni),
|
||||
reportMessageManager.report(Optional.of(sourceNumber), sourceAci, Optional.of(sourcePni),
|
||||
messageGuid,
|
||||
reporterUuid, Optional.empty(), "user-agent", false);
|
||||
}
|
||||
@@ -125,11 +125,11 @@ class ReportMessageManagerTest {
|
||||
assertEquals(0, reportMessageManager.getRecentReportCount(sourceAccount));
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
reportMessageManager.report(Optional.empty(), Optional.of(sourceAci), Optional.of(sourcePni),
|
||||
reportMessageManager.report(Optional.empty(), sourceAci, Optional.of(sourcePni),
|
||||
messageGuid, UUID.randomUUID(), Optional.empty(), "user-agent", false);
|
||||
}
|
||||
|
||||
reportMessageManager.report(Optional.empty(), Optional.of(sourceAci), Optional.empty(),
|
||||
reportMessageManager.report(Optional.empty(), sourceAci, Optional.empty(),
|
||||
messageGuid, UUID.randomUUID(), Optional.empty(), "user-agent", false);
|
||||
|
||||
final int recentReportCount = reportMessageManager.getRecentReportCount(sourceAccount);
|
||||
|
||||
Reference in new issue
Block a user