From 0f944404425e7a85d742074d23fcb27f5ae5bf9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Sun, 1 Nov 2026 09:02:17 +0900 Subject: [PATCH] fix(s3): name delete markers in GET and HEAD errors and the key in NoSuchKey (#7149) * fix(s3): answer a GET or HEAD that reaches a delete marker as S3 does A read of a key whose current version is a delete marker sends the marker's headers with the 404, and a versionId that names a delete marker is 405 MethodNotAllowed, as on S3. * fix(s3): name the key in NoSuchKey errors Every NoSuchKey error names the key in a Key element, and GetObjectTagging names it with its bucket, as on S3. --- docs/services/s3.md | 5 + .../floci/services/s3/S3Controller.java | 37 ++++-- .../services/s3/S3DeleteMarkerException.java | 40 +++++++ .../floci/services/s3/S3Service.java | 20 ++-- .../s3/S3DeleteMarkerReadIntegrationTest.java | 110 ++++++++++++++++++ .../s3/S3NoSuchKeyIntegrationTest.java | 36 ++++++ 6 files changed, 233 insertions(+), 15 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/s3/S3DeleteMarkerException.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteMarkerReadIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/s3/S3NoSuchKeyIntegrationTest.java diff --git a/docs/services/s3.md b/docs/services/s3.md index 9351c73408..5997de394a 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -102,6 +102,11 @@ A key with no null version answers as for any missing version: `404 NoSuchVersio the key already has a null version, a later versioned write or delete marker keeps it as a noncurrent version. Deleting newer versions can make that null version current again. +GetObject and HeadObject of a key whose current version is a delete marker answer `404` with +`x-amz-delete-marker: true` and the marker's id in `x-amz-version-id`, as on S3. A `versionId` that +names a delete marker answers `405 MethodNotAllowed` with the same headers, the marker's +`Last-Modified` and `Allow: DELETE`. + `DeleteObject` without a version id in a versioning-suspended bucket makes a `null` delete marker the current version, as on S3. It replaces the null version or an earlier `null` delete marker, and the other versions stay as noncurrent versions. Deleting `versionId=null` removes that marker and 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 2b28134c3b..6dcbc64c6d 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 @@ -1137,7 +1137,8 @@ public class S3Controller { closeQuietly(read); } } - } catch (AwsException e) { + } catch (AwsException caught) { + AwsException e = deleteMarkerRefusal(caught, versionId, "GET"); emitCloudTrailEvent("GetObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage()); if (S3Service.isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders, uriInfo)) { Response websiteError = serveWebsiteErrorResponse(bucket, authorization, e); @@ -1145,7 +1146,7 @@ public class S3Controller { return websiteError; } } - return xmlErrorResponse(e, bucket); + return withDeleteMarkerHeaders(xmlErrorResponse(e, bucket), e); } } @@ -1442,7 +1443,8 @@ public class S3Controller { checksumDescribesRange && checksumMode); emitCloudTrailEvent("HeadObject", bucket, key, 0L, obj.getSize(), null, null); return resp.build(); - } catch (AwsException e) { + } catch (AwsException caught) { + AwsException e = deleteMarkerRefusal(caught, versionId, "HEAD"); emitCloudTrailEvent("HeadObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage()); if (S3Service.isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders, uriInfo)) { Response websiteError = serveWebsiteErrorResponse(bucket, authorization, e); @@ -1450,7 +1452,7 @@ public class S3Controller { return headOnlyResponse(websiteError); } } - return xmlErrorResponse(e, bucket); + return withDeleteMarkerHeaders(xmlErrorResponse(e, bucket), e); } } @@ -3749,11 +3751,11 @@ public class S3Controller { } // S3 InvalidArgument responses carry ArgumentName and ArgumentValue so the SDK can // surface which input was rejected, and EntityTooSmall and EntityTooLarge the proposed and - // allowed sizes. Multipart size errors also identify the rejected part, and MethodNotAllowed - // the method and the kind of resource that refused it. + // allowed sizes. Multipart size errors also identify the rejected part, MethodNotAllowed + // the method and the kind of resource that refused it, and NoSuchKey the key. // They travel through AwsException.extendedData. if (e.getExtendedData() != null) { - for (String field : List.of("Method", "ResourceType", "ProposedSize", "MinSizeAllowed", + for (String field : List.of("Key", "Method", "ResourceType", "ProposedSize", "MinSizeAllowed", "MaxSizeAllowed", "PartNumber", "ETag")) { Object value = e.getExtendedData().get(field); if (value != null) { @@ -3799,6 +3801,27 @@ public class S3Controller { return response.build(); } + /** A GET or HEAD that names a delete marker by its version id is refused instead of answered as a missing key. */ + private static AwsException deleteMarkerRefusal(AwsException e, String versionId, String method) { + return e instanceof S3DeleteMarkerException marker && versionId != null ? marker.named(method) : e; + } + + /** S3 sends the id of the delete marker a GET or HEAD reached, and for one the request named, its time. */ + private static Response withDeleteMarkerHeaders(Response response, AwsException e) { + if (!(e instanceof S3DeleteMarkerException marker)) { + return response; + } + Response.ResponseBuilder builder = Response.fromResponse(response) + .header("x-amz-version-id", marker.versionId()); + if (e.getHttpStatus() == 405) { + // xmlErrorResponse already sends x-amz-delete-marker for this refusal. + return builder.header("Last-Modified", RFC_822.format(marker.lastModified())) + .header("Allow", "DELETE") + .build(); + } + return builder.header("x-amz-delete-marker", "true").build(); + } + private Response checkPreconditions(S3Object obj, String ifMatch, String ifNoneMatch, String ifModifiedSince, String ifUnmodifiedSince) { String eTag = obj.getETag(); diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3DeleteMarkerException.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3DeleteMarkerException.java new file mode 100644 index 0000000000..dacc80146f --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3DeleteMarkerException.java @@ -0,0 +1,40 @@ +package io.github.hectorvent.floci.services.s3; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.s3.model.S3Object; + +import java.time.Instant; +import java.util.Map; + +/** A read that reached a delete marker, answered as a missing key unless the request named the marker. */ +final class S3DeleteMarkerException extends AwsException { + + private final String versionId; + private final Instant lastModified; + + S3DeleteMarkerException(S3Object marker) { + super("NoSuchKey", "The specified key does not exist.", 404, Map.of("Key", marker.getKey())); + this.versionId = marker.getVersionId() != null ? marker.getVersionId() : "null"; + this.lastModified = marker.getLastModified(); + } + + private S3DeleteMarkerException(S3DeleteMarkerException current, String method) { + super("MethodNotAllowed", "The specified method is not allowed against this resource.", 405, + Map.of("Method", method, "ResourceType", "DeleteMarker")); + this.versionId = current.versionId; + this.lastModified = current.lastModified; + } + + /** The refusal S3 gives a {@code method} request that names this delete marker by its version id. */ + S3DeleteMarkerException named(String method) { + return new S3DeleteMarkerException(this, method); + } + + String versionId() { + return versionId; + } + + Instant lastModified() { + return lastModified; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index bd64bfa6c4..784bfcbda6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java @@ -1926,7 +1926,7 @@ public class S3Service implements Resettable, ResourceProvider { .orElseThrow(() -> new AwsException( "NoSuchVersion", "The specified version does not exist.", 404)); if (stored.object().isDeleteMarker()) { - throw new AwsException("NoSuchKey", "The specified key does not exist.", 404); + throw new S3DeleteMarkerException(stored.object()); } S3Object object = copyObject(stored.object()); boolean archived = stored.storeKey().equals(versionedKey(bucketName, key, "null")); @@ -2223,13 +2223,17 @@ public class S3Service implements Resettable, ResourceProvider { S3Object object = findStoredObject(ownedBucket, bucketName, key, versionId) .orElseThrow(() -> versionId != null ? new AwsException("NoSuchVersion", "The specified version does not exist.", 404) - : new AwsException("NoSuchKey", "The specified key does not exist.", 404)); + : noSuchKey(key)); if (object.isDeleteMarker()) { - throw new AwsException("NoSuchKey", "The specified key does not exist.", 404); + throw new S3DeleteMarkerException(object); } return new AccountAwareStorageBackend.OwnedEntry<>(ownedBucket.account(), object); } + private static AwsException noSuchKey(String key) { + return new AwsException("NoSuchKey", "The specified key does not exist.", 404, Map.of("Key", key)); + } + /** The stored version {@code versionId} names, the current one when it is null, delete markers included. */ private Optional findStoredObject(AccountAwareStorageBackend.OwnedEntry ownedBucket, String bucketName, String key, String versionId) { @@ -2398,7 +2402,7 @@ public class S3Service implements Resettable, ResourceProvider { } S3Object current = objectStore.get(objectKey(bucketName, key)).orElse(null); if (current == null || current.isDeleteMarker()) { - throw new AwsException("NoSuchKey", "The specified key does not exist.", 404); + throw noSuchKey(key); } if (!eTagMatches(ifMatch, current.getETag())) { throw new S3PreconditionFailedException("If-Match"); @@ -3372,7 +3376,8 @@ public class S3Service implements Resettable, ResourceProvider { .map(StoredVersion::object) .orElseThrow(() -> versionId != null ? new AwsException("NoSuchVersion", "The specified version does not exist.", 404) - : new AwsException("NoSuchKey", "The specified key does not exist.", 404)); + // GetObjectTagging names the key with its bucket. + : noSuchKey("GET".equals(method) ? bucketName + "/" + key : key)); if (object.isDeleteMarker()) { throw new AwsException("MethodNotAllowed", "The specified method is not allowed against this resource.", 405, @@ -3624,10 +3629,9 @@ public class S3Service implements Resettable, ResourceProvider { private S3Object resolveParentObject(String bucketName, String key, String versionId) { S3Object object = findStoredVersion(bucketName, key, versionId) .map(StoredVersion::object) - .orElseThrow(() -> new AwsException("NoSuchKey", - "The specified key does not exist.", 404)); + .orElseThrow(() -> noSuchKey(key)); if (object.isDeleteMarker()) { - throw new AwsException("NoSuchKey", "The specified key does not exist.", 404); + throw noSuchKey(key); } return object; } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteMarkerReadIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteMarkerReadIntegrationTest.java new file mode 100644 index 0000000000..ab12d855b1 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteMarkerReadIntegrationTest.java @@ -0,0 +1,110 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Locale; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; + +@QuarkusTest +class S3DeleteMarkerReadIntegrationTest { + private static final DateTimeFormatter HTTP_DATE = DateTimeFormatter + .ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US) + .withZone(ZoneOffset.UTC); + + @Test + void aReadOfTheCurrentDeleteMarkerIsAMissingKeyThatNamesTheMarker() { + String bucket = createBucket("marker-read-current", "Enabled"); + given().body("data").when().put("/" + bucket + "/key").then().statusCode(200); + String marker = deleteKey(bucket); + + for (String method : List.of("GET", "HEAD")) { + given().when().request(method, "/" + bucket + "/key").then().statusCode(404) + .header("x-amz-delete-marker", "true") + .header("x-amz-version-id", marker) + .header("Last-Modified", nullValue()) + .header("Allow", nullValue()); + } + given().header("Range", "bytes=0-0").when().get("/" + bucket + "/key").then().statusCode(404) + .header("x-amz-delete-marker", "true") + .header("x-amz-version-id", marker) + .body("Error.Key", equalTo("key")); + given().header("If-Match", "\"stale\"").when().get("/" + bucket + "/key").then().statusCode(404) + .header("x-amz-delete-marker", "true") + .header("x-amz-version-id", marker); + } + + @Test + void aReadThatNamesADeleteMarkerIsRefused() { + String bucket = createBucket("marker-read-named", "Enabled"); + given().body("data").when().put("/" + bucket + "/key").then().statusCode(200); + String marker = deleteKey(bucket); + String lastModified = markerLastModified(bucket); + + assertRefused(bucket, marker, lastModified); + given().body("again").when().put("/" + bucket + "/key").then().statusCode(200); + assertRefused(bucket, marker, lastModified); + } + + @Test + void theNullDeleteMarkerOfASuspendedBucketIsNamedNull() { + String bucket = createBucket("marker-read-null", "Suspended"); + given().body("data").when().put("/" + bucket + "/key").then().statusCode(200); + deleteKey(bucket); + + given().when().get("/" + bucket + "/key").then().statusCode(404) + .header("x-amz-delete-marker", "true") + .header("x-amz-version-id", "null"); + assertRefused(bucket, "null", markerLastModified(bucket)); + } + + private static void assertRefused(String bucket, String versionId, String lastModified) { + String path = "/" + bucket + "/key?versionId=" + versionId; + given().when().get(path).then().statusCode(405) + .header("x-amz-delete-marker", "true") + .header("x-amz-version-id", versionId) + .header("Last-Modified", lastModified) + .header("Allow", "DELETE") + .body(allOf(containsString("MethodNotAllowed"), + containsString("GET"), + containsString("DeleteMarker"), + not(containsString("")))); + given().when().head(path).then().statusCode(405) + .header("x-amz-delete-marker", "true") + .header("x-amz-version-id", versionId) + .header("Last-Modified", lastModified) + .header("Allow", "DELETE"); + given().when().get(path + "&partNumber=1").then().statusCode(405) + .header("x-amz-version-id", versionId); + } + + private static String createBucket(String bucket, String versioning) { + given().when().put("/" + bucket).then().statusCode(200); + given().body("" + versioning + "") + .when().put("/" + bucket + "?versioning").then().statusCode(200); + return bucket; + } + + private static String deleteKey(String bucket) { + return given().when().delete("/" + bucket + "/key").then().statusCode(204) + .header("x-amz-delete-marker", "true") + .extract().header("x-amz-version-id"); + } + + private static String markerLastModified(String bucket) { + String listed = given().queryParam("versions", "").queryParam("prefix", "key").when().get("/" + bucket) + .then().statusCode(200) + .extract().xmlPath().getString("ListVersionsResult.DeleteMarker.LastModified"); + return HTTP_DATE.format(Instant.parse(listed)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3NoSuchKeyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3NoSuchKeyIntegrationTest.java new file mode 100644 index 0000000000..d523feb45a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3NoSuchKeyIntegrationTest.java @@ -0,0 +1,36 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class S3NoSuchKeyIntegrationTest { + private static final String BUCKET = "no-such-key-names-the-key"; + private static final String KEY = "dir/missing.txt"; + private static final String PATH = "/" + BUCKET + "/" + KEY; + + @Test + void noSuchKeyNamesTheKey() { + given().when().put("/" + BUCKET).then().statusCode(200); + + given().when().get(PATH).then().statusCode(404) + .body("Error.Code", equalTo("NoSuchKey")) + .body("Error.Key", equalTo(KEY)); + given().when().get(PATH + "?acl").then().statusCode(404).body("Error.Key", equalTo(KEY)); + given().header("x-amz-object-attributes", "ETag").when().get(PATH + "?attributes") + .then().statusCode(404).body("Error.Key", equalTo(KEY)); + given().body("").when().put(PATH + "?tagging") + .then().statusCode(404).body("Error.Key", equalTo(KEY)); + given().when().delete(PATH + "?tagging").then().statusCode(404).body("Error.Key", equalTo(KEY)); + given().when().get(PATH + "?annotation&annotationName=labels") + .then().statusCode(404).body("Error.Key", equalTo(KEY)); + given().header("If-Match", "*").when().delete(PATH).then().statusCode(404).body("Error.Key", equalTo(KEY)); + given().header("x-amz-copy-source", PATH).when().put("/" + BUCKET + "/copy") + .then().statusCode(404).body("Error.Key", equalTo(KEY)); + // GetObjectTagging names the key with its bucket. + given().when().get(PATH + "?tagging").then().statusCode(404).body("Error.Key", equalTo(BUCKET + "/" + KEY)); + } +}