fix(s3): refuse Object Lock settings on a bucket without Object Lock (#5969)

PutObjectRetention, GetObjectRetention, PutObjectLegalHold and
GetObjectLegalHold on a bucket without Object Lock set or returned the
setting with a 200. S3 refuses them with 400 InvalidRequest, "Bucket is
missing Object Lock Configuration", as LocalStack's AWS-validated
recordings show, and Floci now does too, before it looks for the key.
A legal hold without a status is still refused as malformed first, as
recorded.

PutObject, CopyObject and CreateMultipartUpload applied a mode, a
retain-until date or a legal hold of ON on such a bucket as well, and
since deletes enforce the lock there too, a COMPLIANCE object in it
could not be deleted until its date passed. They are refused the same
way. No AWS document or recording covers object creation; moto refuses
the same headers with InvalidRequest under its own message, so the
recorded one is used. A legal hold of OFF sets no lock and is still
accepted.

In a bucket with Object Lock, GetObjectRetention and GetObjectLegalHold
of an object without a retention or a legal hold answered 200, with no
mode or with OFF. S3 was recorded answering both with 404
NoSuchObjectLockConfiguration, which they answer now.

PutObjectRetention saved a mode without a date, a date without a mode,
or a mode S3 does not know. S3 was recorded refusing a mode without a
date, and an unknown mode, with 400 MalformedXML, and a date without a
mode would now read as no retention at all, so all three are refused
that way. A retention naming neither still removes it.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-19 07:43:15 -05:00
1 parent fe5e0f06fa
commit 36b315d77b
11 files changed
+238 -27

No files matched your search

@@ -1481,17 +1481,24 @@ class S3AuthEnforcementIntegrationTest {
String bucket = "auth-batch-bypass-scope-bucket";
String unlockedKey = "unlocked.txt";
String lockedKey = "locked.txt";
given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
given()
.filter(LOCAL_SIGNER)
.header("x-amz-bucket-object-lock-enabled", "true")
.when()
.put("/" + bucket)
.then()
.statusCode(200);
String unlockedVersion = given()
.filter(LOCAL_SIGNER)
.body("unlocked")
.when()
.put("/" + bucket + "/" + unlockedKey)
.then()
.statusCode(200);
.statusCode(200)
.extract().header("x-amz-version-id");
given()
String lockedVersion = given()
.filter(LOCAL_SIGNER)
.header("x-amz-object-lock-mode", "GOVERNANCE")
.header("x-amz-object-lock-retain-until-date", "2030-01-01T00:00:00Z")
@@ -1499,23 +1506,25 @@ class S3AuthEnforcementIntegrationTest {
.when()
.put("/" + bucket + "/" + lockedKey)
.then()
.statusCode(200);
.statusCode(200)
.extract().header("x-amz-version-id");
given()
.filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(bucket, "s3:DeleteObject"))
.body(publicObjectActionPolicy(bucket, "s3:DeleteObjectVersion"))
.when()
.put("/" + bucket + "?policy")
.then()
.statusCode(200);
// The lock protects a version, so the entries name theirs.
String deleteXml = """
<Delete>
<Object><Key>%s</Key></Object>
<Object><Key>%s</Key></Object>
<Object><Key>%s</Key><VersionId>%s</VersionId></Object>
<Object><Key>%s</Key><VersionId>%s</VersionId></Object>
</Delete>
""".formatted(unlockedKey, lockedKey);
""".formatted(unlockedKey, unlockedVersion, lockedKey, lockedVersion);
given()
.header("x-amz-bypass-governance-retention", "true")
@@ -231,7 +231,7 @@ class S3DeleteObjectsIamEnforcementIntegrationTest {
@Test
void bypassingGovernanceRetentionNeedsTheBypassPermission() {
String bucket = bucket("delobj-governance");
enableVersioning(bucket);
enableObjectLock(bucket);
String versionId = putGovernanceRetained(bucket, "retained.txt");
String plainVersionId = put(bucket, "plain.txt");
String user = uniqueName("delobj-governance");
@@ -260,12 +260,17 @@ class S3DeleteObjectsIamEnforcementIntegrationTest {
}
// An object written before versioning was enabled is the null version, which an entry names as
// VersionId "null". Its retention protects it as any version's does, so the bypass needs the permission.
// VersionId "null". Retention set on it once the bucket has Object Lock protects it as any version's
// does, so the bypass needs the permission.
@Test
void bypassingGovernanceRetentionOnTheNullVersionNeedsTheBypassPermission() {
String bucket = bucket("delobj-governance-null");
putGovernanceRetained(bucket, "retained.txt");
enableVersioning(bucket);
put(bucket, "retained.txt");
enableObjectLock(bucket);
given().header("Authorization", auth(ADMIN, "s3")).queryParam("versionId", "null")
.body("<Retention><Mode>GOVERNANCE</Mode>"
+ "<RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate></Retention>")
.when().put("/" + bucket + "/retained.txt?retention").then().statusCode(200);
String user = uniqueName("delobj-governance-null");
String accessKeyId = createUser(user);
String objects = "arn:aws:s3:::" + bucket + "/*";
@@ -289,7 +294,7 @@ class S3DeleteObjectsIamEnforcementIntegrationTest {
@Test
void aNullVersionEntryDoesNotInheritTheRetentionOfAVersionedObject() {
String bucket = bucket("delobj-governance-no-null");
enableVersioning(bucket);
enableObjectLock(bucket);
String versionId = putGovernanceRetained(bucket, "retained.txt");
String user = uniqueName("delobj-governance-no-null");
String accessKeyId = createUser(user);
@@ -455,6 +460,14 @@ class S3DeleteObjectsIamEnforcementIntegrationTest {
.when().put("/" + bucket + "?versioning").then().statusCode(200);
}
/** Object Lock on an existing bucket, which needs versioning: "Object Lock works only with versioned buckets". */
private static void enableObjectLock(String bucket) {
enableVersioning(bucket);
given().header("Authorization", auth(ADMIN, "s3")).contentType("application/xml")
.body("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")
.when().put("/" + bucket + "?object-lock").then().statusCode(200);
}
private static void assertPresent(String bucket, String key) {
given().header("Authorization", auth(ADMIN, "s3")).when().head("/" + bucket + "/" + key)
.then().statusCode(200);
@@ -129,6 +129,12 @@ class S3DeleteObjectsIntegrationTest {
void deleteObjects_governanceRetentionVersionIdHonorsBypassHeader() {
String bucket = createBucket();
enableVersioning(bucket);
given()
.body("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")
.when()
.put("/" + bucket + "?object-lock")
.then()
.statusCode(200);
String key = "governance.txt";
String versionId = putGovernanceLockedObject(bucket, key);
@@ -252,6 +252,7 @@ class S3GlobalBucketNamespaceTest {
globalNs.putBucketOwnershipControls("shared-metadata-bucket", "<OwnershipControls><Rule>"
+ "<ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>");
globalNs.putBucketVersioning("shared-metadata-bucket", "Enabled");
globalNs.setBucketObjectLockEnabled("shared-metadata-bucket");
S3Object created = globalNs.putObject(
"shared-metadata-bucket", "object.txt", "payload".getBytes(UTF_8),
"text/plain", Map.of());
@@ -129,6 +129,7 @@ class S3LifecycleServiceTest {
@Test
void aNoncurrentVersionUnderALegalHoldIsKept() {
s3Service.putBucketVersioning(BUCKET, "Enabled");
s3Service.setBucketObjectLockEnabled(BUCKET);
S3Object held = s3Service.putObject(BUCKET, "a.txt", "held".getBytes(StandardCharsets.UTF_8), "text/plain",
Map.of(), null, null, "ON");
S3Object current = put("a.txt");
@@ -144,6 +144,7 @@ class S3ObjectIndexJournalTest {
S3Service s3 = newService(first, directory);
s3.createBucket(VERSIONED_BUCKET, REGION);
s3.putBucketVersioning(VERSIONED_BUCKET, "Enabled");
s3.setBucketObjectLockEnabled(VERSIONED_BUCKET);
String current = s3.putObject(VERSIONED_BUCKET, key(0), body(0), "text/plain", Map.of()).getVersionId();
s3.putObjectLegalHold(VERSIONED_BUCKET, key(0), current, "ON");
copyAsLeftByAKill(directory, afterCrash);
@@ -465,6 +465,130 @@ class S3ObjectLockIntegrationTest {
}
}
// --- Object Lock settings on a bucket without Object Lock ---
@Test
@Order(25)
void retentionAndLegalHoldAreRefusedOnABucketWithoutObjectLock() {
String retention = "<Retention><Mode>GOVERNANCE</Mode>"
+ "<RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate></Retention>";
for (String bucket : new String[] {PLAIN_BUCKET, NO_LOCK_BUCKET}) {
String path = "/" + bucket + "/unlockable.txt";
given().body("x").when().put(path).then().statusCode(200);
assertMissingObjectLockConfiguration(given().body(retention).when().put(path + "?retention"));
assertMissingObjectLockConfiguration(given().when().get(path + "?retention"));
assertMissingObjectLockConfiguration(given().body("<LegalHold><Status>ON</Status></LegalHold>")
.when().put(path + "?legal-hold"));
assertMissingObjectLockConfiguration(given().when().get(path + "?legal-hold"));
// S3 was recorded answering a legal hold without a status as malformed first.
given()
.body("<LegalHold></LegalHold>")
.when()
.put(path + "?legal-hold")
.then()
.statusCode(400)
.body(containsString("<Code>MalformedXML</Code>"));
}
}
@Test
@Order(26)
void objectLockHeadersAreRefusedOnABucketWithoutObjectLock() {
given().body("source").when().put("/" + PLAIN_BUCKET + "/plain-source.txt").then().statusCode(200);
String copySource = "/" + PLAIN_BUCKET + "/plain-source.txt";
for (String operation : new String[] {"put", "copy", "upload"}) {
String path = "/" + PLAIN_BUCKET + "/refused-lock-" + operation + ".txt";
assertMissingObjectLockConfiguration(createObject(operation, path, copySource, given()
.header("x-amz-object-lock-mode", "GOVERNANCE")
.header("x-amz-object-lock-retain-until-date", "2099-01-01T00:00:00Z")));
assertMissingObjectLockConfiguration(createObject(operation, path, copySource, given()
.header("x-amz-object-lock-legal-hold", "ON")));
given().when().head(path).then().statusCode(404);
// A legal hold that is OFF sets no lock, so the object is created.
createObject(operation, path, copySource, given().header("x-amz-object-lock-legal-hold", "OFF"))
.then()
.statusCode(200);
}
}
@Test
@Order(27)
void aCopyOfALockedObjectIntoABucketWithoutObjectLockTakesNoLock() {
given()
.header("x-amz-copy-source", "/" + LOCK_BUCKET + "/copy-lock-target.txt")
.when()
.put("/" + PLAIN_BUCKET + "/copy-of-locked.txt")
.then()
.statusCode(200);
given().when().delete("/" + PLAIN_BUCKET + "/copy-of-locked.txt").then().statusCode(204);
}
@Test
@Order(28)
void readingARetentionOrLegalHoldTheObjectDoesNotHaveIsNoSuchObjectLockConfiguration() {
String bucket = "object-lock-without-rule";
given().header("x-amz-bucket-object-lock-enabled", "true").when().put("/" + bucket).then().statusCode(200);
String path = "/" + bucket + "/never-locked.txt";
given().body("x").when().put(path).then().statusCode(200);
for (String setting : new String[] {"?retention", "?legal-hold"}) {
given()
.when()
.get(path + setting)
.then()
.statusCode(404)
.body(containsString("<Code>NoSuchObjectLockConfiguration</Code>"))
.body(containsString("<Message>The specified object does not have a ObjectLock configuration</Message>"));
}
}
@Test
@Order(29)
void aRetentionMissingItsModeOrItsDateOrWithAnUnknownModeIsMalformedAndNotSaved() {
String bucket = "object-lock-incomplete-retention";
given().header("x-amz-bucket-object-lock-enabled", "true").when().put("/" + bucket).then().statusCode(200);
String path = "/" + bucket + "/incomplete.txt";
given().body("x").when().put(path).then().statusCode(200);
for (String retention : new String[] {
"<Mode>GOVERNANCE</Mode>",
"<RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate>",
"<Mode>BAD-VALUE</Mode><RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate>"}) {
given()
.body("<Retention xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + retention + "</Retention>")
.when()
.put(path + "?retention")
.then()
.statusCode(400)
.body(containsString("<Code>MalformedXML</Code>"));
}
given()
.when()
.get(path + "?retention")
.then()
.statusCode(404)
.body(containsString("<Code>NoSuchObjectLockConfiguration</Code>"));
}
/** A PutObject, a CopyObject of {@code copySource} or a CreateMultipartUpload of {@code path}. */
private static Response createObject(String operation, String path, String copySource,
RequestSpecification request) {
return switch (operation) {
case "put" -> request.body("x").when().put(path);
case "copy" -> request.header("x-amz-copy-source", copySource).when().put(path);
default -> request.when().post(path + "?uploads");
};
}
private static void assertMissingObjectLockConfiguration(Response response) {
response.then()
.statusCode(400)
.body(containsString("<Code>InvalidRequest</Code>"))
.body(containsString("<Message>Bucket is missing Object Lock Configuration</Message>"));
}
/** A PutObject, a CopyObject of {@code copySource} or a CreateMultipartUpload refused for its lock headers. */
private static void assertInvalidLockHeaders(String operation, String copySource, String mode, String until,
String argumentName, String argumentValue, String message) {
@@ -476,12 +600,7 @@ class S3ObjectLockIntegrationTest {
request = request.header("x-amz-object-lock-retain-until-date", until);
}
String path = "/" + LOCK_BUCKET + "/invalid-lock-" + operation + ".txt";
Response response = switch (operation) {
case "put" -> request.body("x").when().put(path);
case "copy" -> request.header("x-amz-copy-source", copySource).when().put(path);
default -> request.when().post(path + "?uploads");
};
ValidatableResponse then = response.then()
ValidatableResponse then = createObject(operation, path, copySource, request).then()
.statusCode(400)
.body(containsString("<Code>InvalidArgument</Code>"))
.body(containsString(message))
@@ -493,6 +493,7 @@ class S3ServiceAnnotationsTest {
@Test
void governanceRetentionBlocksAnnotationDeleteWithoutBypass() {
s3Service.setBucketObjectLockEnabled(BUCKET);
s3Service.putObject(BUCKET, "locked.txt", "body".getBytes(StandardCharsets.UTF_8),
null, null, new io.github.hectorvent.floci.services.s3.model.PutObjectOptions()
.withObjectLockMode("GOVERNANCE")
@@ -523,6 +524,7 @@ class S3ServiceAnnotationsTest {
@Test
void complianceRetentionBlocksAnnotationPutAndDelete() {
s3Service.setBucketObjectLockEnabled(BUCKET);
s3Service.putObject(BUCKET, "compliance.txt", "body".getBytes(StandardCharsets.UTF_8),
null, null, new io.github.hectorvent.floci.services.s3.model.PutObjectOptions()
.withObjectLockMode("COMPLIANCE")