mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(s3): reject response-* overrides on unsigned requests (#1098)
* 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).
This commit is contained in:
1 parent
c7a4ef37f1
commit
9b7f8e40f0
3 files changed
+226
-1
No files matched your search
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user