fix(s3): honor ACL headers and explicit grants (#1768)

* fix(s3): honor ACL headers and explicit grants

Closes #1766 #1767

PutObjectAcl/PutBucketAcl ignored x-amz-acl and x-amz-grant-* headers,
storing the raw (often empty) request body as the ACL verbatim. AWS
SDKs send canned ACLs to these endpoints as a header with an empty
body, so a canned-ACL update corrupted the stored ACL and the next
GetObjectAcl failed client-side XML parsing with "Premature end of
file".

PutObject/CopyObject also silently dropped x-amz-grant-* (explicit
ACL grant) headers - no error, just a missing grant.

Add resolveObjectAclXml() to parse canned ACLs and explicit grant
headers (uri=/id=/emailAddress= tokens, comma-separated per grantee
header) into proper Grant XML, and wire it into all four call sites:
PutObject, CopyObject, PutObjectAcl, PutBucketAcl. emailAddress
grantees return 501 (no account directory to resolve them against).

Add regression coverage in S3AclIntegrationTest for both the
header-with-empty-body case and explicit multi-grantee headers.

* fix(s3): Fixing review comments

---------

Co-authored-by: Krishnan Mahadevan <kmahadevan-bidgely@users.noreply.github.com>
This commit is contained in:
Krishnan MahadevanandKrishnan Mahadevan authored and GitHub committed 2026-07-15 00:55:49 -04:00
1 parent 4a109b2f6c
commit 43a80381ee
5 files changed
+357 -8

No files matched your search

@@ -228,7 +228,13 @@ public class S3Controller {
.build();
}
if (hasQueryParam(uriInfo, "acl")) {
s3Service.putBucketAcl(bucket, new String(body, StandardCharsets.UTF_8));
s3Service.putBucketAcl(bucket, new String(body, StandardCharsets.UTF_8),
httpHeaders.getHeaderString("x-amz-acl"),
httpHeaders.getHeaderString("x-amz-grant-read"),
httpHeaders.getHeaderString("x-amz-grant-write"),
httpHeaders.getHeaderString("x-amz-grant-full-control"),
httpHeaders.getHeaderString("x-amz-grant-read-acp"),
httpHeaders.getHeaderString("x-amz-grant-write-acp"));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "encryption")) {
@@ -550,7 +556,13 @@ public class S3Controller {
}
if (hasQueryParam(uriInfo, "acl")) {
s3Service.putObjectAcl(bucket, key, uriInfo.getQueryParameters().getFirst("versionId"),
new String(body, StandardCharsets.UTF_8));
new String(body, StandardCharsets.UTF_8),
httpHeaders.getHeaderString("x-amz-acl"),
httpHeaders.getHeaderString("x-amz-grant-read"),
httpHeaders.getHeaderString("x-amz-grant-write"),
httpHeaders.getHeaderString("x-amz-grant-full-control"),
httpHeaders.getHeaderString("x-amz-grant-read-acp"),
httpHeaders.getHeaderString("x-amz-grant-write-acp"));
return Response.ok().build();
}
@@ -607,6 +619,11 @@ public class S3Controller {
.withSseCustomerKey(sseCustomerKey)
.withSseCustomerKeyMd5(sseCustomerKeyMd5)
.withAcl(cannedAcl)
.withGrantRead(httpHeaders.getHeaderString("x-amz-grant-read"))
.withGrantWrite(httpHeaders.getHeaderString("x-amz-grant-write"))
.withGrantFullControl(httpHeaders.getHeaderString("x-amz-grant-full-control"))
.withGrantReadAcp(httpHeaders.getHeaderString("x-amz-grant-read-acp"))
.withGrantWriteAcp(httpHeaders.getHeaderString("x-amz-grant-write-acp"))
.withChecksumAlgorithm(checksumAlgorithm)
.withClientChecksum(extractChecksumFromHeaders(httpHeaders))
.withIfMatch(ifMatch)
@@ -1867,7 +1884,12 @@ public class S3Controller {
.withCopySourceSseCustomerKey(httpHeaders.getHeaderString("x-amz-copy-source-server-side-encryption-customer-key"))
.withCopySourceSseCustomerKeyMd5(httpHeaders.getHeaderString("x-amz-copy-source-server-side-encryption-customer-key-MD5"))
.withChecksumAlgorithm(getChecksumAlgorithm(httpHeaders))
.withAcl(cannedAcl));
.withAcl(cannedAcl)
.withGrantRead(httpHeaders.getHeaderString("x-amz-grant-read"))
.withGrantWrite(httpHeaders.getHeaderString("x-amz-grant-write"))
.withGrantFullControl(httpHeaders.getHeaderString("x-amz-grant-full-control"))
.withGrantReadAcp(httpHeaders.getHeaderString("x-amz-grant-read-acp"))
.withGrantWriteAcp(httpHeaders.getHeaderString("x-amz-grant-write-acp")));
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("CopyObjectResult", AwsNamespaces.S3)
@@ -40,6 +40,8 @@ import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ApplicationScoped
public class S3Service implements Resettable {
@@ -370,7 +372,9 @@ public class S3Service implements Resettable {
object.setSseCustomerAlgorithm(sseCustomerKey.algorithm());
object.setSseCustomerKeyMd5(sseCustomerKey.keyMd5());
}
object.setAcl(cannedObjectAclXml(effectiveOptions.getAcl()));
object.setAcl(resolveObjectAclXml(effectiveOptions.getAcl(), effectiveOptions.getGrantRead(),
effectiveOptions.getGrantWrite(), effectiveOptions.getGrantFullControl(),
effectiveOptions.getGrantReadAcp(), effectiveOptions.getGrantWriteAcp()));
if (effectiveOptions.getTagging() != null && !effectiveOptions.getTagging().isEmpty()) {
object.setTags(new HashMap<>(effectiveOptions.getTagging()));
}
@@ -1738,10 +1742,12 @@ public class S3Service implements Resettable {
return bucket.getAcl() != null ? bucket.getAcl() : defaultAclXml(ownerId(), DEFAULT_OWNER_DISPLAY_NAME);
}
public void putBucketAcl(String bucketName, String acl) {
public void putBucketAcl(String bucketName, String bodyAcl, String cannedAcl, String grantRead,
String grantWrite, String grantFullControl, String grantReadAcp, String grantWriteAcp) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
bucket.setAcl(acl);
String resolvedAcl = resolveObjectAclXml(cannedAcl, grantRead, grantWrite, grantFullControl, grantReadAcp, grantWriteAcp);
bucket.setAcl(resolvedAcl != null ? resolvedAcl : (bodyAcl.isBlank() ? null : bodyAcl));
bucketStore.put(bucketName, bucket);
}
@@ -1750,9 +1756,12 @@ public class S3Service implements Resettable {
return obj.getAcl() != null ? obj.getAcl() : defaultAclXml(ownerId(), DEFAULT_OWNER_DISPLAY_NAME);
}
public void putObjectAcl(String bucketName, String key, String versionId, String acl) {
public void putObjectAcl(String bucketName, String key, String versionId, String bodyAcl, String cannedAcl,
String grantRead, String grantWrite, String grantFullControl,
String grantReadAcp, String grantWriteAcp) {
S3Object obj = getObject(bucketName, key, versionId);
obj.setAcl(acl);
String resolvedAcl = resolveObjectAclXml(cannedAcl, grantRead, grantWrite, grantFullControl, grantReadAcp, grantWriteAcp);
obj.setAcl(resolvedAcl != null ? resolvedAcl : (bodyAcl.isBlank() ? null : bodyAcl));
String storeKey = (versionId != null) ? versionedKey(bucketName, key, versionId) : objectKey(bucketName, key);
objectStore.put(storeKey, obj);
}
@@ -1911,6 +1920,68 @@ public class S3Service implements Resettable {
.build();
}
/**
* Resolves the ACL to store for an object/bucket from a canned ACL and/or the explicit
* ACL grant headers (x-amz-grant-read, x-amz-grant-write, x-amz-grant-full-control,
* x-amz-grant-read-acp, x-amz-grant-write-acp). If both are somehow present, the canned ACL
* takes precedence - real S3 instead rejects that combination with 400 "Conflicting header
* values", but Floci doesn't model that validation yet. Returns null if neither is set, so
* callers can fall back to a pre-existing ACL (e.g. an explicit AccessControlPolicy XML body).
*/
String resolveObjectAclXml(String cannedAcl, String grantRead, String grantWrite,
String grantFullControl, String grantReadAcp, String grantWriteAcp) {
if (cannedAcl != null && !cannedAcl.isBlank()) {
return cannedObjectAclXml(cannedAcl);
}
if (isBlank(grantRead) && isBlank(grantWrite) && isBlank(grantFullControl)
&& isBlank(grantReadAcp) && isBlank(grantWriteAcp)) {
return null;
}
List<String> grants = new ArrayList<>();
grants.add(ownerFullControlGrant());
appendGrantHeader(grants, grantRead, "READ");
appendGrantHeader(grants, grantWrite, "WRITE");
appendGrantHeader(grants, grantFullControl, "FULL_CONTROL");
appendGrantHeader(grants, grantReadAcp, "READ_ACP");
appendGrantHeader(grants, grantWriteAcp, "WRITE_ACP");
return objectAclXml(grants.toArray(new String[0]));
}
private static boolean isBlank(String value) {
return value == null || value.isBlank();
}
// Matches a single grantee token from an x-amz-grant-* header value, e.g.
// uri="http://acs.amazonaws.com/groups/global/AllUsers" or id="<canonical-id>". AWS allows
// a comma-separated list of these per header.
private static final Pattern GRANTEE_TOKEN_PATTERN = Pattern.compile("(uri|id|emailAddress)=\"([^\"]*)\"");
private void appendGrantHeader(List<String> grants, String headerValue, String permission) {
if (isBlank(headerValue)) {
return;
}
Matcher matcher = GRANTEE_TOKEN_PATTERN.matcher(headerValue);
boolean matched = false;
while (matcher.find()) {
matched = true;
grants.add(granteeGrant(matcher.group(1), matcher.group(2), permission));
}
if (!matched) {
throw new AwsException("InvalidArgument", "Malformed ACL grant header: " + headerValue, 400);
}
}
private String granteeGrant(String granteeType, String granteeValue, String permission) {
return switch (granteeType) {
case "uri" -> groupGrant(granteeValue, permission);
// Floci has no directory of external accounts, so an explicit CanonicalUser grant is
// stored using the caller-supplied ID verbatim as both ID and display name.
case "id" -> canonicalUserGrant(granteeValue, granteeValue, permission);
default -> throw new AwsException("NotImplemented",
"Explicit ACL grants by emailAddress are not supported.", 501);
};
}
String cannedObjectAclXml(String cannedAcl) {
if (cannedAcl == null || cannedAcl.isBlank()) {
return null;
@@ -2611,6 +2682,11 @@ public class S3Service implements Resettable {
.withSseCustomerKey(effectiveOptions.getSseCustomerKey())
.withSseCustomerKeyMd5(effectiveOptions.getSseCustomerKeyMd5())
.withAcl(effectiveOptions.getAcl())
.withGrantRead(effectiveOptions.getGrantRead())
.withGrantWrite(effectiveOptions.getGrantWrite())
.withGrantFullControl(effectiveOptions.getGrantFullControl())
.withGrantReadAcp(effectiveOptions.getGrantReadAcp())
.withGrantWriteAcp(effectiveOptions.getGrantWriteAcp())
.withChecksumAlgorithm(copyChecksumAlgorithm)
.withTagging(effectiveTags));
copy.setETag(source.getETag());
@@ -20,6 +20,11 @@ public class CopyObjectOptions {
private String copySourceSseCustomerKey;
private String copySourceSseCustomerKeyMd5;
private String acl;
private String grantRead;
private String grantWrite;
private String grantFullControl;
private String grantReadAcp;
private String grantWriteAcp;
private String checksumAlgorithm;
public String getMetadataDirective() { return metadataDirective; }
@@ -73,6 +78,21 @@ public class CopyObjectOptions {
public String getAcl() { return acl; }
public CopyObjectOptions withAcl(String acl) { this.acl = acl; return this; }
public String getGrantRead() { return grantRead; }
public CopyObjectOptions withGrantRead(String grantRead) { this.grantRead = grantRead; return this; }
public String getGrantWrite() { return grantWrite; }
public CopyObjectOptions withGrantWrite(String grantWrite) { this.grantWrite = grantWrite; return this; }
public String getGrantFullControl() { return grantFullControl; }
public CopyObjectOptions withGrantFullControl(String grantFullControl) { this.grantFullControl = grantFullControl; return this; }
public String getGrantReadAcp() { return grantReadAcp; }
public CopyObjectOptions withGrantReadAcp(String grantReadAcp) { this.grantReadAcp = grantReadAcp; return this; }
public String getGrantWriteAcp() { return grantWriteAcp; }
public CopyObjectOptions withGrantWriteAcp(String grantWriteAcp) { this.grantWriteAcp = grantWriteAcp; return this; }
public String getChecksumAlgorithm() { return checksumAlgorithm; }
public CopyObjectOptions withChecksumAlgorithm(String checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; return this; }
}
@@ -16,6 +16,11 @@ public class PutObjectOptions {
private String sseCustomerKey;
private String sseCustomerKeyMd5;
private String acl;
private String grantRead;
private String grantWrite;
private String grantFullControl;
private String grantReadAcp;
private String grantWriteAcp;
private String checksumAlgorithm;
private String ifMatch;
private String ifNoneMatch;
@@ -57,6 +62,21 @@ public class PutObjectOptions {
public String getAcl() { return acl; }
public PutObjectOptions withAcl(String acl) { this.acl = acl; return this; }
public String getGrantRead() { return grantRead; }
public PutObjectOptions withGrantRead(String grantRead) { this.grantRead = grantRead; return this; }
public String getGrantWrite() { return grantWrite; }
public PutObjectOptions withGrantWrite(String grantWrite) { this.grantWrite = grantWrite; return this; }
public String getGrantFullControl() { return grantFullControl; }
public PutObjectOptions withGrantFullControl(String grantFullControl) { this.grantFullControl = grantFullControl; return this; }
public String getGrantReadAcp() { return grantReadAcp; }
public PutObjectOptions withGrantReadAcp(String grantReadAcp) { this.grantReadAcp = grantReadAcp; return this; }
public String getGrantWriteAcp() { return grantWriteAcp; }
public PutObjectOptions withGrantWriteAcp(String grantWriteAcp) { this.grantWriteAcp = grantWriteAcp; return this; }
public String getChecksumAlgorithm() { return checksumAlgorithm; }
public PutObjectOptions withChecksumAlgorithm(String checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; return this; }