From 5026242122ea344ac7a67803ced34a2f29e29f50 Mon Sep 17 00:00:00 2001 From: Chris Eager Date: Mon, 5 Oct 2026 11:30:02 -0400 Subject: [PATCH] Add positive constraint for uploadLength parameter in ArchiveController --- .../controllers/ArchiveController.java | 5 ++-- .../controllers/ArchiveControllerTest.java | 30 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ArchiveController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ArchiveController.java index cbd624f6a..ebc5e4125 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ArchiveController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/ArchiveController.java @@ -25,6 +25,7 @@ import jakarta.validation.Valid; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Positive; import jakarta.validation.constraints.PositiveOrZero; import jakarta.validation.constraints.Size; import jakarta.ws.rs.BadRequestException; @@ -595,7 +596,7 @@ public class ArchiveController { @HeaderParam(X_SIGNAL_ZK_AUTH_SIGNATURE) final BackupAuthCredentialPresentationSignature signature, @Parameter(description = "The size of the message backup to upload in bytes") - @QueryParam("uploadLength") final Optional uploadLength) + @QueryParam("uploadLength") final Optional<@Positive Long> uploadLength) throws BackupFailedZkAuthenticationException, BackupWrongCredentialTypeException, BackupPermissionException { if (account.isPresent()) { throw new BadRequestException("must not use authenticated connection for anonymous operations"); @@ -651,7 +652,7 @@ public class ArchiveController { @HeaderParam(X_SIGNAL_ZK_AUTH_SIGNATURE) final BackupAuthCredentialPresentationSignature signature, @Parameter(description = "The size of the temporary attachment to upload in bytes") - @QueryParam("uploadLength") final Optional uploadLength) + @QueryParam("uploadLength") final Optional<@Positive Long> uploadLength) throws RateLimitExceededException, BackupFailedZkAuthenticationException, BackupWrongCredentialTypeException, BackupPermissionException { if (account.isPresent()) { throw new BadRequestException("must not use authenticated connection for anonymous operations"); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/ArchiveControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/ArchiveControllerTest.java index 17455d168..a880e83a0 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/ArchiveControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/ArchiveControllerTest.java @@ -613,15 +613,16 @@ public class ArchiveControllerTest { static Stream messagesUploadForm() { return Stream.of( - Arguments.of(Optional.empty(), true), - Arguments.of(Optional.of(MAX_MESSAGE_BACKUP_SIZE), true), - Arguments.of(Optional.of(MAX_MESSAGE_BACKUP_SIZE + 1), false) + Arguments.of(Optional.empty(), 200), + Arguments.of(Optional.of(-1L), 400), + Arguments.of(Optional.of(MAX_MESSAGE_BACKUP_SIZE), 200), + Arguments.of(Optional.of(MAX_MESSAGE_BACKUP_SIZE + 1), 413) ); } @ParameterizedTest @MethodSource - public void messagesUploadForm(Optional uploadLength, boolean expectSuccess) throws VerificationFailedException, BackupException { + public void messagesUploadForm(Optional uploadLength, int expectedStatus) throws VerificationFailedException, BackupException { final BackupAuthCredentialPresentation presentation = backupAuthTestUtil.getPresentation(BackupLevel.PAID, messagesBackupKey, aci); when(backupManager.authenticateBackupUser(any(), any(), any())) @@ -637,28 +638,27 @@ public class ArchiveControllerTest { .header("X-Signal-ZK-Auth", Base64.getEncoder().encodeToString(presentation.serialize())) .header("X-Signal-ZK-Auth-Signature", "aaa") .get(); - if (expectSuccess) { - assertThat(response.getStatus()).isEqualTo(200); + assertThat(response.getStatus()).isEqualTo(expectedStatus); + if (200 == expectedStatus) { ArchiveController.UploadDescriptorResponse desc = response.readEntity(ArchiveController.UploadDescriptorResponse.class); assertThat(desc) .isEqualTo(new ArchiveController.UploadDescriptorResponse(3, "abc", Map.of("k", "v"), "example.org")); verify(backupManager).createMessageBackupUploadDescriptor(any(), eq(uploadLength.orElse(MAX_MESSAGE_BACKUP_SIZE))); - } else { - assertThat(response.getStatus()).isEqualTo(413); } } static Stream mediaUploadForm() { return Stream.of( - Arguments.of(Optional.empty(), true), - Arguments.of(Optional.of(MAX_ATTACHMENT_SIZE), true), - Arguments.of(Optional.of(MAX_ATTACHMENT_SIZE + 1), false) + Arguments.of(Optional.empty(), 200), + Arguments.of(Optional.of(-1L), 400), + Arguments.of(Optional.of(MAX_ATTACHMENT_SIZE), 200), + Arguments.of(Optional.of(MAX_ATTACHMENT_SIZE + 1), 413) ); } @ParameterizedTest @MethodSource - public void mediaUploadForm(Optional uploadLength, boolean expectSuccess) throws VerificationFailedException, BackupException, RateLimitExceededException { + public void mediaUploadForm(Optional uploadLength, int expectedSuccess) throws VerificationFailedException, BackupException, RateLimitExceededException { final BackupAuthCredentialPresentation presentation = backupAuthTestUtil.getPresentation(BackupLevel.PAID, messagesBackupKey, aci); when(backupManager.authenticateBackupUser(any(), any(), any())) @@ -673,15 +673,13 @@ public class ArchiveControllerTest { .header("X-Signal-ZK-Auth", Base64.getEncoder().encodeToString(presentation.serialize())) .header("X-Signal-ZK-Auth-Signature", "aaa") .get(); - if (expectSuccess) { - assertThat(response.getStatus()).isEqualTo(200); + assertThat(response.getStatus()).isEqualTo(expectedSuccess); + if (200 == expectedSuccess) { final ArchiveController.UploadDescriptorResponse desc = response.readEntity(ArchiveController.UploadDescriptorResponse.class); assertThat(desc) .isEqualTo(new ArchiveController.UploadDescriptorResponse(3, "abc", Map.of("k", "v"), "example.org")); verify(backupManager).createTemporaryAttachmentUploadDescriptor(any(), eq(uploadLength.orElse(MAX_ATTACHMENT_SIZE))); - } else { - assertThat(response.getStatus()).isEqualTo(413); } }