diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/attachments/AttachmentGenerator.java b/service/src/main/java/org/whispersystems/textsecuregcm/attachments/AttachmentGenerator.java index 87457277d..c3ee957af 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/attachments/AttachmentGenerator.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/attachments/AttachmentGenerator.java @@ -10,7 +10,7 @@ public interface AttachmentGenerator { record Descriptor(Map headers, String signedUploadLocation) {} - Descriptor generateAttachment(final String key); + Descriptor generateAttachment(final String key, final long uploadLength); long maxUploadSizeInBytes(); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/attachments/GcsAttachmentGenerator.java b/service/src/main/java/org/whispersystems/textsecuregcm/attachments/GcsAttachmentGenerator.java index 1260f8221..647ec1b75 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/attachments/GcsAttachmentGenerator.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/attachments/GcsAttachmentGenerator.java @@ -22,20 +22,23 @@ public class GcsAttachmentGenerator implements AttachmentGenerator { @Nonnull private final CanonicalRequestSigner canonicalRequestSigner; - private final int maxSizeInBytes; + private final long maxSizeInBytes; public GcsAttachmentGenerator(@Nonnull String domain, @Nonnull String email, - int maxSizeInBytes, @Nonnull String pathPrefix, @Nonnull String rsaSigningKey) + long maxSizeInBytes, @Nonnull String pathPrefix, @Nonnull String rsaSigningKey) throws IOException, InvalidKeyException, InvalidKeySpecException { this.maxSizeInBytes = maxSizeInBytes; - this.canonicalRequestGenerator = new CanonicalRequestGenerator(domain, email, maxSizeInBytes, pathPrefix); + this.canonicalRequestGenerator = new CanonicalRequestGenerator(domain, email, pathPrefix); this.canonicalRequestSigner = new CanonicalRequestSigner(rsaSigningKey); } @Override - public Descriptor generateAttachment(final String key) { + public Descriptor generateAttachment(final String key, final long uploadLength) { + if (uploadLength > maxSizeInBytes) { + throw new IllegalArgumentException("uploadLength " + uploadLength + " exceeds maximum " + maxSizeInBytes); + } final ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); - final CanonicalRequest canonicalRequest = canonicalRequestGenerator.createFor(key, now); + final CanonicalRequest canonicalRequest = canonicalRequestGenerator.createFor(key, now, uploadLength); return new Descriptor(getHeaderMap(canonicalRequest), getSignedUploadLocation(canonicalRequest)); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/attachments/TusAttachmentGenerator.java b/service/src/main/java/org/whispersystems/textsecuregcm/attachments/TusAttachmentGenerator.java index 94e6789a5..467fb2bcc 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/attachments/TusAttachmentGenerator.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/attachments/TusAttachmentGenerator.java @@ -28,9 +28,13 @@ public class TusAttachmentGenerator implements AttachmentGenerator { } @Override - public Descriptor generateAttachment(final String key) { + public Descriptor generateAttachment(final String key, final long uploadLength) { + if (uploadLength > maxUploadSize) { + throw new IllegalArgumentException("uploadLength " + uploadLength + " exceeds maximum " + maxUploadSize); + } + final String token = jwtGenerator.generateJwt(ATTACHMENTS, key, - builder -> builder.withClaim(JwtGenerator.MAX_LENGTH_CLAIM_KEY, maxUploadSize)); + builder -> builder.withClaim(JwtGenerator.MAX_LENGTH_CLAIM_KEY, uploadLength)); final String b64Key = Base64.getEncoder().encodeToString(key.getBytes(StandardCharsets.UTF_8)); final Map headers = Map.of( diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/backup/BackupManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/backup/BackupManager.java index 5650ca5c5..1cac2ce86 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/backup/BackupManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/backup/BackupManager.java @@ -176,7 +176,7 @@ public class BackupManager { rateLimiters.forDescriptor(RateLimiters.For.BACKUP_ATTACHMENT).validate(rateLimitKey(backupUser)); final String attachmentKey = AttachmentUtil.generateAttachmentKey(secureRandom); - final AttachmentGenerator.Descriptor descriptor = tusAttachmentGenerator.generateAttachment(attachmentKey); + final AttachmentGenerator.Descriptor descriptor = tusAttachmentGenerator.generateAttachment(attachmentKey, tusAttachmentGenerator.maxUploadSizeInBytes()); return new BackupUploadDescriptor(3, attachmentKey, descriptor.headers(), descriptor.signedUploadLocation()); } 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 fb57ee747..1233584e9 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4.java @@ -7,17 +7,25 @@ package org.whispersystems.textsecuregcm.controllers; import io.dropwizard.auth.Auth; 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 io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import jakarta.validation.constraints.Positive; +import jakarta.ws.rs.ClientErrorException; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; 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 org.whispersystems.textsecuregcm.attachments.AttachmentGenerator; +import org.whispersystems.textsecuregcm.attachments.AttachmentUtil; import org.whispersystems.textsecuregcm.attachments.GcsAttachmentGenerator; import org.whispersystems.textsecuregcm.attachments.TusAttachmentGenerator; import org.whispersystems.textsecuregcm.auth.AuthenticatedDevice; @@ -25,7 +33,6 @@ import org.whispersystems.textsecuregcm.entities.AttachmentDescriptorV3; import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager; import org.whispersystems.textsecuregcm.limits.RateLimiter; import org.whispersystems.textsecuregcm.limits.RateLimiters; -import org.whispersystems.textsecuregcm.attachments.AttachmentUtil; /** @@ -66,19 +73,32 @@ public class AttachmentControllerV4 { description = """ Retrieve an upload form that can be used to perform a resumable upload. The response will include a cdn number indicating what protocol should be used to perform the upload. + + Uploads with the returned form will be limited to a maximum size of the provided uploadLength. """ ) @ApiResponse(responseCode = "200", description = "Success, response body includes upload form", useReturnTypeSchema = true) + @ApiResponse(responseCode = "400", description = "The provided uploadLength was not valid") + @ApiResponse(responseCode = "413", description = "The provided uploadLength is larger than the maximum supported upload size. The maximum upload size is subject to change and is governed by `global.attachments.maxBytes`.") @ApiResponse(responseCode = "429", description = "Too many attempts", headers = @Header( name = "Retry-After", description = "If present, an positive integer indicating the number of seconds before a subsequent attempt could succeed")) - public AttachmentDescriptorV3 getAttachmentUploadForm(@Auth AuthenticatedDevice auth) + 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) throws RateLimitExceededException { - rateLimiter.validate(auth.accountIdentifier()); final String key = AttachmentUtil.generateAttachmentKey(secureRandom); final boolean useCdn3 = this.experimentEnrollmentManager.isEnrolled(auth.accountIdentifier(), AttachmentUtil.CDN3_EXPERIMENT_NAME); int cdn = useCdn3 ? 3 : 2; - final AttachmentGenerator.Descriptor descriptor = this.attachmentGenerators.get(cdn).generateAttachment(key); + final AttachmentGenerator attachmentGenerator = this.attachmentGenerators.get(cdn); + final long uploadLength = maybeUploadLength.orElse(attachmentGenerator.maxUploadSizeInBytes()); + if (uploadLength > attachmentGenerator.maxUploadSizeInBytes()) { + throw new ClientErrorException("exceeded maximum uploadLength", Response.Status.REQUEST_ENTITY_TOO_LARGE); + } + + rateLimiter.validate(auth.accountIdentifier()); + final AttachmentGenerator.Descriptor descriptor = attachmentGenerator.generateAttachment(key, uploadLength); return new AttachmentDescriptorV3(cdn, key, descriptor.headers(), descriptor.signedUploadLocation()); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/gcp/CanonicalRequest.java b/service/src/main/java/org/whispersystems/textsecuregcm/gcp/CanonicalRequest.java index c4a94686b..b4fd3fac1 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/gcp/CanonicalRequest.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/gcp/CanonicalRequest.java @@ -27,9 +27,9 @@ public class CanonicalRequest { @Nonnull private final String domain; - private final int maxSizeInBytes; + private final long maxSizeInBytes; - public CanonicalRequest(@Nonnull String canonicalRequest, @Nonnull String resourcePath, @Nonnull String canonicalQuery, @Nonnull String activeDatetime, @Nonnull String credentialScope, @Nonnull String domain, int maxSizeInBytes) { + public CanonicalRequest(@Nonnull String canonicalRequest, @Nonnull String resourcePath, @Nonnull String canonicalQuery, @Nonnull String activeDatetime, @Nonnull String credentialScope, @Nonnull String domain, long maxSizeInBytes) { this.canonicalRequest = canonicalRequest; this.resourcePath = resourcePath; this.canonicalQuery = canonicalQuery; @@ -69,7 +69,7 @@ public class CanonicalRequest { return domain; } - public int getMaxSizeInBytes() { + public long getMaxSizeInBytes() { return maxSizeInBytes; } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/gcp/CanonicalRequestGenerator.java b/service/src/main/java/org/whispersystems/textsecuregcm/gcp/CanonicalRequestGenerator.java index 926db0a75..fffe0e3b2 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/gcp/CanonicalRequestGenerator.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/gcp/CanonicalRequestGenerator.java @@ -26,19 +26,16 @@ public class CanonicalRequestGenerator { @Nonnull private final String email; - private final int maxSizeBytes; - @Nonnull private final String pathPrefix; - public CanonicalRequestGenerator(@Nonnull String domain, @Nonnull String email, int maxSizeBytes, @Nonnull String pathPrefix) { + public CanonicalRequestGenerator(@Nonnull String domain, @Nonnull String email, @Nonnull String pathPrefix) { this.domain = domain; this.email = email; - this.maxSizeBytes = maxSizeBytes; this.pathPrefix = pathPrefix; } - public CanonicalRequest createFor(@Nonnull final String key, @Nonnull final ZonedDateTime now) { + public CanonicalRequest createFor(@Nonnull final String key, @Nonnull final ZonedDateTime now, final long uploadLength) { final StringBuilder result = new StringBuilder("POST\n"); final StringBuilder resourcePathBuilder = new StringBuilder(); @@ -58,7 +55,7 @@ public class CanonicalRequestGenerator { result.append(canonicalQuery).append('\n'); result.append("host:").append(domain).append('\n'); - result.append("x-goog-content-length-range:1,").append(maxSizeBytes).append('\n'); + result.append("x-goog-content-length-range:1,").append(uploadLength).append('\n'); result.append("x-goog-resumable:start\n"); result.append('\n'); @@ -66,7 +63,7 @@ public class CanonicalRequestGenerator { result.append("UNSIGNED-PAYLOAD"); - return new CanonicalRequest(result.toString(), resourcePath, canonicalQuery, activeDatetime, makeCredentialScope(now), domain, maxSizeBytes); + return new CanonicalRequest(result.toString(), resourcePath, canonicalQuery, activeDatetime, makeCredentialScope(now), domain, uploadLength); } private String makeCredentialScope(@Nonnull ZonedDateTime now) { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AttachmentsGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AttachmentsGrpcService.java index 029f3034e..8f86ac509 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AttachmentsGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/AttachmentsGrpcService.java @@ -11,7 +11,9 @@ import org.signal.chat.attachments.GetUploadFormRequest; import org.signal.chat.attachments.GetUploadFormResponse; import org.signal.chat.attachments.SimpleAttachmentsGrpc; import org.signal.chat.common.UploadForm; +import org.signal.chat.errors.FailedPrecondition; import org.whispersystems.textsecuregcm.attachments.AttachmentGenerator; +import org.whispersystems.textsecuregcm.attachments.AttachmentUtil; import org.whispersystems.textsecuregcm.attachments.GcsAttachmentGenerator; import org.whispersystems.textsecuregcm.attachments.TusAttachmentGenerator; import org.whispersystems.textsecuregcm.auth.grpc.AuthenticatedDevice; @@ -20,7 +22,6 @@ import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager; import org.whispersystems.textsecuregcm.limits.RateLimiter; import org.whispersystems.textsecuregcm.limits.RateLimiters; -import org.whispersystems.textsecuregcm.attachments.AttachmentUtil; public class AttachmentsGrpcService extends SimpleAttachmentsGrpc.AttachmentsImplBase { @@ -45,12 +46,19 @@ public class AttachmentsGrpcService extends SimpleAttachmentsGrpc.AttachmentsImp @Override public GetUploadFormResponse getUploadForm(final GetUploadFormRequest request) throws RateLimitExceededException { final AuthenticatedDevice auth = AuthenticationUtil.requireAuthenticatedDevice(); - rateLimiter.validate(auth.accountIdentifier()); final String key = AttachmentUtil.generateAttachmentKey(secureRandom); final boolean useCdn3 = this.experimentEnrollmentManager.isEnrolled(auth.accountIdentifier(), AttachmentUtil.CDN3_EXPERIMENT_NAME); final int cdn = useCdn3 ? 3 : 2; - final AttachmentGenerator.Descriptor descriptor = this.attachmentGenerators.get(cdn).generateAttachment(key); + final AttachmentGenerator attachmentGenerator = this.attachmentGenerators.get(cdn); + if (request.getUploadLength() > attachmentGenerator.maxUploadSizeInBytes()) { + return GetUploadFormResponse.newBuilder() + .setExceedsMaxUploadLength(FailedPrecondition.getDefaultInstance()) + .build(); + } + + rateLimiter.validate(auth.accountIdentifier()); + final AttachmentGenerator.Descriptor descriptor = attachmentGenerator.generateAttachment(key, request.getUploadLength()); return GetUploadFormResponse.newBuilder().setUploadForm(UploadForm.newBuilder() .setCdn(cdn) .setKey(key) diff --git a/service/src/main/proto/org/signal/chat/attachments.proto b/service/src/main/proto/org/signal/chat/attachments.proto index 9634407dc..3939002e2 100644 --- a/service/src/main/proto/org/signal/chat/attachments.proto +++ b/service/src/main/proto/org/signal/chat/attachments.proto @@ -11,6 +11,8 @@ package org.signal.chat.attachments; import "org/signal/chat/common.proto"; import "org/signal/chat/require.proto"; +import "org/signal/chat/errors.proto"; +import "org/signal/chat/tag.proto"; service Attachments { option (require.auth) = AUTH_ONLY_AUTHENTICATED; @@ -19,7 +21,19 @@ service Attachments { rpc GetUploadForm(GetUploadFormRequest) returns (GetUploadFormResponse) {} } -message GetUploadFormRequest {} +message GetUploadFormRequest { + // The length of the attachment for the requested upload form. Uploads + // performed with this form will be limited to the provided length. + uint64 uploadLength = 1 [(require.range) = {min: 1}]; +} + message GetUploadFormResponse { - common.UploadForm upload_form = 1; + oneof outcome { + common.UploadForm upload_form = 1; + + // The request size was larger than the maximum supported upload size. The + // maximum upload size is subject to change and is governed by + // `global.attachments.maxBytes` + errors.FailedPrecondition exceeds_max_upload_length = 2 [(tag.reason) = "oversize_upload"]; + } } 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 00477bce0..a81846e4e 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4Test.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/AttachmentControllerV4Test.java @@ -27,11 +27,14 @@ import java.security.spec.InvalidKeySpecException; import java.util.Base64; import java.util.HashMap; import java.util.Map; +import jakarta.ws.rs.core.Response; import org.assertj.core.api.Assertions; import org.assertj.core.api.InstanceOfAssertFactories; import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.whispersystems.textsecuregcm.attachments.GcsAttachmentGenerator; import org.whispersystems.textsecuregcm.attachments.TusAttachmentGenerator; import org.whispersystems.textsecuregcm.attachments.TusConfiguration; @@ -65,6 +68,7 @@ class AttachmentControllerV4Test { private static final byte[] TUS_SECRET = TestRandomUtil.nextBytes(32); private static final String TUS_URL = "https://example.com/uploads"; + private static final long MAX_UPLOAD_LENGTH = 1000; public static final String RSA_PRIVATE_KEY_PEM; @@ -96,7 +100,7 @@ class AttachmentControllerV4Test { .setTestContainerFactory(new GrizzlyWebTestContainerFactory()) .addProvider(new AttachmentControllerV4(RATE_LIMITERS, gcsAttachmentGenerator, - new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL, 1000)), + new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL, MAX_UPLOAD_LENGTH)), EXPERIMENT_MANAGER)) .build(); } catch (IOException | InvalidKeyException | InvalidKeySpecException e) { @@ -104,10 +108,34 @@ class AttachmentControllerV4Test { } } + @ParameterizedTest + @ValueSource(longs = {-1, 0}) + void testInvalidUploadLength(final long uploadLength) { + assertThat(resources.getJerseyTest() + .target("/v4/attachments/form/upload") + .queryParam("uploadLength", uploadLength) + .request() + .header("Authorization", CDN3_ENABLED_CREDS) + .get().getStatus()) + .isEqualTo(Response.Status.BAD_REQUEST.getStatusCode()); + } + + @Test + void testUploadTooLarge() { + assertThat(resources.getJerseyTest() + .target("/v4/attachments/form/upload") + .queryParam("uploadLength", MAX_UPLOAD_LENGTH + 1) + .request() + .header("Authorization", CDN3_ENABLED_CREDS) + .get().getStatus()) + .isEqualTo(Response.Status.REQUEST_ENTITY_TOO_LARGE.getStatusCode()); + } + @Test void testV4TusForm() { AttachmentDescriptorV3 descriptor = resources.getJerseyTest() .target("/v4/attachments/form/upload") + .queryParam("uploadLength", MAX_UPLOAD_LENGTH) .request() .header("Authorization", CDN3_ENABLED_CREDS) .get(AttachmentDescriptorV3.class); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AttachmentsGrpcServiceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AttachmentsGrpcServiceTest.java index f3a54ab70..293dea5d3 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AttachmentsGrpcServiceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/grpc/AttachmentsGrpcServiceTest.java @@ -31,11 +31,14 @@ import java.util.stream.Collectors; import org.assertj.core.api.Assertions; import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.signal.chat.attachments.AttachmentsGrpc; import org.signal.chat.attachments.GetUploadFormRequest; import org.signal.chat.attachments.GetUploadFormResponse; import org.signal.chat.common.UploadForm; +import org.whispersystems.textsecuregcm.attachments.AttachmentUtil; import org.whispersystems.textsecuregcm.attachments.GcsAttachmentGenerator; import org.whispersystems.textsecuregcm.attachments.TusAttachmentGenerator; import org.whispersystems.textsecuregcm.attachments.TusConfiguration; @@ -44,7 +47,6 @@ import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager; import org.whispersystems.textsecuregcm.limits.RateLimiter; import org.whispersystems.textsecuregcm.limits.RateLimiters; -import org.whispersystems.textsecuregcm.attachments.AttachmentUtil; import org.whispersystems.textsecuregcm.util.MockUtils; import org.whispersystems.textsecuregcm.util.TestRandomUtil; @@ -53,6 +55,7 @@ class AttachmentsGrpcServiceTest extends private static final byte[] TUS_SECRET = TestRandomUtil.nextBytes(32); private static final String TUS_URL = "https://example.com/uploads"; + private static final int MAX_UPLOAD_LENGTH = 1000; @Mock private ExperimentEnrollmentManager experimentEnrollmentManager; @@ -72,7 +75,7 @@ class AttachmentsGrpcServiceTest extends final GcsAttachmentGenerator gcsAttachmentGenerator = new GcsAttachmentGenerator( "some-cdn.signal.org", "signal@example.com", 1000, "/attach-here", gcsPrivateKeyPem); final TusAttachmentGenerator tusAttachmentGenerator = - new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL, 1000)); + new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL, MAX_UPLOAD_LENGTH)); return new AttachmentsGrpcService( experimentEnrollmentManager, @@ -85,13 +88,27 @@ class AttachmentsGrpcServiceTest extends } } + @ParameterizedTest + @ValueSource(longs = {-1, 0}) + void invalidUploadLength(final long uploadLength) { + GrpcTestUtils.assertStatusInvalidArgument(() -> authenticatedServiceStub() + .getUploadForm(GetUploadFormRequest.newBuilder().setUploadLength(uploadLength).build())); + } + + @Test + void uploadLengthTooBig() { + assertThat(authenticatedServiceStub() + .getUploadForm(GetUploadFormRequest.newBuilder().setUploadLength(MAX_UPLOAD_LENGTH + 1).build()) + .hasExceedsMaxUploadLength()).isTrue(); + } + @Test void getUploadFormCdn3() { when(experimentEnrollmentManager.isEnrolled(AUTHENTICATED_ACI, AttachmentUtil.CDN3_EXPERIMENT_NAME)) .thenReturn(true); final GetUploadFormResponse response = authenticatedServiceStub() - .getUploadForm(GetUploadFormRequest.newBuilder().build()); + .getUploadForm(GetUploadFormRequest.newBuilder().setUploadLength(1).build()); final UploadForm uploadForm = response.getUploadForm(); assertThat(uploadForm.getCdn()).isEqualTo(3); @@ -105,11 +122,12 @@ class AttachmentsGrpcServiceTest extends @Test void getUploadFormCdn2() throws MalformedURLException { + final long uploadLength = 13; when(experimentEnrollmentManager.isEnrolled(AUTHENTICATED_ACI, AttachmentUtil.CDN3_EXPERIMENT_NAME)) .thenReturn(false); final GetUploadFormResponse response = authenticatedServiceStub() - .getUploadForm(GetUploadFormRequest.newBuilder().build()); + .getUploadForm(GetUploadFormRequest.newBuilder().setUploadLength(uploadLength).build()); final UploadForm uploadForm = response.getUploadForm(); assertThat(uploadForm.getCdn()).isEqualTo(2); @@ -117,7 +135,7 @@ class AttachmentsGrpcServiceTest extends assertThat(uploadForm.getHeadersMap()).containsExactlyInAnyOrderEntriesOf(Map.of( "host", "some-cdn.signal.org", "x-goog-resumable", "start", - "x-goog-content-length-range", "1,1000")); + "x-goog-content-length-range", "1," + uploadLength)); assertThat(uploadForm.getSignedUploadLocation()).isNotEmpty(); final URL signedUploadLocation = URI.create(uploadForm.getSignedUploadLocation()).toURL(); @@ -156,6 +174,6 @@ class AttachmentsGrpcServiceTest extends doThrow(new RateLimitExceededException(retryAfter)).when(rateLimiter).validate(any(UUID.class)); assertRateLimitExceeded(retryAfter, () -> - authenticatedServiceStub().getUploadForm(GetUploadFormRequest.newBuilder().build())); + authenticatedServiceStub().getUploadForm(GetUploadFormRequest.newBuilder().setUploadLength(1).build())); } }