fix(s3): return object owners in list responses (#6953)

* fix(s3): return object owners in list responses

* refactor(s3): share effective object owner resolution
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-29 20:39:40 -04:00
1 parent 4364310b95
commit f8d37ded69
6 files changed
+303 -6

No files matched your search

@@ -764,6 +764,9 @@ public class S3Controller {
List<S3Object> objects = result.objects();
List<String> commonPrefixes = result.commonPrefixes();
boolean v2 = "2".equals(listType);
String bucketOwnerAccount = (v1 || Boolean.parseBoolean(uriInfo.getQueryParameters().getFirst("fetch-owner")))
? s3Service.getBucketOwnerAccountId(bucket) : null;
boolean ownershipEnforced = bucketOwnerAccount != null && s3Service.bucketOwnershipIsEnforced(bucket);
XmlBuilder xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
@@ -782,8 +785,15 @@ public class S3Controller {
.elem("LastModified", ISO_FORMAT.format(obj.getLastModified()))
.elem("ETag", obj.getETag())
.elem("Size", obj.getSize())
.elem("StorageClass", obj.getStorageClass())
.end("Contents");
.elem("StorageClass", obj.getStorageClass());
if (bucketOwnerAccount != null) {
xml.start("Owner")
.elem("ID", AwsCanonicalUserId.forAccount(S3Service.effectiveObjectOwnerAccount(
obj, bucketOwnerAccount, ownershipEnforced)))
.elem("DisplayName", S3Service.DEFAULT_OWNER_DISPLAY_NAME)
.end("Owner");
}
xml.end("Contents");
}
for (String cp : commonPrefixes) {
xml.start("CommonPrefixes")
@@ -627,6 +627,12 @@ public class S3Service implements Resettable, ResourceProvider {
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
}
boolean bucketOwnershipIsEnforced(String bucketName) {
return resolveBucketEntry(bucketName)
.map(entry -> ownershipIsEnforced(entry.value()))
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
}
public void putBucketLogging(String bucketName, String loggingConfigurationXml) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
@@ -910,6 +916,9 @@ public class S3Service implements Resettable, ResourceProvider {
byte[] data = body instanceof BytesBody bytes ? bytes.data() : null;
S3Object object = new S3Object(bucketName, key, body.size(), contentType,
eTag != null ? eTag : computeETag(data));
String objectOwnerAccount = objectOwnerAccount(bucket, bucketOwnerAccount,
aclRequestAccount, effectiveOptions.getAcl());
object.setOwnerAccountId(objectOwnerAccount);
object.setData(data);
if (metadata != null) {
object.getMetadata().putAll(metadata);
@@ -935,7 +944,7 @@ public class S3Service implements Resettable, ResourceProvider {
rejectUploadAclWhenOwnershipEnforced(bucket, bucketOwnerAccount, effectiveOptions,
aclRequestAccount);
rejectObjectLockWithoutLockEnabled(bucket, effectiveOptions);
String objectAcl = resolveObjectAclXml(bucketOwnerAccount,
String objectAcl = resolveStoredObjectAclXml(objectOwnerAccount, bucketOwnerAccount,
effectiveOptions.getAcl(), effectiveOptions.getGrantRead(),
effectiveOptions.getGrantWrite(), effectiveOptions.getGrantFullControl(),
effectiveOptions.getGrantReadAcp(), effectiveOptions.getGrantWriteAcp());
@@ -6060,9 +6069,11 @@ public class S3Service implements Resettable, ResourceProvider {
S3Object obj = ownedObject.value();
Bucket bucket = resolveBucket(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
return !ownershipIsEnforced(bucket) && obj.getAcl() != null
boolean enforced = ownershipIsEnforced(bucket);
return !enforced && obj.getAcl() != null
? obj.getAcl()
: defaultAclXml(ownedObject.account(), DEFAULT_OWNER_DISPLAY_NAME);
: defaultAclXml(effectiveObjectOwnerAccount(obj, ownedObject.account(), enforced),
DEFAULT_OWNER_DISPLAY_NAME);
}
public void putObjectAcl(String bucketName, String key, String versionId, String bodyAcl, String cannedAcl,
@@ -6075,7 +6086,8 @@ public class S3Service implements Resettable, ResourceProvider {
getStoredObjectEntry(bucketName, key, versionId);
S3Object obj = ownedObject.value();
rejectAclWhenOwnershipEnforced(bucket);
String resolvedAcl = resolveObjectAclXml(ownedObject.account(), cannedAcl, grantRead,
String resolvedAcl = resolveStoredObjectAclXml(effectiveObjectOwnerAccount(obj, ownedObject.account(), false),
ownedObject.account(), cannedAcl, grantRead,
grantWrite, grantFullControl, grantReadAcp, grantWriteAcp);
String newAcl = resolvedAcl != null ? resolvedAcl : (bodyAcl.isBlank() ? null : bodyAcl);
rejectPublicAclWhenBlocked(bucketName, newAcl);
@@ -6136,6 +6148,23 @@ public class S3Service implements Resettable, ResourceProvider {
"ObjectOwnership", "BucketOwnerEnforced");
}
private static String objectOwnerAccount(Bucket bucket, String bucketOwnerAccount,
String writerAccount, String cannedAcl) {
String controls = bucket.getOwnershipControlsConfiguration();
if (ownershipIsEnforced(bucket)
|| (XmlParser.containsValue(controls, "ObjectOwnership", "BucketOwnerPreferred")
&& "bucket-owner-full-control".equals(cannedAcl))) {
return bucketOwnerAccount;
}
return writerAccount;
}
static String effectiveObjectOwnerAccount(S3Object object, String bucketOwnerAccount,
boolean ownershipEnforced) {
return ownershipEnforced || object.getOwnerAccountId() == null
? bucketOwnerAccount : object.getOwnerAccountId();
}
/**
* Returns the bucket's server-side encryption configuration as XML.
* <p>
@@ -6581,6 +6610,27 @@ public class S3Service implements Resettable, ResourceProvider {
return objectAclXml(ownerAccount, grants.toArray(new String[0]));
}
private String resolveStoredObjectAclXml(String objectOwnerAccount, String bucketOwnerAccount,
String cannedAcl, String grantRead, String grantWrite,
String grantFullControl, String grantReadAcp,
String grantWriteAcp) {
String acl = resolveObjectAclXml(objectOwnerAccount, cannedAcl, grantRead, grantWrite,
grantFullControl, grantReadAcp, grantWriteAcp);
if (objectOwnerAccount.equals(bucketOwnerAccount)) {
return acl;
}
if ("bucket-owner-full-control".equals(cannedAcl)) {
return objectAclXml(objectOwnerAccount, ownerFullControlGrant(objectOwnerAccount),
ownerFullControlGrant(bucketOwnerAccount));
}
if ("bucket-owner-read".equals(cannedAcl)) {
return objectAclXml(objectOwnerAccount, ownerFullControlGrant(objectOwnerAccount),
canonicalUserGrant(AwsCanonicalUserId.forAccount(bucketOwnerAccount),
DEFAULT_OWNER_DISPLAY_NAME, "READ"));
}
return acl;
}
private static boolean isBlank(String value) {
return value == null || value.isBlank();
}
@@ -7183,6 +7233,7 @@ public class S3Service implements Resettable, ResourceProvider {
copy.setRetainUntilDate(source.getRetainUntilDate());
copy.setLegalHoldStatus(source.getLegalHoldStatus());
copy.setAcl(source.getAcl());
copy.setOwnerAccountId(source.getOwnerAccountId());
copy.setDataGeneration(source.getDataGeneration());
copy.setEventSequencer(source.getEventSequencer());
return copy;
@@ -45,6 +45,7 @@ public class S3Object {
private Instant retainUntilDate; // null if no retention
private String legalHoldStatus; // "ON" | "OFF" | null
private String acl;
private String ownerAccountId;
// Internal-only per-write stamp (not an S3 versionId, and set even when bucket versioning is
// off - see S3Service#getLatestObject). A fresh value is assigned every time this key is
@@ -170,6 +171,9 @@ public class S3Object {
public String getAcl() { return acl; }
public void setAcl(String acl) { this.acl = acl; }
public String getOwnerAccountId() { return ownerAccountId; }
public void setOwnerAccountId(String ownerAccountId) { this.ownerAccountId = ownerAccountId; }
public String getDataGeneration() { return dataGeneration; }
public void setDataGeneration(String dataGeneration) { this.dataGeneration = dataGeneration; }