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

@@ -3007,11 +3007,10 @@ public class S3Controller {
private Response handleGetObjectLegalHold(String bucket, String key, String versionId) {
S3Object obj = s3Service.getObjectLegalHold(bucket, key, versionId);
String status = obj.getLegalHoldStatus() != null ? obj.getLegalHoldStatus() : "OFF";
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("LegalHold", AwsNamespaces.S3)
.elem("Status", status)
.elem("Status", obj.getLegalHoldStatus())
.end("LegalHold")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
@@ -695,6 +695,7 @@ public class S3Service implements Resettable, ResourceProvider {
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
rejectUploadAclWhenOwnershipEnforced(checkedBucket.value(), checkedBucket.account(), effectiveOptions);
rejectObjectLockWithoutLockEnabled(checkedBucket.value(), effectiveOptions);
rejectConflictingServerSideEncryption(
normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption()),
validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(),
@@ -877,6 +878,7 @@ public class S3Service implements Resettable, ResourceProvider {
object.setSseCustomerKeyMd5(sseCustomerKey.keyMd5());
}
rejectUploadAclWhenOwnershipEnforced(bucket, bucketOwnerAccount, effectiveOptions);
rejectObjectLockWithoutLockEnabled(bucket, effectiveOptions);
String objectAcl = resolveObjectAclXml(bucketOwnerAccount,
effectiveOptions.getAcl(), effectiveOptions.getGrantRead(),
effectiveOptions.getGrantWrite(), effectiveOptions.getGrantFullControl(),
@@ -4027,12 +4029,55 @@ public class S3Service implements Resettable, ResourceProvider {
return bucket.getDefaultRetention();
}
/**
* An object has Object Lock settings only in a bucket with Object Lock enabled. S3 was recorded
* refusing retention and legal hold on any other bucket with this error.
*/
private static void requireObjectLockEnabled(Bucket bucket) {
if (!bucket.isObjectLockEnabled()) {
throw new AwsException("InvalidRequest", "Bucket is missing Object Lock Configuration", 400);
}
}
/**
* Refuses the Object Lock settings an object is created with in a bucket without Object Lock, as
* retention and legal hold are refused there: a mode, a retain-until date or a legal hold that is
* ON. A legal hold that is OFF sets no lock.
*/
private static void rejectObjectLockWithoutLockEnabled(Bucket bucket, PutObjectOptions options) {
if (options.getObjectLockMode() != null || options.getRetainUntilDate() != null
|| "ON".equals(options.getLegalHoldStatus())) {
requireObjectLockEnabled(bucket);
}
}
/**
* The version a retention or legal-hold request names. S3 was recorded refusing these requests
* on a bucket without Object Lock before it looks for the key.
*/
private AccountAwareStorageBackend.OwnedEntry<S3Object> getLockableObjectEntry(
String bucketName, String key, String versionId) {
requireObjectLockEnabled(resolveBucket(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404)));
return getStoredObjectEntry(bucketName, key, versionId);
}
public void putObjectRetention(String bucketName, String key, String versionId,
String mode, Instant retainUntil, boolean bypassGovernance) {
AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =
getStoredObjectEntry(bucketName, key, versionId);
getLockableObjectEntry(bucketName, key, versionId);
S3Object obj = ownedObject.value();
// A mode without a date, or a mode S3 does not know, is malformed, as S3 was recorded
// answering; a date without a mode is refused the same way, since GetObjectRetention reads a
// retention by its mode. A retention naming neither removes it.
if ((mode == null) != (retainUntil == null)
|| (mode != null && !"GOVERNANCE".equals(mode) && !"COMPLIANCE".equals(mode))) {
throw new AwsException("MalformedXML",
"The XML you provided was not well-formed or did not validate against our published schema", 400);
}
boolean activeComplianceRetention = "COMPLIANCE".equals(obj.getObjectLockMode())
&& obj.getRetainUntilDate() != null
&& Instant.now().isBefore(obj.getRetainUntilDate());
@@ -4070,12 +4115,22 @@ public class S3Service implements Resettable, ResourceProvider {
}
public S3Object getObjectRetention(String bucketName, String key, String versionId) {
return getStoredObject(bucketName, key, versionId);
S3Object object = getLockableObjectEntry(bucketName, key, versionId).value();
if (object.getObjectLockMode() == null) {
throw noSuchObjectLockConfiguration();
}
return object;
}
/** S3 was recorded answering a read of a retention or legal hold the object does not have with this error. */
private static AwsException noSuchObjectLockConfiguration() {
return new AwsException("NoSuchObjectLockConfiguration",
"The specified object does not have a ObjectLock configuration", 404);
}
public void putObjectLegalHold(String bucketName, String key, String versionId, String status) {
AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =
getStoredObjectEntry(bucketName, key, versionId);
getLockableObjectEntry(bucketName, key, versionId);
S3Object obj = ownedObject.value();
obj.setLegalHoldStatus(status);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);
@@ -4083,7 +4138,11 @@ public class S3Service implements Resettable, ResourceProvider {
}
public S3Object getObjectLegalHold(String bucketName, String key, String versionId) {
return getStoredObject(bucketName, key, versionId);
S3Object object = getLockableObjectEntry(bucketName, key, versionId).value();
if (object.getLegalHoldStatus() == null) {
throw noSuchObjectLockConfiguration();
}
return object;
}
// --- Multipart Upload Operations ---
@@ -4181,6 +4240,7 @@ public class S3Service implements Resettable, ResourceProvider {
"The specified bucket does not exist.", 404));
String ownerAccount = getBucketOwnerAccountId(bucket);
rejectUploadAclWhenOwnershipEnforced(destination, ownerAccount, objectOptions);
rejectObjectLockWithoutLockEnabled(destination, objectOptions);
resolveObjectAclXml(ownerAccount, acl, objectOptions.getGrantRead(), objectOptions.getGrantWrite(),
objectOptions.getGrantFullControl(), objectOptions.getGrantReadAcp(),
objectOptions.getGrantWriteAcp());