From 9b7f8e40f021250fb7b67e0d25fecd9cdb6c0200 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sat, 30 May 2026 16:04:51 -0500 Subject: [PATCH] fix(s3): reject response-* overrides on unsigned requests (#1098) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(s3): reject response-* overrides on unsigned requests AWS S3 requires a signed request (Authorization header or presigned URL) when any of the six response-* query parameters are present. Unsigned requests with these parameters were silently honoured, diverging from real S3 behaviour in a way that would only surface in production. Confirmed against real S3: HTTP 400, Code=InvalidRequest, "Request specific response headers cannot be used for anonymous GET requests." Adds hasAny() to ResponseHeaderOverrides and an isSigned() check (Authorization header or X-Amz-Algorithm query param) applied to both GetObject and HeadObject before the overrides are applied. Closes #918 * fix(s3): sign response-* override tests to match AWS requirement PreSignedUrlIntegrationTest orders 11, 12, 14, and 16 made anonymous requests with response-* query params and expected 200. These tests now correctly reflect the AWS requirement that response-* overrides must be on a signed request — anonymous requests must be rejected (closes #918). --- .../floci/services/s3/S3Controller.java | 21 +- .../s3/PreSignedUrlIntegrationTest.java | 8 + ...ResponseHeaderOverrideIntegrationTest.java | 198 ++++++++++++++++++ 3 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/s3/S3ResponseHeaderOverrideIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 16cc36423f..cc0e2dba89 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java @@ -585,6 +585,10 @@ public class S3Controller { ResponseHeaderOverrides overrides = new ResponseHeaderOverrides( responseContentType, responseContentLanguage, responseExpires, responseCacheControl, responseContentDisposition, responseContentEncoding); + if (overrides.hasAny() && !isSigned(httpHeaders, uriInfo)) { + return xmlErrorResponse(new AwsException("InvalidRequest", + "Request specific response headers cannot be used for anonymous GET requests.", 400)); + } if (rangeHeader != null && rangeHeader.startsWith("bytes=")) { return handleRangeRequest(obj, rangeHeader, overrides); @@ -689,7 +693,8 @@ public class S3Controller { @HeaderParam("If-None-Match") String ifNoneMatch, @HeaderParam("If-Modified-Since") String ifModifiedSince, @HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince, - @Context UriInfo uriInfo) { + @Context UriInfo uriInfo, + @Context HttpHeaders httpHeaders) { try { key = extractObjectKey(uriInfo, bucket); S3Object obj = s3Service.headObject(bucket, key, versionId); @@ -700,6 +705,10 @@ public class S3Controller { ResponseHeaderOverrides overrides = new ResponseHeaderOverrides( responseContentType, responseContentLanguage, responseExpires, responseCacheControl, responseContentDisposition, responseContentEncoding); + if (overrides.hasAny() && !isSigned(httpHeaders, uriInfo)) { + return xmlErrorResponse(new AwsException("InvalidRequest", + "Request specific response headers cannot be used for anonymous GET requests.", 400)); + } var resp = Response.ok() .header("Content-Type", overrides.contentType() != null ? overrides.contentType() : obj.getContentType()) .header("Content-Length", obj.getSize()) @@ -1515,6 +1524,11 @@ public class S3Controller { String contentEncoding) { static final ResponseHeaderOverrides NONE = new ResponseHeaderOverrides(null, null, null, null, null, null); + boolean hasAny() { + return contentType != null || contentLanguage != null || expires != null + || cacheControl != null || contentDisposition != null || contentEncoding != null; + } + ResponseHeaderOverrides { // Real S3 ignores empty `response-*` values; @QueryParam binds "?foo=" as "" rather than null. contentType = emptyToNull(contentType); @@ -1908,6 +1922,11 @@ public class S3Controller { return null; } + private static boolean isSigned(HttpHeaders httpHeaders, UriInfo uriInfo) { + return httpHeaders.getHeaderString("Authorization") != null + || uriInfo.getQueryParameters().containsKey("X-Amz-Algorithm"); + } + private boolean hasPreconditions(String ifMatch, String ifNoneMatch, String ifModifiedSince, String ifUnmodifiedSince) { return ifMatch != null || ifNoneMatch != null || ifModifiedSince != null || ifUnmodifiedSince != null; diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/PreSignedUrlIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/PreSignedUrlIntegrationTest.java index 6f61b4dc13..39995a87a9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/PreSignedUrlIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/PreSignedUrlIntegrationTest.java @@ -19,6 +19,9 @@ class PreSignedUrlIntegrationTest { private static final String BUCKET = "presign-test-bucket"; + private static final String AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/s3/aws4_request"; + @Inject PreSignedUrlGenerator presignGenerator; @@ -138,8 +141,10 @@ class PreSignedUrlIntegrationTest { @Order(11) void getObjectAppliesResponseContentDispositionOverride() { // Stored disposition is "inline"; override should win. + // Must be a signed request per AWS spec (response-* params require Authorization or presigned URL). given() .urlEncodingEnabled(false) + .header("Authorization", AUTH_HEADER) .when() .get("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22file.txt%22") .then() @@ -152,6 +157,7 @@ class PreSignedUrlIntegrationTest { void getObjectAppliesAllResponseOverrides() { given() .urlEncodingEnabled(false) + .header("Authorization", AUTH_HEADER) .when() .get("/" + BUCKET + "/disposition-file.txt" + "?response-content-type=application%2Fpdf" @@ -187,6 +193,7 @@ class PreSignedUrlIntegrationTest { void headObjectAppliesResponseContentDispositionOverride() { given() .urlEncodingEnabled(false) + .header("Authorization", AUTH_HEADER) .when() .head("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22head.txt%22") .then() @@ -212,6 +219,7 @@ class PreSignedUrlIntegrationTest { void rangeRequestAppliesResponseContentDispositionOverride() { given() .urlEncodingEnabled(false) + .header("Authorization", AUTH_HEADER) .header("Range", "bytes=0-3") .when() .get("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22range.txt%22") diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ResponseHeaderOverrideIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ResponseHeaderOverrideIntegrationTest.java new file mode 100644 index 0000000000..a539549713 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ResponseHeaderOverrideIntegrationTest.java @@ -0,0 +1,198 @@ +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 static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +/** + * Verifies response-* header override behaviour on GetObject / HeadObject. + * + * Empirically confirmed against real AWS S3: + * HTTP 400, Code=InvalidRequest, + * Message="Request specific response headers cannot be used for anonymous GET requests." + * + * Unsigned requests that supply any response-* query parameter must be rejected. + * Signed requests (Authorization header present) must be accepted and the override + * applied to the response. + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class S3ResponseHeaderOverrideIntegrationTest { + + private static final String BUCKET = "response-override-test-bucket"; + private static final String KEY = "test-object.txt"; + + private static final String AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/s3/aws4_request"; + + @Test + @Order(1) + void setup_createBucketAndObject() { + given().put("/" + BUCKET).then().statusCode(200); + given() + .header("Authorization", AUTH_HEADER) + .contentType("text/plain") + .body("hello") + .put("/" + BUCKET + "/" + KEY) + .then().statusCode(200); + } + + // ── Anonymous requests must be rejected (400 InvalidRequest) ───────────── + + @Test + @Order(2) + void anonymousGetWithResponseCacheControl_returns400() { + given() + .queryParam("response-cache-control", "no-cache") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(400) + .body(containsString("InvalidRequest")) + .body(containsString("anonymous GET requests")); + } + + @Test + @Order(3) + void anonymousGetWithResponseContentType_returns400() { + given() + .queryParam("response-content-type", "text/html") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(400) + .body(containsString("InvalidRequest")); + } + + @Test + @Order(4) + void anonymousGetWithResponseContentDisposition_returns400() { + given() + .queryParam("response-content-disposition", "attachment; filename=x.txt") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(400) + .body(containsString("InvalidRequest")); + } + + @Test + @Order(5) + void anonymousGetWithResponseContentEncoding_returns400() { + given() + .queryParam("response-content-encoding", "gzip") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(400) + .body(containsString("InvalidRequest")); + } + + @Test + @Order(6) + void anonymousGetWithResponseContentLanguage_returns400() { + given() + .queryParam("response-content-language", "en-US") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(400) + .body(containsString("InvalidRequest")); + } + + @Test + @Order(7) + void anonymousGetWithResponseExpires_returns400() { + given() + .queryParam("response-expires", "Thu, 01 Jan 2099 00:00:00 GMT") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(400) + .body(containsString("InvalidRequest")); + } + + @Test + @Order(8) + void anonymousHeadWithResponseCacheControl_returns400() { + given() + .queryParam("response-cache-control", "no-cache") + .when() + .head("/" + BUCKET + "/" + KEY) + .then() + .statusCode(400); + } + + // ── Signed requests must be accepted and the override applied ──────────── + + @Test + @Order(9) + void signedGetWithResponseCacheControl_appliesOverride() { + given() + .header("Authorization", AUTH_HEADER) + .queryParam("response-cache-control", "max-age=3600") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(200) + .header("Cache-Control", equalTo("max-age=3600")); + } + + @Test + @Order(10) + void signedGetWithResponseContentType_appliesOverride() { + given() + .header("Authorization", AUTH_HEADER) + .queryParam("response-content-type", "application/octet-stream") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(200) + .header("Content-Type", containsString("application/octet-stream")); + } + + @Test + @Order(11) + void signedGetWithResponseContentDisposition_appliesOverride() { + given() + .header("Authorization", AUTH_HEADER) + .queryParam("response-content-disposition", "attachment; filename=download.txt") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(200) + .header("Content-Disposition", equalTo("attachment; filename=download.txt")); + } + + // ── Anonymous request without response-* params must still work ────────── + + @Test + @Order(12) + void anonymousGetWithoutOverrides_returns200() { + given() + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(200); + } + + // ── Empty response-* values are ignored (not treated as present) ───────── + + @Test + @Order(13) + void anonymousGetWithEmptyResponseCacheControl_returns200() { + // Real S3 ignores ?response-cache-control= (empty value) — should not reject. + given() + .queryParam("response-cache-control", "") + .when() + .get("/" + BUCKET + "/" + KEY) + .then() + .statusCode(200); + } +}