fix(s3): omit checksum headers from GetObject/HeadObject unless x-amz-checksum-mode: ENABLED (#1633)

* fix(s3): omit checksum headers from GetObject/HeadObject unless x-amz-checksum-mode: ENABLED

Real AWS only returns x-amz-checksum-* headers on GetObject and HeadObject
responses when the client sends x-amz-checksum-mode: ENABLED. Without that
header, returning the checksum causes the PHP AWS SDK to attempt checksum
validation on a non-seekable streaming response body, throwing
"Stream is not seekable" and making fopen fail.

Closes #1632

* refactor(s3): drop dead appendObjectHeaders overloads, dedupe test @Order

Addresses Greptile review on PR #1633:

- Remove the now-unreachable 2-arg and 3-arg appendObjectHeaders overloads.
  All real callers pass includeChecksum explicitly to the 4-arg version, so the
  3-arg overload's hard-coded `true` was a footgun that could silently re-emit
  checksum headers. Also drops the now-orphaned ResponseHeaderOverrides.NONE.
- Move getObjectWithChecksumModeReturnsChecksum / headObjectWithChecksumMode...
  to the end of the 5-22 test block with unique @Order(23)/@Order(24), so they
  no longer share @Order(5)/@Order(7) with getObject/headObject.

All 117 S3IntegrationTest cases pass.

* fix(s3): include checksum in empty-object suffix-range response when requested

The empty-object suffix-range path returns a full 200 via fullObjectResponse
instead of a 206, so it must honor x-amz-checksum-mode: ENABLED like any
other full-object response. It previously hardcoded includeChecksum=false,
omitting the checksum even when the client asked for it.

Also extends the existing suffix-range test to assert the checksum is
omitted without the header, and adds a new test covering the
x-amz-checksum-mode: ENABLED case.

* fix(s3): resolve rebase-onto-main test conflicts for checksum-mode gate

Sends x-amz-checksum-mode: ENABLED on the GetObject verification calls
that require checksum headers in the response (copyObject,
copyObjectWithoutChecksumOverride, copyObjectPreservesNonDefaultChecksum),
consistent with the behavioral fix in this branch. Adds
getObjectWithChecksumModeReturnsChecksum and
headObjectWithChecksumModeReturnsChecksum as self-contained tests with
unique, non-colliding @Order values.

---------

Co-authored-by: Mihai Blidaru <mihai.blidaru@lynxfctech.com>
This commit is contained in:
Mihai BlidaruandMihai Blidaru authored and GitHub committed 2026-07-10 23:12:43 -05:00
1 parent cd7370c730
commit 48f310b63f
2 files changed
+106 -19

No files matched your search

@@ -638,6 +638,7 @@ public class S3Controller {
@HeaderParam("If-Modified-Since") String ifModifiedSince,
@HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince,
@HeaderParam("Range") String rangeHeader,
@HeaderParam("x-amz-checksum-mode") String checksumMode,
@Context UriInfo uriInfo,
@Context HttpHeaders httpHeaders) {
S3Service.RequestAuthorization authorization = S3Service.RequestAuthorization.unsigned();
@@ -698,11 +699,12 @@ public class S3Controller {
"Request specific response headers cannot be used for anonymous GET requests.", 400));
}
boolean includeChecksum = "ENABLED".equalsIgnoreCase(checksumMode);
if (rangeHeader != null && rangeHeader.startsWith("bytes=")) {
return handleRangeRequest(bucket, key, versionId, obj, rangeHeader, overrides);
return handleRangeRequest(bucket, key, versionId, obj, rangeHeader, overrides, includeChecksum);
}
return fullObjectResponse(bucket, key, versionId, obj, overrides);
return fullObjectResponse(bucket, key, versionId, obj, overrides, includeChecksum);
} catch (AwsException e) {
if (isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders)) {
try {
@@ -722,7 +724,8 @@ public class S3Controller {
}
private Response fullObjectResponse(String bucket, String key, String versionId,
S3Object obj, ResponseHeaderOverrides overrides) {
S3Object obj, ResponseHeaderOverrides overrides,
boolean includeChecksum) {
StreamingOutput stream = output -> {
try (InputStream input = s3Service.openObjectStream(bucket, key, versionId)) {
input.transferTo(output);
@@ -737,13 +740,14 @@ public class S3Controller {
if (obj.getVersionId() != null) {
resp.header("x-amz-version-id", obj.getVersionId());
}
appendObjectHeaders(resp, obj, overrides);
appendObjectHeaders(resp, obj, overrides, includeChecksum);
return resp.build();
}
private Response handleRangeRequest(String bucket, String key, String versionId,
S3Object obj, String rangeHeader,
ResponseHeaderOverrides overrides) {
ResponseHeaderOverrides overrides,
boolean includeChecksum) {
long totalSize = obj.getSize();
String rangeSpec = rangeHeader.substring("bytes=".length()).trim();
@@ -775,7 +779,7 @@ public class S3Controller {
if (start < 0 || start >= totalSize || start > end) {
if (totalSize == 0 && rangeSpec.startsWith("-")) {
return fullObjectResponse(bucket, key, versionId, obj, overrides);
return fullObjectResponse(bucket, key, versionId, obj, overrides, includeChecksum);
}
return invalidRangeResponse(totalSize);
}
@@ -848,6 +852,7 @@ public class S3Controller {
@HeaderParam("If-None-Match") String ifNoneMatch,
@HeaderParam("If-Modified-Since") String ifModifiedSince,
@HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince,
@HeaderParam("x-amz-checksum-mode") String checksumMode,
@Context UriInfo uriInfo,
@Context HttpHeaders httpHeaders) {
try {
@@ -882,7 +887,8 @@ public class S3Controller {
if (obj.getVersionId() != null) {
resp.header("x-amz-version-id", obj.getVersionId());
}
appendObjectHeaders(resp, obj, overrides);
boolean includeChecksum = "ENABLED".equalsIgnoreCase(checksumMode);
appendObjectHeaders(resp, obj, overrides, includeChecksum);
return resp.build();
} catch (AwsException e) {
return xmlErrorResponse(e);
@@ -1696,8 +1702,6 @@ public class S3Controller {
String cacheControl,
String contentDisposition,
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;
@@ -1718,10 +1722,6 @@ public class S3Controller {
}
}
private void appendObjectHeaders(Response.ResponseBuilder resp, S3Object obj) {
appendObjectHeaders(resp, obj, ResponseHeaderOverrides.NONE);
}
// PutObject's response body is empty — body-describing headers and x-amz-meta-*
// must not be emitted, or SDK clients will try to decompress and fail.
private void appendPutObjectResponseHeaders(Response.ResponseBuilder resp, S3Object obj) {
@@ -1736,10 +1736,6 @@ public class S3Controller {
appendLockHeaders(resp, obj);
}
private void appendObjectHeaders(Response.ResponseBuilder resp, S3Object obj, ResponseHeaderOverrides overrides) {
appendObjectHeaders(resp, obj, overrides, true);
}
// includeChecksum must be false for partial (206) responses: obj.getChecksum() is the
// whole-object checksum, which does not match the range bytes returned. SDKs that
// validate it against the received body fail. Real S3 omits it on ranged responses.