mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(s3): refuse a canned ACL sent together with grant headers (#6919)
* fix(s3): refuse a canned ACL sent together with grant headers PutBucketAcl, PutObjectAcl, PutObject, CopyObject and CreateMultipartUpload now answer 400 InvalidRequest, as S3 does, when x-amz-acl and any x-amz-grant-* header are both set, instead of applying the canned ACL and dropping the grants. The check moves from CreateBucket into the shared ACL header resolver so every operation is covered; blank values still count as absent. Fixes #5826 * docs(s3): note that a canned ACL and grant headers are refused together * refactor(s3): share the grant-header check and test header list Compute one hasGrants flag in resolveObjectAclXml for both the canned-ACL refusal and the no-ACL fallback, and list the five grant headers once in a @MethodSource.
This commit is contained in:
1 parent
cc76085322
commit
804b503a33
3 files changed
+243
-11
No files matched your search
@@ -456,6 +456,10 @@ When a request is denied by bucket policy enforcement, Floci returns HTTP 403 wi
|
||||
|
||||
New buckets start with `BucketOwnerEnforced` ownership, matching the current S3 default. `GetBucketOwnershipControls` returns that setting immediately after `CreateBucket`. Bucket and object ACL writes are disabled, while uploads without an ACL and uploads using `bucket-owner-full-control` remain supported. To use ACL grants, set `x-amz-object-ownership` to `ObjectWriter` or `BucketOwnerPreferred` during `CreateBucket`, or change the mode later with `PutBucketOwnershipControls`. Existing stored buckets keep their previously configured ownership mode.
|
||||
|
||||
### Canned ACLs and grant headers
|
||||
|
||||
A request sets its ACL either with a canned ACL (`x-amz-acl`) or with grant headers (`x-amz-grant-*`), not both. As on AWS, `CreateBucket`, `PutBucketAcl`, `PutObjectAcl`, `PutObject`, `CopyObject` and `CreateMultipartUpload` refuse a request carrying both with `InvalidRequest` (400), `Specifying both Canned ACLs and Header Grants is not allowed`, and change nothing. A header with a blank value counts as absent.
|
||||
|
||||
## Block Public Access
|
||||
|
||||
Floci enforces all four Block Public Access settings. A bucket's configuration is combined with the bucket owner account's and the most restrictive of the two wins, which for four independent booleans is a per-flag OR: a flag set at either level is in force.
|
||||
|
||||
@@ -496,10 +496,6 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
if (!OBJECT_OWNERSHIP_VALUES.contains(ownership)) {
|
||||
throw new AwsException("InvalidArgument", "Invalid x-amz-object-ownership value: " + ownership, 400);
|
||||
}
|
||||
if (!isBlank(cannedAcl) && (!isBlank(grantRead) || !isBlank(grantWrite)
|
||||
|| !isBlank(grantFullControl) || !isBlank(grantReadAcp) || !isBlank(grantWriteAcp))) {
|
||||
throw new AwsException("InvalidRequest", "Specifying both Canned ACLs and Header Grants is not allowed", 400);
|
||||
}
|
||||
String requestedAcl = resolveObjectAclXml(cannedAcl, grantRead, grantWrite,
|
||||
grantFullControl, grantReadAcp, grantWriteAcp);
|
||||
if ("BucketOwnerEnforced".equals(ownership)
|
||||
@@ -6578,10 +6574,10 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
/**
|
||||
* 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).
|
||||
* x-amz-grant-read-acp, x-amz-grant-write-acp). A canned ACL together with any grant header is
|
||||
* refused with 400 InvalidRequest, as S3 does; blank values count as absent. 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) {
|
||||
@@ -6593,11 +6589,15 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
String grantRead, String grantWrite,
|
||||
String grantFullControl, String grantReadAcp,
|
||||
String grantWriteAcp) {
|
||||
if (cannedAcl != null && !cannedAcl.isBlank()) {
|
||||
boolean hasGrants = !isBlank(grantRead) || !isBlank(grantWrite) || !isBlank(grantFullControl)
|
||||
|| !isBlank(grantReadAcp) || !isBlank(grantWriteAcp);
|
||||
if (!isBlank(cannedAcl) && hasGrants) {
|
||||
throw new AwsException("InvalidRequest", "Specifying both Canned ACLs and Header Grants is not allowed", 400);
|
||||
}
|
||||
if (!isBlank(cannedAcl)) {
|
||||
return cannedObjectAclXml(ownerAccount, cannedAcl);
|
||||
}
|
||||
if (isBlank(grantRead) && isBlank(grantWrite) && isBlank(grantFullControl)
|
||||
&& isBlank(grantReadAcp) && isBlank(grantWriteAcp)) {
|
||||
if (!hasGrants) {
|
||||
return null;
|
||||
}
|
||||
List<String> grants = new ArrayList<>();
|
||||
|
||||
+228
@@ -0,0 +1,228 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.BucketCannedACL;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectAclResponse;
|
||||
import software.amazon.awssdk.services.s3.model.Grant;
|
||||
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
|
||||
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
|
||||
import software.amazon.awssdk.services.s3.model.ObjectOwnership;
|
||||
import software.amazon.awssdk.services.s3.model.Permission;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* S3 refuses a request that sets a canned ACL ({@code x-amz-acl}) together with any
|
||||
* {@code x-amz-grant-*} header, on every operation that takes ACL headers.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class S3AclHeaderValidationIntegrationTest {
|
||||
|
||||
private static final String MESSAGE = "Specifying both Canned ACLs and Header Grants is not allowed";
|
||||
private static final String GRANTEE_ID = "123456789012";
|
||||
private static final String GRANTEE = "id=\"" + GRANTEE_ID + "\"";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI baseUri;
|
||||
|
||||
private S3Client s3;
|
||||
private String bucket;
|
||||
|
||||
@BeforeEach
|
||||
void createBucket() {
|
||||
s3 = S3Client.builder()
|
||||
.endpointOverride(baseUri)
|
||||
.region(Region.US_EAST_1)
|
||||
.forcePathStyle(true)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||
.build();
|
||||
bucket = "acl-header-validation-" + UUID.randomUUID();
|
||||
s3.createBucket(request -> request.bucket(bucket).objectOwnership(ObjectOwnership.OBJECT_WRITER));
|
||||
s3.putObject(request -> request.bucket(bucket).key("existing"), RequestBody.fromString("existing"));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void closeClient() {
|
||||
s3.close();
|
||||
}
|
||||
|
||||
static Stream<String> grantHeaders() {
|
||||
return Stream.of("x-amz-grant-read", "x-amz-grant-write", "x-amz-grant-full-control",
|
||||
"x-amz-grant-read-acp", "x-amz-grant-write-acp");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("grantHeaders")
|
||||
void putBucketAclRefusesCannedAclWithGrantHeader(String grantHeader) {
|
||||
List<Grant> before = s3.getBucketAcl(request -> request.bucket(bucket)).grants();
|
||||
|
||||
assertRefused(() -> s3.putBucketAcl(request -> request.bucket(bucket)
|
||||
.acl(BucketCannedACL.PRIVATE)
|
||||
.overrideConfiguration(o -> o.putHeader(grantHeader, GRANTEE))));
|
||||
|
||||
assertEquals(before, s3.getBucketAcl(request -> request.bucket(bucket)).grants());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("grantHeaders")
|
||||
void putObjectAclRefusesCannedAclWithGrantHeader(String grantHeader) {
|
||||
List<Grant> before = s3.getObjectAcl(request -> request.bucket(bucket).key("existing")).grants();
|
||||
|
||||
assertRefused(() -> s3.putObjectAcl(request -> request.bucket(bucket).key("existing")
|
||||
.acl(ObjectCannedACL.PRIVATE)
|
||||
.overrideConfiguration(o -> o.putHeader(grantHeader, GRANTEE))));
|
||||
|
||||
assertEquals(before, s3.getObjectAcl(request -> request.bucket(bucket).key("existing")).grants());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("grantHeaders")
|
||||
void putObjectRefusesCannedAclWithGrantHeader(String grantHeader) {
|
||||
assertRefused(() -> s3.putObject(request -> request.bucket(bucket).key("refused")
|
||||
.acl(ObjectCannedACL.PRIVATE)
|
||||
.overrideConfiguration(o -> o.putHeader(grantHeader, GRANTEE)),
|
||||
RequestBody.fromString("refused")));
|
||||
|
||||
assertObjectAbsent("refused");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("grantHeaders")
|
||||
void copyObjectRefusesCannedAclWithGrantHeader(String grantHeader) {
|
||||
assertRefused(() -> s3.copyObject(request -> request
|
||||
.sourceBucket(bucket).sourceKey("existing")
|
||||
.destinationBucket(bucket).destinationKey("copy")
|
||||
.acl(ObjectCannedACL.PRIVATE)
|
||||
.overrideConfiguration(o -> o.putHeader(grantHeader, GRANTEE))));
|
||||
|
||||
assertObjectAbsent("copy");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("grantHeaders")
|
||||
void createMultipartUploadRefusesCannedAclWithGrantHeader(String grantHeader) {
|
||||
assertRefused(() -> s3.createMultipartUpload(request -> request.bucket(bucket).key("multipart")
|
||||
.acl(ObjectCannedACL.PRIVATE)
|
||||
.overrideConfiguration(o -> o.putHeader(grantHeader, GRANTEE))));
|
||||
|
||||
assertThat(s3.listMultipartUploads(request -> request.bucket(bucket)).uploads(), empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObjectRefusesTypedCannedAclAndGrantFields() {
|
||||
assertRefused(() -> s3.putObject(request -> request.bucket(bucket).key("typed")
|
||||
.acl(ObjectCannedACL.BUCKET_OWNER_READ)
|
||||
.grantRead(GRANTEE),
|
||||
RequestBody.fromString("typed")));
|
||||
|
||||
assertObjectAbsent("typed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannedAclAloneIsStillApplied() {
|
||||
s3.putObject(request -> request.bucket(bucket).key("canned").acl(ObjectCannedACL.BUCKET_OWNER_READ),
|
||||
RequestBody.fromString("canned"));
|
||||
s3.putObjectAcl(request -> request.bucket(bucket).key("existing").acl(ObjectCannedACL.PRIVATE));
|
||||
s3.putBucketAcl(request -> request.bucket(bucket).acl(BucketCannedACL.PRIVATE));
|
||||
|
||||
assertEquals("canned", s3.getObjectAsBytes(request -> request.bucket(bucket).key("canned")).asUtf8String());
|
||||
}
|
||||
|
||||
@Test
|
||||
void grantsAloneAreStillApplied() {
|
||||
s3.putObject(request -> request.bucket(bucket).key("granted").grantRead(GRANTEE),
|
||||
RequestBody.fromString("granted"));
|
||||
assertGrantedRead("granted");
|
||||
|
||||
s3.putObjectAcl(request -> request.bucket(bucket).key("existing").grantRead(GRANTEE));
|
||||
assertGrantedRead("existing");
|
||||
|
||||
s3.putBucketAcl(request -> request.bucket(bucket).grantRead(GRANTEE));
|
||||
assertThat(s3.getBucketAcl(request -> request.bucket(bucket)).grants().stream()
|
||||
.filter(grant -> grant.permission() == Permission.READ)
|
||||
.map(grant -> grant.grantee().id())
|
||||
.toList(), hasItem(GRANTEE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankGrantHeaderWithCannedAclIsAccepted() {
|
||||
given().header("x-amz-acl", "private").header("x-amz-grant-read", "")
|
||||
.body("blank grant")
|
||||
.when().put("/" + bucket + "/blank-grant")
|
||||
.then().statusCode(200);
|
||||
given().header("x-amz-acl", "private").header("x-amz-grant-full-control", " ")
|
||||
.when().put("/" + bucket + "?acl")
|
||||
.then().statusCode(200);
|
||||
|
||||
assertThat(granteeIds("blank-grant"), not(hasItem(GRANTEE_ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankCannedAclWithGrantHeaderAppliesTheGrant() {
|
||||
given().header("x-amz-acl", "").header("x-amz-grant-read", GRANTEE)
|
||||
.body("blank canned")
|
||||
.when().put("/" + bucket + "/blank-canned")
|
||||
.then().statusCode(200);
|
||||
|
||||
assertGrantedRead("blank-canned");
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusalIsAnS3InvalidRequestError() {
|
||||
given().header("x-amz-acl", "private").header("x-amz-grant-write-acp", GRANTEE)
|
||||
.when().put("/" + bucket + "/existing?acl")
|
||||
.then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidRequest</Code>"))
|
||||
.body(containsString("<Message>" + MESSAGE + "</Message>"));
|
||||
}
|
||||
|
||||
private static void assertRefused(Runnable call) {
|
||||
S3Exception error = assertThrows(S3Exception.class, call::run);
|
||||
assertEquals(400, error.statusCode());
|
||||
assertEquals("InvalidRequest", error.awsErrorDetails().errorCode());
|
||||
assertEquals(MESSAGE, error.awsErrorDetails().errorMessage());
|
||||
}
|
||||
|
||||
private void assertObjectAbsent(String key) {
|
||||
assertThrows(NoSuchKeyException.class,
|
||||
() -> s3.getObject(request -> request.bucket(bucket).key(key)).close());
|
||||
}
|
||||
|
||||
private void assertGrantedRead(String key) {
|
||||
GetObjectAclResponse acl = s3.getObjectAcl(request -> request.bucket(bucket).key(key));
|
||||
assertThat(acl.grants().stream()
|
||||
.filter(grant -> grant.permission() == Permission.READ)
|
||||
.map(grant -> grant.grantee().id())
|
||||
.toList(), hasItem(GRANTEE_ID));
|
||||
}
|
||||
|
||||
private List<String> granteeIds(String key) {
|
||||
return s3.getObjectAcl(request -> request.bucket(bucket).key(key)).grants().stream()
|
||||
.map(grant -> grant.grantee().id())
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user