fix(s3): honor per-object VersionId in batch DeleteObjects (#3232)

* fix(s3): honor per-object VersionId in batch DeleteObjects

DeleteObjects parsed request bodies with a flat extractAll(xml, "Key"),
discarding every <VersionId> before it reached S3Service. The batch path
then always called the 2-arg deleteObject(bucket, key), the
no-version-specified overload — so on a versioned bucket every batch
delete placed a new delete marker instead of removing the version named,
even when the request pointed at a specific version or a delete marker's
own version id. Repeated list-then-batch-delete cycles accumulate
delete markers without bound and slow down ListObjectVersions.

XmlParser gains extractDeleteObjectEntries, which pairs each <Key> with
the VersionId from the same <Object> block (absent VersionId -> null).
S3Controller.handleDeleteObjects and S3Service.deleteObjects now carry
that pairing through authorization and into the existing, already-correct
3-arg deleteObject(bucket, key, versionId) - the same code path #1045
fixed for the single-object DeleteObject API. That fix only touched the
single-object path; the batch path never reached it, which is the gap
this closes.

* fix(s3): authorize and report explicit-version batch deletes correctly

The batch DeleteObjects path introduced in the previous commit carried
each entry's VersionId through to deleteObject, but two spots still
treated every entry as an unversioned delete:

- handleDeleteObjects authorized every entry as s3:DeleteObject, even
  one naming an explicit VersionId. A bucket policy granting ordinary
  deletion but not s3:DeleteObjectVersion therefore let an unsigned
  caller permanently remove a retained version through the batch API,
  though the equivalent single-object DELETE (authorizeDeleteObject)
  already denies it. The batch loop now calls that same
  authorizeDeleteObject(bucket, key, versionId, authorization) helper
  instead of hardcoding the action, so both paths enforce identically.

- The <Deleted> entries in the response never serialized VersionId, so
  a successful explicit-version delete returned no version id to the
  caller even though S3Service.DeleteResult already carried it.
  handleDeleteObjects now emits <VersionId> when present.

Adds batchDeleteObjectsPolicyGrantDoesNotAuthorizeVersionedDelete,
mirroring deleteObjectPolicyGrantDoesNotAuthorizeVersionedDelete for the
batch endpoint, and extends S3DeleteObjectsIntegrationTest to assert the
response VersionId on explicit-version deletes and its absence on plain
key deletes.

* docs(core): remove em dash from extractDeleteObjectEntries javadoc

Replace the em dash in XmlParser.extractDeleteObjectEntries's javadoc
with a period, per the repository's prohibited-em-dash directive.
This commit is contained in:
Ricky Rattlesnake authored and GitHub committed 2026-09-08 07:27:27 -04:00
1 parent ebd849a956
commit d9cddc56cb
7 files changed
+270 -15

No files matched your search

@@ -121,6 +121,63 @@ class XmlParserTest {
assertNull(XmlParser.extractElementTree(null, "Target"));
}
// --- extractDeleteObjectEntries: batch delete Key/VersionId pairing ---
@Test
void extractDeleteObjectEntriesPairsKeyWithVersionId() {
String xml = """
<Delete>
<Object><Key>a.txt</Key><VersionId>v1</VersionId></Object>
<Object><Key>b.txt</Key><VersionId>v2</VersionId></Object>
</Delete>
""";
List<XmlParser.KeyVersion> entries = XmlParser.extractDeleteObjectEntries(xml);
assertEquals(2, entries.size());
assertEquals(new XmlParser.KeyVersion("a.txt", "v1"), entries.get(0));
assertEquals(new XmlParser.KeyVersion("b.txt", "v2"), entries.get(1));
}
@Test
void extractDeleteObjectEntriesVersionIdIsOptional() {
String xml = """
<Delete>
<Object><Key>no-version.txt</Key></Object>
</Delete>
""";
List<XmlParser.KeyVersion> entries = XmlParser.extractDeleteObjectEntries(xml);
assertEquals(1, entries.size());
assertEquals("no-version.txt", entries.get(0).key());
assertNull(entries.get(0).versionId());
}
@Test
void extractDeleteObjectEntriesMixedVersionedAndUnversioned() {
String xml = """
<Delete>
<Quiet>true</Quiet>
<Object><Key>versioned.txt</Key><VersionId>v1</VersionId></Object>
<Object><Key>plain.txt</Key></Object>
</Delete>
""";
List<XmlParser.KeyVersion> entries = XmlParser.extractDeleteObjectEntries(xml);
assertEquals(2, entries.size());
assertEquals("v1", entries.get(0).versionId());
assertNull(entries.get(1).versionId());
}
@Test
void extractDeleteObjectEntriesEmptyWhenNoObjects() {
assertTrue(XmlParser.extractDeleteObjectEntries("<Delete><Quiet>true</Quiet></Delete>").isEmpty());
assertTrue(XmlParser.extractDeleteObjectEntries(null).isEmpty());
assertTrue(XmlParser.extractDeleteObjectEntries("").isEmpty());
}
// --- extractPairsPerGroup ---
@Test
@@ -43,6 +43,7 @@ class S3AuthEnforcementIntegrationTest {
private static final String PUBLIC_WRITE_ACL_BUCKET = "auth-public-write-acl-bucket";
private static final String DENY_WRITE_ACL_BUCKET = "auth-deny-write-acl-bucket";
private static final String DELETE_VERSION_BUCKET = "auth-delete-version-bucket";
private static final String BATCH_DELETE_VERSION_BUCKET = "auth-batch-delete-version-bucket";
private static final String BYPASS_BUCKET = "auth-bypass-governance-bucket";
private static final String BYPASS_KEY = "bypass-target.txt";
private static final String CONDITIONAL_DENY_ACL_BUCKET = "auth-conditional-deny-acl-bucket";
@@ -1309,6 +1310,61 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
}
@Test
@Order(44)
void batchDeleteObjectsPolicyGrantDoesNotAuthorizeVersionedDelete() {
given().when().put("/" + BATCH_DELETE_VERSION_BUCKET).then().statusCode(200);
given()
.contentType("application/xml")
.body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when()
.put("/" + BATCH_DELETE_VERSION_BUCKET + "?versioning")
.then()
.statusCode(200);
String versionId = given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("versioned batch delete target")
.when()
.put("/" + BATCH_DELETE_VERSION_BUCKET + "/" + VERSION_KEY)
.then()
.statusCode(200)
.extract().header("x-amz-version-id");
given()
.contentType("application/json")
.body(publicObjectActionPolicy(BATCH_DELETE_VERSION_BUCKET, "s3:DeleteObject"))
.when()
.put("/" + BATCH_DELETE_VERSION_BUCKET + "?policy")
.then()
.statusCode(200);
String deleteXml = """
<Delete>
<Object><Key>%s</Key><VersionId>%s</VersionId></Object>
</Delete>
""".formatted(VERSION_KEY, versionId);
given()
.contentType("application/xml")
.body(deleteXml)
.when()
.post("/" + BATCH_DELETE_VERSION_BUCKET + "?delete")
.then()
.statusCode(200)
.body(containsString("<Code>AccessDenied</Code>"))
.body(not(containsString("<Deleted>")));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.get("/" + BATCH_DELETE_VERSION_BUCKET + "?versions&prefix=" + VERSION_KEY)
.then()
.statusCode(200)
.body(containsString("<VersionId>" + versionId + "</VersionId>"));
}
private static String conditionalDenyObjectActionPolicy(String bucket, String action) {
return """
{
@@ -7,6 +7,7 @@ import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
@QuarkusTest
class S3DeleteObjectsIntegrationTest {
@@ -38,7 +39,8 @@ class S3DeleteObjectsIntegrationTest {
.post("/" + bucket + "?delete")
.then()
.statusCode(200)
.body(containsString("<Key>remove.txt</Key>"));
.body(containsString("<Key>remove.txt</Key>"))
.body(not(containsString("<VersionId>")));
given()
.when()
@@ -53,6 +55,47 @@ class S3DeleteObjectsIntegrationTest {
.statusCode(200);
}
@Test
void deleteObjects_withExplicitVersionIds_permanentlyDeletesVersionsOnVersionedBucket() {
String bucket = createBucket();
enableVersioning(bucket);
String key = "versioned.txt";
String versionId1 = putVersionedObject(bucket, key);
String versionId2 = putVersionedObject(bucket, key);
String deleteBody = """
<Delete>
<Object><Key>%s</Key><VersionId>%s</VersionId></Object>
<Object><Key>%s</Key><VersionId>%s</VersionId></Object>
</Delete>
""".formatted(key, versionId1, key, versionId2);
given()
.contentType("application/xml")
.body(deleteBody)
.when()
.post("/" + bucket + "?delete")
.then()
.statusCode(200)
.body(containsString("<Key>" + key + "</Key>"))
.body(containsString("<VersionId>" + versionId1 + "</VersionId>"))
.body(containsString("<VersionId>" + versionId2 + "</VersionId>"));
given()
.when()
.get("/" + bucket + "?versions&prefix=" + key)
.then()
.statusCode(200)
.body(not(containsString("<Version>")));
given()
.when()
.get("/" + bucket + "/" + key)
.then()
.statusCode(404);
}
private static String createBucket() {
String bucket = "delete-objects-" + UUID.randomUUID().toString().substring(0, 8);
given()
@@ -71,4 +114,24 @@ class S3DeleteObjectsIntegrationTest {
.then()
.statusCode(200);
}
private static void enableVersioning(String bucket) {
given()
.body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when()
.put("/" + bucket + "?versioning")
.then()
.statusCode(200);
}
private static String putVersionedObject(String bucket, String key) {
return given()
.body("content")
.when()
.put("/" + bucket + "/" + key)
.then()
.statusCode(200)
.extract()
.header("x-amz-version-id");
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.s3.model.CopyObjectOptions;
import io.github.hectorvent.floci.services.s3.model.S3Object;
@@ -352,4 +353,22 @@ class S3VersioningServiceTest {
"getObject should return the promoted version's content after deleting the latest");
}
@Test
void deleteObjectsWithExplicitVersionIdsPermanentlyDeletesVersions() {
s3Service.putBucketVersioning("versioned-bucket", "Enabled");
S3Object v1 = s3Service.putObject("versioned-bucket", "key",
"v1".getBytes(StandardCharsets.UTF_8), "text/plain", null);
S3Object v2 = s3Service.putObject("versioned-bucket", "key",
"v2".getBytes(StandardCharsets.UTF_8), "text/plain", null);
s3Service.deleteObjects("versioned-bucket", List.of(
new XmlParser.KeyVersion("key", v1.getVersionId()),
new XmlParser.KeyVersion("key", v2.getVersionId())));
S3Service.ListVersionsResult result = s3Service.listObjectVersions("versioned-bucket", null, 100, null);
assertTrue(result.versions().isEmpty(),
"batch delete with explicit VersionIds should permanently remove those versions, not place a delete marker");
}
}