fix(s3): answer GetObject Range requests as S3 does (#5436)

GetObject answered 416 InvalidRange to any Range that was not one valid
byte range: several ranges, a last byte before the first, or a malformed
value such as bytes=-, bytes=-1- or bytes=0--1. S3 answers those as a
GetObject without a Range, with the whole object and 200, as
LocalStack's AWS-validated capture of GetObject over a dozen ranges
(test_get_object_range) records it. Only a range that starts past the
end of the object, or bytes=-0, gets 416. HeadObject and a CloudFront S3
origin already answered them with the whole object.

Since every caller now treats a Range that is not one valid range as no
Range at all, ByteRange.resolve reports it as WHOLE, and the separate
INVALID outcome is gone.

The same capture shows a 206 for a range covering the whole object
(bytes=-15 and bytes=0-100 of 10 bytes) carrying the object's checksum,
which Floci left out of every ranged response. The User Guide's
"Checking object integrity" has GetObject and HeadObject return a part's
checksum for "a part number or byte range that aligns with a single
part", and an object stored in one request, or completed from one part,
is that part. Such a range now gets the stored checksum, from GetObject
and HeadObject, when the request asks for checksums and the checksum is
a full-object one. A composite checksum is not that of any one part, nor
is a checksum of an object of several parts, so their ranges still get
none.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-11 09:13:03 -05:00
1 parent 013a9214b5
commit 09a2d5c005
7 files changed
+205 -51

No files matched your search

@@ -618,7 +618,7 @@ public class CloudFrontServingController {
closeUnsent(read);
yield rangeNotSatisfiable(obj.getSize());
}
case WHOLE, INVALID -> new OriginResponse(
case WHOLE -> new OriginResponse(
200, contentType(obj), null, read.body(), obj.getSize(), s3ObjectHeaders(obj));
};
}
@@ -634,7 +634,7 @@ public class CloudFrontServingController {
case PART -> new OriginResponse(
200, contentType(meta), null, resolution.range().length(), s3ObjectHeaders(meta));
case NOT_SATISFIABLE -> rangeNotSatisfiable(meta.getSize());
case WHOLE, INVALID -> new OriginResponse(
case WHOLE -> new OriginResponse(
200, contentType(meta), null, meta.getSize(), s3ObjectHeaders(meta));
};
}
@@ -25,11 +25,12 @@ public record ByteRange(long first, long last) {
private static final Pattern SPEC = Pattern.compile("(\\d*)-(\\d*)");
/**
* What a {@code Range} header asks of an object: a part of it, the whole of it (no header, a
* unit other than bytes, or a suffix of an empty object), a range that starts past its end, or
* something that is not one valid range, such as several ranges or a last byte before the first.
* What a {@code Range} header asks of an object: a part of it, the whole of it, or a range that
* starts past its end. The whole object is asked for by no header, a unit other than bytes, a
* suffix of an empty object, or anything that is not one valid range, such as several ranges or
* a last byte before the first, which S3 answers as a request without a {@code Range}.
*/
public enum Outcome { PART, WHOLE, NOT_SATISFIABLE, INVALID }
public enum Outcome { PART, WHOLE, NOT_SATISFIABLE }
/** A resolved {@code Range} header; {@code range} is set only for {@link Outcome#PART}. */
public record Resolution(Outcome outcome, ByteRange range) {
@@ -102,7 +103,7 @@ public record ByteRange(long first, long last) {
}
Matcher matcher = SPEC.matcher(header.substring(UNIT.length()).trim());
if (!matcher.matches() || (matcher.group(1).isEmpty() && matcher.group(2).isEmpty())) {
return new Resolution(Outcome.INVALID, null);
return new Resolution(Outcome.WHOLE, null);
}
long first;
long last;
@@ -120,7 +121,7 @@ public record ByteRange(long first, long last) {
first = position(matcher.group(1));
last = matcher.group(2).isEmpty() ? Long.MAX_VALUE : position(matcher.group(2));
if (last < first) {
return new Resolution(Outcome.INVALID, null);
return new Resolution(Outcome.WHOLE, null);
}
last = Math.min(last, size - 1);
}
@@ -1126,7 +1126,7 @@ public class S3Controller {
emitCloudTrailEvent("GetObject", bucket, key, 0L, totalSize, null, null);
return fullObjectResponse(obj, body, overrides, includeChecksum);
}
if (resolution.outcome() != ByteRange.Outcome.PART) {
if (resolution.outcome() == ByteRange.Outcome.NOT_SATISFIABLE) {
emitCloudTrailEvent("GetObject", bucket, key, 0L, 0L,
"InvalidRange", ByteRange.NOT_SATISFIABLE_MESSAGE);
return invalidRangeResponse(rangeHeader, totalSize);
@@ -1150,11 +1150,24 @@ public class S3Controller {
if (obj.getVersionId() != null) {
resp.header("x-amz-version-id", obj.getVersionId());
}
// includeChecksum=false: the stored checksum covers the whole object, not this range.
appendObjectHeaders(resp, obj, overrides, false);
appendObjectHeaders(resp, obj, overrides, includeChecksum && rangeCarriesObjectChecksum(range, obj));
return resp.build();
}
/**
* Whether the stored checksum is the checksum of {@code range}: S3 returns a part's checksum for a
* range aligned with a single part, and the whole of an object stored in one request, or completed
* from one part, is that part, whose checksum a full-object checksum of the object is. A composite
* checksum is not that of any one part, nor is a checksum of an object of several parts, so their
* ranges get none.
*/
private static boolean rangeCarriesObjectChecksum(ByteRange range, S3Object obj) {
S3Checksum checksum = obj.getChecksum();
return range.first() == 0 && range.last() == obj.getSize() - 1
&& (obj.getParts() == null || obj.getParts().size() <= 1)
&& checksum != null && checksum.getChecksumType() != ChecksumType.COMPOSITE;
}
private Response websiteObjectResponse(S3Service.ObjectRead read, ResponseHeaderOverrides overrides) {
return fullObjectResponse(read.object(), read.body(), overrides, false);
}
@@ -1249,14 +1262,14 @@ public class S3Controller {
// "If the Range is satisfiable, only the ContentLength is affected in the response. If the
// Range is not satisfiable, S3 returns a 416." A Range that is not one valid range is ignored.
long contentLength = obj.getSize();
boolean ranged = false;
boolean checksumDescribesRange = true;
String rangeHeader = httpHeaders.getHeaderString("Range");
if (ByteRange.inBytes(rangeHeader) && ByteRange.rangeApplies(
httpHeaders.getHeaderString("If-Range"), obj.getETag(), obj.getLastModified())) {
ByteRange.Resolution resolution = ByteRange.resolve(rangeHeader, obj.getSize());
if (resolution.outcome() == ByteRange.Outcome.PART) {
contentLength = resolution.range().length();
ranged = true;
checksumDescribesRange = rangeCarriesObjectChecksum(resolution.range(), obj);
} else if (resolution.outcome() == ByteRange.Outcome.NOT_SATISFIABLE) {
emitCloudTrailEvent("HeadObject", bucket, key, 0L, 0L,
"InvalidRange", ByteRange.NOT_SATISFIABLE_MESSAGE);
@@ -1272,8 +1285,8 @@ public class S3Controller {
if (obj.getVersionId() != null) {
resp.header("x-amz-version-id", obj.getVersionId());
}
// As on a ranged GetObject, the stored checksum covers the whole object, not the range.
boolean includeChecksum = !ranged
// As on a ranged GetObject, the stored checksum is sent only for a range it describes.
boolean includeChecksum = checksumDescribesRange
&& "ENABLED".equalsIgnoreCase(resolveHeaderOrQueryParam(checksumMode, uriInfo, "x-amz-checksum-mode"));
appendObjectHeaders(resp, obj, overrides, includeChecksum);
emitCloudTrailEvent("HeadObject", bucket, key, 0L, obj.getSize(), null, null);
@@ -2720,9 +2733,9 @@ public class S3Controller {
appendLockHeaders(resp, obj);
}
// 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.
// includeChecksum must be false for a range the stored checksum does not describe:
// 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 there.
private void appendObjectHeaders(Response.ResponseBuilder resp, S3Object obj, ResponseHeaderOverrides overrides,
boolean includeChecksum) {
if (obj.getStorageClass() != null) {