From f2a37c879237d9f85b3b2fc7330b1bd5610b027c 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 11:29:58 +0900 Subject: [PATCH] fix(s3): refuse a malformed or empty versionId with 400 InvalidArgument (#7164) * fix(s3): refuse a malformed or empty versionId with 400 InvalidArgument S3 refuses a version id it did not mint with 400 InvalidArgument, also on a missing key, and an empty versionId with its own message. Floci mints every version id as a UUID, so it refuses any other value on the object operations that take a version id. * docs(s3): say an unknown UUID version id is handled as a missing version --- docs/services/s3.md | 11 ++- .../floci/services/s3/S3Controller.java | 31 ++++++++ .../s3/S3InvalidVersionIdIntegrationTest.java | 71 +++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/s3/S3InvalidVersionIdIntegrationTest.java diff --git a/docs/services/s3.md b/docs/services/s3.md index 5997de394a..3521841c46 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -87,7 +87,7 @@ specific object version through the four `?annotation` operations. Notes on the governance retention requires `x-amz-bypass-governance-retention` (put never takes the bypass), compliance and legal hold always block. - The literal `versionId=null` names the null version only, as it does on every object operation - (see [Object Versions](#object-versions)); a key without one answers `NoSuchKey`, as for any + (see [Object Versions](#object-versions)); a key without one answers `NoSuchVersion`, as for any missing version. - Annotations are stored per AWS account. With `globalBucketNamespace` enabled, object reads resolve cross-account but annotation reads, writes, and listings stay in the caller's account. @@ -98,10 +98,17 @@ specific object version through the four `?annotation` operations. Notes on the A request that names `versionId=null`, the id ListObjectVersions reports for an object written while versioning was off or suspended, acts on that null version only: GetObject, HeadObject, GetObjectAttributes, the object ACL, tagging, retention, legal hold, annotations and a copy source. -A key with no null version answers as for any missing version: `404 NoSuchVersion`, or `404 NoSuchKey` for the annotation operations. If +A key with no null version answers `404 NoSuchVersion`, as for any missing version. If 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. +Floci mints every version id as a UUID. On GetObject, HeadObject, DeleteObject, GetObjectAttributes, +RestoreObject, the object ACL, tagging, retention, legal hold and annotations, a `versionId` that is +neither a UUID nor `null` answers `400 InvalidArgument`, "Invalid version id specified", as S3 +answers an id it did not mint. An empty `versionId` answers "Version id cannot be +the empty string". A UUID that Floci did not mint is handled as the id of a version that does not +exist, where S3 answers `400`. + 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 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 4145729d6b..abcfa74b55 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 @@ -89,6 +89,7 @@ import java.util.StringJoiner; import java.util.UUID; import java.util.function.Function; import java.util.function.Predicate; +import java.util.regex.Pattern; import java.util.stream.Stream; import static io.github.hectorvent.floci.services.s3.S3RequestParser.hasQueryParam; @@ -114,6 +115,9 @@ public class S3Controller { /** The errors S3Service answers a read of an object that is not there with. */ private static final Set MISSING_OBJECT_ERRORS = Set.of("NoSuchBucket", "NoSuchKey", "NoSuchVersion"); + private static final Pattern MINTED_VERSION_ID = + Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); + private static final Logger LOG = Logger.getLogger(S3Controller.class); private static final DateTimeFormatter ISO_FORMAT = DateTimeFormatter .ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'") @@ -857,12 +861,14 @@ public class S3Controller { // body sets. String versionId = uriInfo.getQueryParameters().getFirst("versionId"); if (hasQueryParam(uriInfo, "tagging")) { + checkVersionId(bucket, uriInfo); byte[] tags = readWholeBody(body); s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:PutObjectTagging", authorization, S3ObjectActions.taggingBodyConditions(new String(tags, StandardCharsets.UTF_8))); return handlePutObjectTagging(bucket, key, versionId, tags); } if (hasQueryParam(uriInfo, "retention")) { + checkVersionId(bucket, uriInfo); String retention = new String(readWholeBody(body), StandardCharsets.UTF_8); s3Service.authorizeObjectWrite(bucket, key, null, "s3:PutObjectRetention", authorization, S3ObjectActions.retentionBodyConditions(retention, clock.instant())); @@ -877,6 +883,7 @@ public class S3Controller { return handlePutObjectRetention(bucket, key, versionId, retention, effectiveBypass(bypass, overrides)); } if (hasQueryParam(uriInfo, "legal-hold")) { + checkVersionId(bucket, uriInfo); byte[] legalHold = readWholeBody(body); s3Service.authorizeObjectWrite(bucket, key, null, "s3:PutObjectLegalHold", authorization, S3ObjectActions.legalHoldBodyConditions( @@ -884,6 +891,7 @@ public class S3Controller { return handlePutObjectLegalHold(bucket, key, versionId, legalHold); } if (hasQueryParam(uriInfo, "acl")) { + checkVersionId(bucket, uriInfo); s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:PutObjectAcl", authorization, null); s3Service.putObjectAcl(bucket, key, versionId, new String(readWholeBody(body), StandardCharsets.UTF_8), @@ -897,6 +905,7 @@ public class S3Controller { } if (hasQueryParam(uriInfo, "annotation")) { + checkVersionId(bucket, uriInfo); s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:PutObjectAnnotation", authorization, null); return handlePutObjectAnnotation(bucket, key, readWholeBody(body), uriInfo, httpHeaders); } @@ -1037,6 +1046,7 @@ public class S3Controller { return handleListParts(bucket, key, uploadId, maxPartsQuery, partNumberMarkerQuery); } + checkVersionId(bucket, uriInfo); if (hasQueryParam(uriInfo, "tagging")) { s3Service.authorizeObjectRead(bucket, key, versionId, "s3:GetObjectTagging", authorization); return handleGetObjectTagging(bucket, key, versionId); @@ -1382,6 +1392,7 @@ public class S3Controller { return headOnlyResponse(websiteResponse); } } + checkVersionId(bucket, uriInfo); // HEAD honors the annotation subresource so a HEAD probe reports the same status and // annotation metadata a GET would, instead of falling through to the object's headers. if (hasQueryParam(uriInfo, "annotation")) { @@ -1542,11 +1553,13 @@ public class S3Controller { s3Service.isAuthEnforced(), httpHeaders, uriInfo); if (hasQueryParam(uriInfo, "tagging")) { + checkVersionId(bucket, uriInfo); s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:DeleteObjectTagging", authorization, null); return withVersionId(Response.noContent(), s3Service.deleteObjectTagging(bucket, key, versionId)) .build(); } if (hasQueryParam(uriInfo, "annotation")) { + checkVersionId(bucket, uriInfo); // The user guide: "The BypassGovernanceRetention permission does not apply to annotation // operations", so the bypass header asks for no permission of its own here. boolean bypass = "true".equalsIgnoreCase( @@ -1560,6 +1573,7 @@ public class S3Controller { s3Service.abortMultipartUpload(bucket, key, uploadId); return Response.noContent().build(); } + checkVersionId(bucket, uriInfo); boolean bypass = "true".equalsIgnoreCase( httpHeaders.getHeaderString("x-amz-bypass-governance-retention")); String ifMatch = httpHeaders.getHeaderString("If-Match"); @@ -1695,6 +1709,7 @@ public class S3Controller { } if (hasQueryParam(uriInfo, "restore")) { + checkVersionId(bucket, uriInfo); s3Service.authorizeObjectWrite(bucket, key, "s3:RestoreObject", authorization); s3Service.restoreObject(bucket, key, versionId, new String(body, StandardCharsets.UTF_8)); return Response.accepted().build(); @@ -3802,6 +3817,22 @@ public class S3Controller { return response.build(); } + /** + * S3 refuses a version id it did not mint, even on a missing key, but reports a missing bucket first. + * Floci mints every version id as a UUID, so it refuses any other value. The raw query keeps an empty + * {@code versionId=}, which a {@code @QueryParam} reads as absent. + */ + private void checkVersionId(String bucket, UriInfo uriInfo) { + String versionId = uriInfo.getQueryParameters().getFirst("versionId"); + if (versionId == null || "null".equals(versionId) || MINTED_VERSION_ID.matcher(versionId).matches() + || !s3Service.bucketExists(bucket)) { + return; + } + throw new AwsException("InvalidArgument", + versionId.isEmpty() ? "Version id cannot be the empty string" : "Invalid version id specified", + 400, Map.of("ArgumentName", "versionId", "ArgumentValue", versionId)); + } + /** 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; diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3InvalidVersionIdIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3InvalidVersionIdIntegrationTest.java new file mode 100644 index 0000000000..8eebca86a9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3InvalidVersionIdIntegrationTest.java @@ -0,0 +1,71 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class S3InvalidVersionIdIntegrationTest { + private static final String BUCKET = "invalid-version-id-it"; + private static final String KEY = "dir/k.txt"; + private static final String PATH = "/" + BUCKET + "/" + KEY; + + @Test + void aVersionIdFlociDidNotMintIsRefused() { + given().when().put("/" + BUCKET).then().statusCode(200); + given().body("Enabled") + .when().put("/" + BUCKET + "?versioning").then().statusCode(200); + given().body("1").when().put(PATH).then().statusCode(200); + + for (String key : List.of(KEY, "missing")) { + String path = "/" + BUCKET + "/" + key; + for (String versionId : List.of("bogus", "")) { + String version = "versionId=" + versionId; + refused(given().when().get(path + "?" + version), versionId); + refused(given().when().delete(path + "?" + version), versionId); + refused(given().when().get(path + "?acl&" + version), versionId); + refused(given().header("x-amz-acl", "private").when().put(path + "?acl&" + version), versionId); + refused(given().header("x-amz-object-attributes", "ETag").when().get(path + "?attributes&" + version), + versionId); + refused(given().when().get(path + "?tagging&" + version), versionId); + refused(given().body("").when().put(path + "?tagging&" + version), + versionId); + refused(given().when().delete(path + "?tagging&" + version), versionId); + // S3 checks the version id before the bucket's Object Lock configuration. + refused(given().when().get(path + "?retention&" + version), versionId); + refused(given().body("GOVERNANCE" + + "2030-01-01T00:00:00Z") + .when().put(path + "?retention&" + version), versionId); + refused(given().when().get(path + "?legal-hold&" + version), versionId); + refused(given().body("OFF") + .when().put(path + "?legal-hold&" + version), versionId); + refused(given().body("1") + .when().post(path + "?restore&" + version), versionId); + refused(given().when().get(path + "?annotation&annotationName=labels&" + version), versionId); + refused(given().when().get(path + "?annotation&" + version), versionId); + refused(given().body("x").when().put(path + "?annotation&annotationName=labels&" + version), + versionId); + refused(given().when().delete(path + "?annotation&annotationName=labels&" + version), versionId); + given().when().head(path + "?" + version).then().statusCode(400); + } + } + // The refused DeleteObject left the object current. + given().when().get(PATH).then().statusCode(200).body(equalTo("1")); + given().when().get("/" + BUCKET + "-missing/" + KEY + "?versionId=bogus").then().statusCode(404) + .body("Error.Code", equalTo("NoSuchBucket")); + } + + private static void refused(Response response, String versionId) { + response.then().statusCode(400) + .body("Error.Code", equalTo("InvalidArgument")) + .body("Error.Message", equalTo(versionId.isEmpty() + ? "Version id cannot be the empty string" : "Invalid version id specified")) + .body("Error.ArgumentName", equalTo("versionId")) + .body("Error.ArgumentValue", equalTo(versionId)); + } +}