Explicitly call spam-filter for messages

Pass in the same information to the spam-filter, but just use explicit
method calls rather than jersey request filters.
This commit is contained in:
Ravi Khadiwala
2024-02-05 11:59:04 -06:00
committed by ravi-signal
parent 0965ab8063
commit 3b44ed6d16
7 changed files with 155 additions and 104 deletions

View File

@@ -12,13 +12,4 @@ import java.io.IOException;
public interface RateLimitChallengeListener {
void handleRateLimitChallengeAnswered(Account account, ChallengeType type);
/**
* Configures this rate limit challenge listener. This method will be called before the service begins processing any
* challenges.
*
* @param environmentName the name of the environment in which this listener is running (e.g. "staging" or "production")
* @throws IOException if the listener could not read its configuration source for any reason
*/
void configure(String environmentName) throws IOException;
}

View File

@@ -1,5 +1,6 @@
package org.whispersystems.textsecuregcm.spam;
import org.whispersystems.textsecuregcm.storage.Account;
import javax.ws.rs.container.ContainerRequestContext;
import java.util.Optional;
import java.util.function.Function;
@@ -12,10 +13,13 @@ public interface ReportSpamTokenProvider {
/**
* Generate a new ReportSpamToken
*
* @param context the message request context
* @param context the message request context
* @param sender the account that sent the unsealed sender message
* @param maybeDestination the intended recepient of the message if available
* @return either a generated token or nothing
*/
Optional<byte[]> makeReportSpamToken(ContainerRequestContext context);
Optional<byte[]> makeReportSpamToken(ContainerRequestContext context, final Account sender,
final Optional<Account> maybeDestination);
/**
* Provider which generates nothing
@@ -23,6 +27,6 @@ public interface ReportSpamTokenProvider {
* @return the provider
*/
static ReportSpamTokenProvider noop() {
return context -> Optional.empty();
return (ignoredContext, ignoredSender, ignoredDest) -> Optional.empty();
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.spam;
import org.whispersystems.textsecuregcm.storage.Account;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Response;
import java.util.Optional;
public interface SpamChecker {
/**
* Determine if a message may be spam
*
* @param requestContext The request context for a message send attempt
* @param maybeSource The sender of the message, could be empty if this as message sent with sealed sender
* @param maybeDestination The destination of the message, could be empty if the destination does not exist or could
* not be retrieved
* @return A response to return if the request is determined to be spam, otherwise empty if the message should be sent
*/
Optional<Response> checkForSpam(
final ContainerRequestContext requestContext,
final Optional<Account> maybeSource,
final Optional<Account> maybeDestination);
static SpamChecker noop() {
return (ignoredContext, ignoredSource, ignoredDestination) -> Optional.empty();
}
}

View File

@@ -9,26 +9,26 @@ import io.dropwizard.lifecycle.Managed;
import org.whispersystems.textsecuregcm.storage.ReportedMessageListener;
import javax.ws.rs.container.ContainerRequestFilter;
import java.io.IOException;
import java.util.List;
/**
* A spam filter is a {@link ContainerRequestFilter} that filters requests to message-sending endpoints to
* detect and respond to patterns of spam.
* A spam filter is a {@link ContainerRequestFilter} that filters requests to endpoints to detect and respond to
* patterns of spam and fraud.
* <p/>
* Spam filters are managed components that are generally loaded dynamically via a
* {@link java.util.ServiceLoader}. Their {@link #configure(String)} method will be called prior to be adding to the
* server's pool of {@link Managed} objects.
* Spam filters are managed components that are generally loaded dynamically via a {@link java.util.ServiceLoader}.
* Their {@link #configure(String)} method will be called prior to be adding to the server's pool of {@link Managed}
* objects.
* <p/>
* Spam filters must be annotated with {@link FilterSpam}, a name binding annotation that
* restricts the endpoints to which the filter may apply.
* Spam filters must be annotated with {@link FilterSpam}, a name binding annotation that restricts the endpoints to
* which the filter may apply.
*/
public interface SpamFilter extends ContainerRequestFilter, Managed {
/**
* Configures this spam filter. This method will be called before the filter is added to the server's pool
* of managed objects and before the server processes any requests.
* Configures this spam filter. This method will be called before the filter is added to the server's pool of managed
* objects and before the server processes any requests.
*
* @param environmentName the name of the environment in which this filter is running (e.g. "staging" or "production")
* @param environmentName the name of the environment in which this filter is running (e.g. "staging" or
* "production")
* @throws IOException if the filter could not read its configuration source for any reason
*/
void configure(String environmentName) throws IOException;
@@ -41,10 +41,27 @@ public interface SpamFilter extends ContainerRequestFilter, Managed {
ReportSpamTokenProvider getReportSpamTokenProvider();
/**
* Return any and all reported message listeners controlled by the spam filter. Listeners will be registered with the
* Return a reported message listener controlled by the spam filter. Listeners will be registered with the
* {@link org.whispersystems.textsecuregcm.storage.ReportMessageManager}.
*
* @return a list of reported message listeners controlled by the spam filter
* @return a reported message listener controlled by the spam filter
*/
List<ReportedMessageListener> getReportedMessageListeners();
ReportedMessageListener getReportedMessageListener();
/**
* Return a rate limit challenge listener. Listeners will be registered with the
* {@link org.whispersystems.textsecuregcm.limits.RateLimitChallengeManager}
*
* @return a {@link RateLimitChallengeListener} controlled by the spam filter
*/
RateLimitChallengeListener getRateLimitChallengeListener();
/**
* Return a spam checker that will be called on message sends via the
* {@link org.whispersystems.textsecuregcm.controllers.MessageController} to determine whether a specific message
* spend is spam.
*
* @return a {@link SpamChecker} controlled by the spam filter
*/
SpamChecker getSpamChecker();
}