fix(s3): honor x-amz-bypass-governance-retention in batch DeleteObjects (#3366)

* fix(s3): honor x-amz-bypass-governance-retention in batch DeleteObjects

The batch delete path parsed the request body but never read the
x-amz-bypass-governance-retention header, so the four-argument
deleteObject overload was unreachable from DeleteObjects: every entry was
deleted with bypassGovernance false. A client that sent the header and
held s3:BypassGovernanceRetention still could not permanently delete a
version under GOVERNANCE retention, and got AccessDenied instead. The
singular DeleteObject path reads the header and checks the permission
already, so the two paths disagreed on the same request.

The controller now reads the header for the batch path, checks
s3:BypassGovernanceRetention per entry when it is set, and passes the flag
through a new deleteObjects overload. The two-argument overload is kept
and delegates with false, matching the existing deleteObject overload
ladder.

A second correction in the same response path: an AwsException raised
while deleting an entry was reported as InternalError, because the
catch-all arm ignored the exception's own code. A refusal to delete a
locked version is a per-object AccessDenied on AWS, not a server fault,
and the flattened code hid the retention lock from any client that
branches on it. AwsException now keeps its code; genuinely unexpected
failures still report InternalError.

The regression test covers both directions: without the header the locked
version survives and is reported as AccessDenied, with it the version is
permanently deleted and echoed in Deleted.VersionId.

* fix(s3): scope batch governance bypass permission

---------

Co-authored-by: dajiaohuang <dajiaohuang@users.noreply.github.com>
This commit is contained in:
Wu Shuwenanddajiaohuang authored and GitHub committed 2026-09-13 15:01:09 -04:00
1 parent f03a716e90
commit 7d96799c5a
4 files changed
+168 -2

No files matched your search

@@ -1478,6 +1478,9 @@ public class S3Controller {
}
boolean quiet = XmlParser.containsValue(xml, "Quiet", "true");
boolean bypass = "true".equalsIgnoreCase(
httpHeaders.getHeaderString("x-amz-bypass-governance-retention"));
S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(
s3Service.isAuthEnforced(), httpHeaders, uriInfo);
s3Service.authorizeSignedRequest(authorization);
@@ -1486,13 +1489,18 @@ public class S3Controller {
for (XmlParser.KeyVersion entry : entries) {
try {
s3Service.authorizeDeleteObject(bucket, entry.key(), entry.versionId(), authorization);
if (bypass && s3Service.isGovernanceRetentionActive(
bucket, entry.key(), entry.versionId())) {
s3Service.authorizeObjectWrite(bucket, entry.key(),
"s3:BypassGovernanceRetention", authorization);
}
authorizedEntries.add(entry);
} catch (AwsException e) {
authorizationErrors.add(new S3Service.DeleteError(entry.key(), e.getErrorCode(), e.getMessage()));
}
}
S3Service.DeleteObjectsResult result = s3Service.deleteObjects(bucket, authorizedEntries);
S3Service.DeleteObjectsResult result = s3Service.deleteObjects(bucket, authorizedEntries, bypass);
XmlBuilder builder = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
@@ -1335,6 +1335,25 @@ public class S3Service implements Resettable, ResourceProvider {
}
}
/**
* Returns whether the requested object version is currently protected by an active
* GOVERNANCE retention period. The bypass permission is only relevant for those versions;
* an {@code x-amz-bypass-governance-retention} header on an otherwise unprotected batch
* entry must not make that entry require {@code s3:BypassGovernanceRetention}.
*/
public boolean isGovernanceRetentionActive(String bucketName, String key, String versionId) {
ensureBucketExists(bucketName);
S3Object object = (versionId != null
? objectStore.get(versionedKey(bucketName, key, versionId))
: objectStore.get(objectKey(bucketName, key)))
.orElse(null);
return object != null
&& !object.isDeleteMarker()
&& "GOVERNANCE".equals(object.getObjectLockMode())
&& object.getRetainUntilDate() != null
&& Instant.now().isBefore(object.getRetainUntilDate());
}
public record ListObjectsResult(List<S3Object> objects, List<String> commonPrefixes, boolean isTruncated, String nextContinuationToken) {}
public List<S3Object> listObjects(String bucketName, String prefix, String delimiter, int maxKeys) {
@@ -1711,17 +1730,24 @@ public class S3Service implements Resettable, ResourceProvider {
}
public DeleteObjectsResult deleteObjects(String bucketName, List<XmlParser.KeyVersion> entries) {
return deleteObjects(bucketName, entries, false);
}
public DeleteObjectsResult deleteObjects(String bucketName, List<XmlParser.KeyVersion> entries,
boolean bypassGovernance) {
ensureBucketExists(bucketName);
List<DeleteResult> deleted = new ArrayList<>();
List<DeleteError> errors = new ArrayList<>();
for (XmlParser.KeyVersion entry : entries) {
try {
S3Object result = deleteObject(bucketName, entry.key(), entry.versionId());
S3Object result = deleteObject(bucketName, entry.key(), entry.versionId(), bypassGovernance);
if (result != null && result.isDeleteMarker()) {
deleted.add(new DeleteResult(entry.key(), entry.versionId(), true, result.getVersionId()));
} else {
deleted.add(new DeleteResult(entry.key(), entry.versionId(), false, null));
}
} catch (AwsException e) {
errors.add(new DeleteError(entry.key(), e.getErrorCode(), e.getMessage()));
} catch (Exception e) {
errors.add(new DeleteError(entry.key(), "InternalError", e.getMessage()));
}
@@ -1392,6 +1392,75 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("<VersionId>" + versionId + "</VersionId>"));
}
@Test
@Order(45)
void batchDeleteBypassPermissionOnlyAppliesToGovernanceLockedEntries() {
String bucket = "auth-batch-bypass-scope-bucket";
String unlockedKey = "unlocked.txt";
String lockedKey = "locked.txt";
given().when().put("/" + bucket).then().statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.body("unlocked")
.when()
.put("/" + bucket + "/" + unlockedKey)
.then()
.statusCode(200);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.header("x-amz-object-lock-mode", "GOVERNANCE")
.header("x-amz-object-lock-retain-until-date", "2030-01-01T00:00:00Z")
.body("locked")
.when()
.put("/" + bucket + "/" + lockedKey)
.then()
.statusCode(200);
given()
.contentType("application/json")
.body(publicObjectActionPolicy(bucket, "s3:DeleteObject"))
.when()
.put("/" + bucket + "?policy")
.then()
.statusCode(200);
String deleteXml = """
<Delete>
<Object><Key>%s</Key></Object>
<Object><Key>%s</Key></Object>
</Delete>
""".formatted(unlockedKey, lockedKey);
given()
.header("x-amz-bypass-governance-retention", "true")
.contentType("application/xml")
.body(deleteXml)
.when()
.post("/" + bucket + "?delete")
.then()
.statusCode(200)
.body(containsString("<Deleted><Key>" + unlockedKey + "</Key>"))
.body(containsString("<Error><Key>" + lockedKey + "</Key>"))
.body(containsString("<Code>AccessDenied</Code>"));
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.get("/" + bucket + "/" + unlockedKey)
.then()
.statusCode(404);
given()
.header("Authorization", LOCAL_AUTH_HEADER)
.when()
.get("/" + bucket + "/" + lockedKey)
.then()
.statusCode(200)
.body(equalTo("locked"));
}
private static String conditionalDenyObjectActionPolicy(String bucket, String action) {
return """
{
@@ -96,6 +96,56 @@ class S3DeleteObjectsIntegrationTest {
.statusCode(404);
}
@Test
void deleteObjects_governanceRetentionVersionIdHonorsBypassHeader() {
String bucket = createBucket();
enableVersioning(bucket);
String key = "governance.txt";
String versionId = putGovernanceLockedObject(bucket, key);
String deleteBody = """
<Delete>
<Object><Key>%s</Key><VersionId>%s</VersionId></Object>
</Delete>
""".formatted(key, versionId);
given()
.contentType("application/xml")
.body(deleteBody)
.when()
.post("/" + bucket + "?delete")
.then()
.statusCode(200)
.body(containsString("<Error>"))
.body(containsString("<Key>" + key + "</Key>"))
.body(containsString("<Code>AccessDenied</Code>"));
given()
.when()
.get("/" + bucket + "?versions&prefix=" + key)
.then()
.statusCode(200)
.body(containsString(versionId));
given()
.header("x-amz-bypass-governance-retention", "true")
.contentType("application/xml")
.body(deleteBody)
.when()
.post("/" + bucket + "?delete")
.then()
.statusCode(200)
.body(containsString("<VersionId>" + versionId + "</VersionId>"));
given()
.when()
.get("/" + bucket + "?versions&prefix=" + key)
.then()
.statusCode(200)
.body(not(containsString(versionId)));
}
private static String createBucket() {
String bucket = "delete-objects-" + UUID.randomUUID().toString().substring(0, 8);
given()
@@ -124,6 +174,19 @@ class S3DeleteObjectsIntegrationTest {
.statusCode(200);
}
private static String putGovernanceLockedObject(String bucket, String key) {
return given()
.header("x-amz-object-lock-mode", "GOVERNANCE")
.header("x-amz-object-lock-retain-until-date", "2030-01-01T00:00:00Z")
.body("locked")
.when()
.put("/" + bucket + "/" + key)
.then()
.statusCode(200)
.extract()
.header("x-amz-version-id");
}
private static String putVersionedObject(String bucket, String key) {
return given()
.body("content")