Skip to content

Commit

Permalink
Enforce story ratelimit
Browse files Browse the repository at this point in the history
  • Loading branch information
katherine-signal authored Nov 16, 2023
1 parent 216ac72 commit 041aa86
Showing 1 changed file with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ public class MessageController {
private final ReportSpamTokenProvider reportSpamTokenProvider;
private final ClientReleaseManager clientReleaseManager;
private final DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager;

private static final String REJECT_OVERSIZE_MESSAGE_COUNTER = name(MessageController.class, "rejectOversizeMessage");
private static final String SENT_MESSAGE_COUNTER_NAME = name(MessageController.class, "sentMessages");
private static final String CONTENT_SIZE_DISTRIBUTION_NAME = name(MessageController.class, "messageContentSize");
Expand Down Expand Up @@ -279,7 +278,7 @@ public Response sendMessage(@Auth Optional<AuthenticatedAccount> source,
}

if (isStory) {
checkStoryRateLimit(destination.get(), userAgent);
rateLimiters.getStoriesLimiter().validate(destination.get().getUuid());
}

final Set<Byte> excludedDeviceIds;
Expand Down Expand Up @@ -378,7 +377,7 @@ public Response sendMultiRecipientMessage(
@QueryParam("ts") long timestamp,
@QueryParam("urgent") @DefaultValue("true") final boolean isUrgent,
@QueryParam("story") boolean isStory,
@NotNull @Valid MultiRecipientMessage multiRecipientMessage) {
@NotNull @Valid MultiRecipientMessage multiRecipientMessage) throws RateLimitExceededException {

final Map<ServiceIdentifier, Account> accountsByServiceIdentifier = new HashMap<>();

Expand Down Expand Up @@ -412,17 +411,20 @@ public Response sendMultiRecipientMessage(

Collection<AccountMismatchedDevices> accountMismatchedDevices = new ArrayList<>();
Collection<AccountStaleDevices> accountStaleDevices = new ArrayList<>();
accountsByServiceIdentifier.forEach((serviceIdentifier, account) -> {

for (Map.Entry<ServiceIdentifier, Account> entry : accountsByServiceIdentifier.entrySet()) {
final ServiceIdentifier serviceIdentifier = entry.getKey();
final Account account = entry.getValue();

if (isStory) {
checkStoryRateLimit(account, userAgent);
rateLimiters.getStoriesLimiter().validate(account.getUuid());
}

Set<Byte> deviceIds = accountToDeviceIdAndRegistrationIdMap
.getOrDefault(account, Collections.emptySet())
.stream()
.map(Pair::first)
.collect(Collectors.toSet());
.getOrDefault(account, Collections.emptySet())
.stream()
.map(Pair::first)
.collect(Collectors.toSet());

try {
DestinationDeviceValidator.validateCompleteDeviceList(account, deviceIds, Collections.emptySet());
Expand All @@ -439,7 +441,8 @@ public Response sendMultiRecipientMessage(
} catch (StaleDevicesException e) {
accountStaleDevices.add(new AccountStaleDevices(serviceIdentifier, new StaleDevices(e.getStaleDevices())));
}
});
}

if (!accountMismatchedDevices.isEmpty()) {
return Response
.status(409)
Expand Down Expand Up @@ -735,14 +738,6 @@ private void sendCommonPayloadMessage(Account destinationAccount,
}
}

private void checkStoryRateLimit(Account destination, String userAgent) {
try {
rateLimiters.getStoriesLimiter().validate(destination.getUuid());
} catch (final RateLimitExceededException e) {
Metrics.counter(RATE_LIMITED_STORIES_COUNTER_NAME, Tags.of(UserAgentTagUtil.getPlatformTag(userAgent))).increment();
}
}

private void checkMessageRateLimit(AuthenticatedAccount source, Account destination, String userAgent)
throws RateLimitExceededException {
final String senderCountryCode = Util.getCountryCode(source.getAccount().getNumber());
Expand Down

0 comments on commit 041aa86

Please sign in to comment.