feat(s3): add presigned POST upload support (#120)

Implement S3 presigned POST (browser-based) uploads via multipart/form-data
POST to /{bucket}. This is the standard AWS mechanism for browser-based
file uploads using pre-signed policies.

Changes:
- S3Controller: detect multipart/form-data POST on /{bucket} and route to
  handlePresignedPost instead of returning InvalidArgument
- Parse multipart body to extract form fields (key, policy, Content-Type,
  x-amz-credential, x-amz-signature, etc.) and file data
- Validate content-length-range from the Base64-encoded policy JSON
- Store object via S3Service.putObject with Content-Type from form field
- Return 204 with ETag and Location headers (AWS-compatible response)

Tests:
- 11 new integration tests covering: basic upload, binary data, content-length
  validation (reject + accept), missing key/file fields, no-policy upload,
  Content-Type precedence (form field over file part), non-existent bucket

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Matej ŠnuderlandDevin AI authored and GitHub committed 2026-03-30 23:17:07 -05:00
1 parent 190773eda6
commit 1e59f8dc59
2 files changed
+548

No files matched your search

@@ -0,0 +1,291 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class S3PresignedPostIntegrationTest {
private static final String BUCKET = "presigned-post-bucket";
private static final DateTimeFormatter AMZ_DATE_FORMAT =
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
@Test
@Order(1)
void createBucket() {
given()
.when()
.put("/" + BUCKET)
.then()
.statusCode(200);
}
@Test
@Order(10)
void presignedPostUploadsObject() {
String key = "uploads/test-file.txt";
String fileContent = "Hello from presigned POST!";
String contentType = "text/plain";
String policy = buildPolicy(BUCKET, key, contentType, 0, 10485760);
String policyBase64 = Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8));
given()
.multiPart("key", key)
.multiPart("Content-Type", contentType)
.multiPart("policy", policyBase64)
.multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
.multiPart("x-amz-credential", "AKIAIOSFODNN7EXAMPLE/20260330/us-east-1/s3/aws4_request")
.multiPart("x-amz-date", AMZ_DATE_FORMAT.format(Instant.now()))
.multiPart("x-amz-signature", "dummysignature")
.multiPart("file", "test-file.txt", fileContent.getBytes(StandardCharsets.UTF_8), contentType)
.when()
.post("/" + BUCKET)
.then()
.statusCode(204)
.header("ETag", notNullValue());
// Verify the object was stored correctly
given()
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.header("Content-Type", equalTo(contentType))
.body(equalTo(fileContent));
}
@Test
@Order(20)
void presignedPostWithBinaryData() {
String key = "uploads/binary-data.bin";
byte[] binaryData = new byte[256];
for (int i = 0; i < 256; i++) {
binaryData[i] = (byte) i;
}
String policy = buildPolicy(BUCKET, key, "application/octet-stream", 0, 10485760);
String policyBase64 = Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8));
given()
.multiPart("key", key)
.multiPart("Content-Type", "application/octet-stream")
.multiPart("policy", policyBase64)
.multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
.multiPart("x-amz-credential", "AKIAIOSFODNN7EXAMPLE/20260330/us-east-1/s3/aws4_request")
.multiPart("x-amz-date", AMZ_DATE_FORMAT.format(Instant.now()))
.multiPart("x-amz-signature", "dummysignature")
.multiPart("file", "binary-data.bin", binaryData, "application/octet-stream")
.when()
.post("/" + BUCKET)
.then()
.statusCode(204)
.header("ETag", notNullValue());
// Verify the binary object was stored correctly
byte[] retrieved = given()
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.extract().body().asByteArray();
org.junit.jupiter.api.Assertions.assertArrayEquals(binaryData, retrieved);
}
@Test
@Order(30)
void presignedPostRejectsExceedingContentLength() {
String key = "uploads/too-large.txt";
// Create data that exceeds the max content-length-range of 10 bytes
String fileContent = "This content is definitely longer than 10 bytes";
String policy = buildPolicy(BUCKET, key, "text/plain", 0, 10);
String policyBase64 = Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8));
given()
.multiPart("key", key)
.multiPart("Content-Type", "text/plain")
.multiPart("policy", policyBase64)
.multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
.multiPart("x-amz-credential", "AKIAIOSFODNN7EXAMPLE/20260330/us-east-1/s3/aws4_request")
.multiPart("x-amz-date", AMZ_DATE_FORMAT.format(Instant.now()))
.multiPart("x-amz-signature", "dummysignature")
.multiPart("file", "too-large.txt", fileContent.getBytes(StandardCharsets.UTF_8), "text/plain")
.when()
.post("/" + BUCKET)
.then()
.statusCode(400)
.body(containsString("EntityTooLarge"));
}
@Test
@Order(40)
void presignedPostRequiresKeyField() {
given()
.multiPart("Content-Type", "text/plain")
.multiPart("policy", "dummypolicy")
.multiPart("file", "test.txt", "content".getBytes(StandardCharsets.UTF_8), "text/plain")
.when()
.post("/" + BUCKET)
.then()
.statusCode(400)
.body(containsString("InvalidArgument"));
}
@Test
@Order(50)
void presignedPostRequiresFileField() {
given()
.multiPart("key", "uploads/no-file.txt")
.multiPart("Content-Type", "text/plain")
.multiPart("policy", "dummypolicy")
.when()
.post("/" + BUCKET)
.then()
.statusCode(400)
.body(containsString("InvalidArgument"));
}
@Test
@Order(60)
void presignedPostWithoutPolicySkipsValidation() {
String key = "uploads/no-policy.txt";
String fileContent = "Uploaded without policy";
given()
.multiPart("key", key)
.multiPart("Content-Type", "text/plain")
.multiPart("file", "no-policy.txt", fileContent.getBytes(StandardCharsets.UTF_8), "text/plain")
.when()
.post("/" + BUCKET)
.then()
.statusCode(204)
.header("ETag", notNullValue());
// Verify the object was stored
given()
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.body(equalTo(fileContent));
}
@Test
@Order(70)
void presignedPostContentTypeFromFormField() {
String key = "uploads/typed-file.json";
String fileContent = "{\"test\": true}";
String policy = buildPolicy(BUCKET, key, "application/json", 0, 10485760);
String policyBase64 = Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8));
given()
.multiPart("key", key)
.multiPart("Content-Type", "application/json")
.multiPart("policy", policyBase64)
.multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
.multiPart("x-amz-credential", "AKIAIOSFODNN7EXAMPLE/20260330/us-east-1/s3/aws4_request")
.multiPart("x-amz-date", AMZ_DATE_FORMAT.format(Instant.now()))
.multiPart("x-amz-signature", "dummysignature")
.multiPart("file", "typed-file.json", fileContent.getBytes(StandardCharsets.UTF_8), "application/octet-stream")
.when()
.post("/" + BUCKET)
.then()
.statusCode(204);
// Content-Type should come from the form field, not the file part
given()
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.header("Content-Type", equalTo("application/json"));
}
@Test
@Order(80)
void presignedPostToNonExistentBucketFails() {
given()
.multiPart("key", "test.txt")
.multiPart("file", "test.txt", "data".getBytes(StandardCharsets.UTF_8), "text/plain")
.when()
.post("/nonexistent-presigned-bucket")
.then()
.statusCode(404)
.body(containsString("NoSuchBucket"));
}
@Test
@Order(90)
void presignedPostWithContentLengthWithinRange() {
String key = "uploads/within-range.txt";
// Exactly 5 bytes, within range [1, 100]
String fileContent = "12345";
String policy = buildPolicy(BUCKET, key, "text/plain", 1, 100);
String policyBase64 = Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8));
given()
.multiPart("key", key)
.multiPart("Content-Type", "text/plain")
.multiPart("policy", policyBase64)
.multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
.multiPart("x-amz-credential", "AKIAIOSFODNN7EXAMPLE/20260330/us-east-1/s3/aws4_request")
.multiPart("x-amz-date", AMZ_DATE_FORMAT.format(Instant.now()))
.multiPart("x-amz-signature", "dummysignature")
.multiPart("file", "within-range.txt", fileContent.getBytes(StandardCharsets.UTF_8), "text/plain")
.when()
.post("/" + BUCKET)
.then()
.statusCode(204);
}
@Test
@Order(100)
void cleanupBucket() {
// Delete all objects
given().delete("/" + BUCKET + "/uploads/test-file.txt");
given().delete("/" + BUCKET + "/uploads/binary-data.bin");
given().delete("/" + BUCKET + "/uploads/no-policy.txt");
given().delete("/" + BUCKET + "/uploads/typed-file.json");
given().delete("/" + BUCKET + "/uploads/within-range.txt");
given()
.when()
.delete("/" + BUCKET)
.then()
.statusCode(204);
}
private String buildPolicy(String bucket, String key, String contentType, long minSize, long maxSize) {
String expiration = Instant.now().plusSeconds(3600)
.atZone(ZoneOffset.UTC)
.format(DateTimeFormatter.ISO_INSTANT);
return """
{
"expiration": "%s",
"conditions": [
{"bucket": "%s"},
{"key": "%s"},
{"Content-Type": "%s"},
["content-length-range", %d, %d]
]
}
""".formatted(expiration, bucket, key, contentType, minSize, maxSize);
}
}