From f8e22ae674b84ef1c33feaa778659ab717ab1041 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Mon, 2 Nov 2026 14:13:54 -0500 Subject: [PATCH] Enforce attachment upload rate limits --- .../controllers/AttachmentControllerV4.java | 26 +++++++------------ .../AttachmentControllerV4Test.java | 23 ++++++++-------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4.java index bf314a212..a4d791b89 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4.java @@ -9,13 +9,11 @@ import com.google.common.net.HttpHeaders; import io.dropwizard.auth.Auth; import io.micrometer.core.instrument.DistributionSummary; import io.micrometer.core.instrument.Metrics; -import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tags; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.headers.Header; import io.swagger.v3.oas.annotations.responses.ApiResponse; -import jakarta.validation.Valid; import jakarta.validation.constraints.Positive; import jakarta.ws.rs.ClientErrorException; import jakarta.ws.rs.GET; @@ -27,7 +25,6 @@ import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import java.security.SecureRandom; import java.util.Map; -import java.util.Optional; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.glassfish.jersey.server.ManagedAsync; @@ -104,30 +101,27 @@ public class AttachmentControllerV4 { public AttachmentDescriptorV3 getAttachmentUploadForm( @Auth AuthenticatedDevice auth, @Parameter(description = "The size of the attachment to upload in bytes") - @QueryParam("uploadLength") final @Valid Optional<@Positive Long> maybeUploadLength, + @QueryParam("uploadLength") @Positive final long uploadLength, @HeaderParam(HttpHeaders.USER_AGENT) @Nullable final String userAgent) throws RateLimitExceededException { - final long uploadLength = maybeUploadLength.orElse(maxUploadLength); if (uploadLength > maxUploadLength) { throw new ClientErrorException("exceeded maximum uploadLength", Response.Status.REQUEST_ENTITY_TOO_LARGE); } countRateLimiter.validate(auth.accountIdentifier()); - if (maybeUploadLength.isPresent()) { - // Ideally we'd check these two rate limits transactionally and only update them if both permits were acquired. - // However, just undoing the first modification if the second one fails is close enough for our purposes - try { - bytesRateLimiter.validate(auth.accountIdentifier(), maybeUploadLength.get()); - } catch (RateLimitExceededException e) { - countRateLimiter.restorePermits(auth.accountIdentifier(), 1); - throw e; - } + + // Ideally we'd check these two rate limits transactionally and only update them if both permits were acquired. + // However, just undoing the first modification if the second one fails is close enough for our purposes + try { + bytesRateLimiter.validate(auth.accountIdentifier(), uploadLength); + } catch (RateLimitExceededException e) { + countRateLimiter.restorePermits(auth.accountIdentifier(), 1); + throw e; } DistributionSummary.builder(ATTACHMENT_SIZE_NAME) - .tags(Tags.of(UserAgentTagUtil.getPlatformTag(userAgent), - Tag.of("uploadLengthSupplied", Boolean.toString(maybeUploadLength.isPresent())))) + .tags(Tags.of(UserAgentTagUtil.getPlatformTag(userAgent))) .register(Metrics.globalRegistry) .record(uploadLength); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4Test.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4Test.java index 569076d84..6bed852ef 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4Test.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4Test.java @@ -144,22 +144,12 @@ class AttachmentControllerV4Test { .isEqualTo(Response.Status.REQUEST_ENTITY_TOO_LARGE.getStatusCode()); } - @Test - void missingUploadLengthDoesNotRateLimit() throws RateLimitExceededException { - doThrow(RateLimitExceededException.class).when(BYTE_RATE_LIMITER).validate(AuthHelper.VALID_UUID); - - assertThatNoException().isThrownBy(() -> resources.getJerseyTest() - .target("/v4/attachments/form/upload") - .request() - .header("Authorization", CDN3_ENABLED_CREDS) - .get().getStatus()); - } - @Test void countRateLimitExceeded() throws RateLimitExceededException { doThrow(RateLimitExceededException.class).when(COUNT_RATE_LIMITER).validate(AuthHelper.VALID_UUID); assertThat(resources.getJerseyTest() .target("/v4/attachments/form/upload") + .queryParam("uploadLength", MAX_UPLOAD_LENGTH) .request() .header("Authorization", CDN3_ENABLED_CREDS) .get().getStatus()) @@ -202,6 +192,7 @@ class AttachmentControllerV4Test { void testV4GcsForm() throws MalformedURLException { AttachmentDescriptorV3 descriptor = resources.getJerseyTest() .target("/v4/attachments/form/upload") + .queryParam("uploadLength", MAX_UPLOAD_LENGTH) .request() .header("Authorization", CDN3_DISABLED_CREDS) .get(AttachmentDescriptorV3.class); @@ -209,6 +200,16 @@ class AttachmentControllerV4Test { assertValidCdn2Response(descriptor); } + @Test + void testV4GcsFormNoUploadLength() throws MalformedURLException { + assertThat(resources.getJerseyTest() + .target("/v4/attachments/form/upload") + .request() + .header("Authorization", CDN3_DISABLED_CREDS) + .get() + .getStatus()).isEqualTo(400); + } + private static void assertValidCdn2Response(final AttachmentDescriptorV3 descriptor) throws MalformedURLException { assertThat(descriptor.key()).isNotBlank(); assertThat(descriptor.cdn()).isEqualTo(2);