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

@@ -70,10 +70,10 @@ class ByteRangeTest {
}
@Test
void anythingButOneValidRangeIsInvalid() {
void anythingButOneValidRangeIsTheWholeObject() {
for (String header : List.of("bytes=5-2", "bytes=0-1,4-5", "bytes=abc", "bytes=-", "bytes=1",
"bytes=--5", "bytes=+1-2", "bytes=")) {
assertOutcome(Outcome.INVALID, header, 100);
"bytes=--5", "bytes=+1-2", "bytes=", "bytes=-1-", "bytes=0--1")) {
assertOutcome(Outcome.WHOLE, header, 100);
}
}
@@ -1112,6 +1112,16 @@ class S3IntegrationTest {
.statusCode(200)
.header("Content-Length", equalTo("20"))
.header("x-amz-checksum-crc64nvme", notNullValue());
given()
.header("Range", "bytes=0-")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.head("/head-range-bucket/greeting.txt")
.then()
.statusCode(200)
.header("Content-Length", equalTo("20"))
.header("x-amz-checksum-crc64nvme", notNullValue())
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"));
given()
.header("Range", "bytes=50-")
.when()
@@ -1188,54 +1198,81 @@ class S3IntegrationTest {
.body(hasXPath("/Error/Message", equalTo("The requested range is not satisfiable")))
.body(hasXPath("/Error/RangeRequested", equalTo("bytes=50-100")))
.body(hasXPath("/Error/ActualObjectSize", equalTo("20")));
// A suffix of no bytes cannot be satisfied either.
given()
.header("Range", "bytes=-0")
.when()
.get("/test-bucket/greeting.txt")
.then()
.statusCode(416)
.header("Content-Range", equalTo("bytes */20"))
.body(hasXPath("/Error/Code", equalTo("InvalidRange")))
.body(hasXPath("/Error/RangeRequested", equalTo("bytes=-0")));
}
@Test
@Order(31)
void getObjectWithMalformedRangeNoDash() {
given()
.header("Range", "bytes=0")
.when()
.get("/test-bucket/greeting.txt")
.then()
.statusCode(416)
.body(containsString("InvalidRange"));
void getObjectIgnoresARangeThatIsNotOneValidRange() {
// S3 answers each of these as a GetObject without a Range, with the whole object and 200.
for (String invalid : List.of("bytes=0", "bytes=-", "bytes=abc-def", "bytes=-1-4", "bytes=1-0",
"bytes=-1-", "bytes=0--1", "bytes=0-1,3-4,7-9", "0-1")) {
given()
.header("Range", invalid)
.when()
.get("/test-bucket/greeting.txt")
.then()
.statusCode(200)
.header("Content-Length", equalTo("20"))
.header("Content-Range", nullValue())
.header("Accept-Ranges", equalTo("bytes"))
.body(equalTo("Hello World from S3!"));
}
}
@Test
@Order(32)
void getObjectWithMalformedRangeEmptySuffix() {
given()
.header("Range", "bytes=-")
void getObjectRangeCoveringTheWholeObjectCarriesItsChecksum() {
String wholeChecksum = given()
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/test-bucket/greeting.txt")
.then()
.statusCode(416)
.body(containsString("InvalidRange"));
}
.statusCode(200)
.extract().header("x-amz-checksum-crc64nvme");
assertNotNull(wholeChecksum);
@Test
@Order(33)
void getObjectWithMalformedRangeNonNumeric() {
// The whole of an object stored in one request is its one part, so a range covering it gets
// the object's checksum, as a range aligned with a single part gets that part's.
for (String whole : List.of("bytes=0-19", "bytes=0-100", "bytes=-50", "bytes=0-")) {
given()
.header("Range", whole)
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/test-bucket/greeting.txt")
.then()
.statusCode(206)
.header("Content-Range", equalTo("bytes 0-19/20"))
.header("x-amz-checksum-crc64nvme", equalTo(wholeChecksum))
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"))
.body(equalTo("Hello World from S3!"));
}
given()
.header("Range", "bytes=abc-def")
.header("Range", "bytes=0-19")
.when()
.get("/test-bucket/greeting.txt")
.then()
.statusCode(416)
.body(containsString("InvalidRange"));
}
@Test
@Order(34)
void getObjectWithMalformedRangeNegativeStart() {
.statusCode(206)
.header("x-amz-checksum-crc64nvme", nullValue());
given()
.header("Range", "bytes=-1-4")
.header("Range", "bytes=0-18")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/test-bucket/greeting.txt")
.then()
.statusCode(416)
.body(containsString("InvalidRange"));
.statusCode(206)
.header("x-amz-checksum-crc64nvme", nullValue())
.header("x-amz-checksum-type", nullValue());
}
@Test
@@ -163,6 +163,27 @@ class S3MultipartIntegrationTest {
.statusCode(200)
.header("x-amz-checksum-sha256", equalTo(COMPOSITE_SHA256))
.header("x-amz-checksum-type", equalTo("COMPOSITE"));
// The composite checksum is not the checksum of any one part, so not even a range covering
// the whole object gets it.
given()
.header("Range", "bytes=0-")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/" + BUCKET + "/" + KEY)
.then()
.statusCode(206)
.header("x-amz-checksum-sha256", nullValue())
.header("x-amz-checksum-type", nullValue())
.body(equalTo(PART_1 + PART_2));
given()
.header("Range", "bytes=0-")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.head("/" + BUCKET + "/" + KEY)
.then()
.statusCode(200)
.header("x-amz-checksum-sha256", nullValue());
}
@Test
@@ -1030,6 +1051,16 @@ class S3MultipartIntegrationTest {
.statusCode(200)
.header("x-amz-checksum-crc64nvme", equalTo(fullObjectCrc64))
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"));
// A range covering an object of two parts is aligned with neither, so it gets no checksum.
given()
.header("Range", "bytes=0-")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(206)
.header("x-amz-checksum-crc64nvme", nullValue());
}
@Test
@@ -1410,6 +1441,56 @@ class S3MultipartIntegrationTest {
.statusCode(204);
}
@Test
@Order(39)
void aRangeCoveringAnUploadCompletedFromOnePartCarriesItsFullObjectChecksum() {
String key = "one-part-multipart.bin";
completeOnePartUpload(key, "CRC64NVME", "");
// The object is its one part, so a range covering it is aligned with that part, whose checksum
// the full-object checksum is.
String partCrc64 = S3Checksum.crc64NvmeBase64(PART_1.getBytes(StandardCharsets.UTF_8));
given()
.header("Range", "bytes=0-")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(206)
.header("x-amz-checksum-crc64nvme", equalTo(partCrc64))
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"))
.body(equalTo(PART_1));
given()
.header("Range", "bytes=0-")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.head("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.header("x-amz-checksum-crc64nvme", equalTo(partCrc64));
given()
.header("Range", "bytes=1-")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(206)
.header("x-amz-checksum-crc64nvme", nullValue());
// A composite checksum of one part is not that part's checksum, so its range gets none.
completeOnePartUpload("one-part-composite.bin", "SHA256",
"<ChecksumSHA256>" + sha256Base64(PART_1) + "</ChecksumSHA256>");
given()
.header("Range", "bytes=0-")
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/" + BUCKET + "/one-part-composite.bin")
.then()
.statusCode(206)
.header("x-amz-checksum-sha256", nullValue())
.header("x-amz-checksum-type", nullValue());
}
@Test
@Order(40)
void cleanUp() {
@@ -1428,6 +1509,8 @@ class S3MultipartIntegrationTest {
given().when().delete("/" + BUCKET + "/checksum-type-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/completion-validation.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/range-source.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/one-part-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/one-part-composite.bin").then().statusCode(204);
given().when().delete("/" + BUCKET).then().statusCode(204);
}
@@ -1442,6 +1525,26 @@ class S3MultipartIntegrationTest {
return S3Checksum.sha256Base64(data.getBytes(StandardCharsets.UTF_8));
}
private static void completeOnePartUpload(String key, String checksumAlgorithm, String checksumElement) {
String newUploadId = given()
.header("x-amz-checksum-algorithm", checksumAlgorithm)
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
String partETag = given().body(PART_1).when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
.then().statusCode(200).extract().header("ETag");
given()
.contentType("application/xml")
.body(singlePartCompleteXml(partETag, checksumElement))
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(200);
}
private static String compositeSha256(String... parts) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");