mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): match a bucket policy's Principal the way IAM does in S3's own checks (#4961)
Follow-up from the reviews of #4838 and #4839. S3 checks a bucket policy itself in two places, and neither matched its Principal the way the IAM evaluator does since #4839: - The signed-request check (s3.enforce-auth) passed the caller to the IAM evaluator as a bare ARN, so a role session was matched through a role ARN rebuilt from the session ARN, which drops the role's path. A bucket policy naming role/team/App never reached that role's sessions. - The CopyObject/UploadPartCopy source check used S3's own matcher, which compared each AWS entry with the caller's ARN as a string. A grant to the caller's account, by id or root ARN, or to its role, never matched, so a cross-account caller allowed to read an object could not copy it. Both now pass the typed caller to the IAM evaluator, a role session with the ARN of the role it assumed, through a RequestPrincipal.caller factory the enforcement filter now uses too. The copy check's own matcher is gone: its direct-grant rules (an exact user ARN, or "*" with aws:PrincipalArn pinned to it) are the IAM evaluator's, and the unit tests that pinned them there move to IamPolicyEvaluatorTest. S3PublicAccessEvaluator keeps its principal matching for CloudFront's origin access identity check. A CanonicalUser entry matched nothing since #4839, and docs/services/iam.md described that as AWS behaviour. The IAM User Guide ("AWS account principals") lets an S3 bucket policy name an account by its canonical user ID, which the S3 User Guide describes as an obfuscated form of the account ID. It now matches the IAM identities of that account, as an account principal does. Floci's canonical ID for an account is its account id, the owner ID its S3 ACL responses report. Integration tests (IAM and S3 enforcement, real SigV4) read and copy across accounts under bucket policies naming the caller's account, a role under a path, and the account's canonical ID; each fails on main. docs/services/iam.md and docs/services/s3.md describe the matching.
This commit is contained in:
1 parent
8581497b0a
commit
b679fe2312
10 files changed
+200
-279
No files matched your search
@@ -686,7 +686,8 @@ class IamPolicyEvaluatorTest {
|
||||
String serviceWildcard = resourcePolicy("Allow", "\"Principal\":{\"Service\":\"*\"}");
|
||||
assertEquals(ResourcePolicyDecision.NEUTRAL, resourceDecision(serviceWildcard, alice));
|
||||
assertEquals(ResourcePolicyDecision.NEUTRAL, resourceDecision(serviceWildcard, sns));
|
||||
// Federated and CanonicalUser name callers of their own kinds, never an IAM identity.
|
||||
// Federated names callers of its own kind, never an IAM identity; CanonicalUser names one
|
||||
// account by its canonical ID, never every account.
|
||||
assertEquals(ResourcePolicyDecision.NEUTRAL,
|
||||
resourceDecision(resourcePolicy("Allow", "\"Principal\":{\"Federated\":\"*\"}"), alice));
|
||||
assertEquals(ResourcePolicyDecision.NEUTRAL,
|
||||
@@ -738,6 +739,52 @@ class IamPolicyEvaluatorTest {
|
||||
sns, "sqs:SendMessage", "*", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCanonicalUserNamesTheIamIdentitiesOfItsAccount() {
|
||||
// S3 bucket policies can name an account by its canonical user ID (IAM User Guide, "AWS
|
||||
// account principals"); Floci's canonical ID for an account is the account id.
|
||||
String canonical = resourcePolicy("Allow", "\"Principal\":{\"CanonicalUser\":\"123456789012\"}");
|
||||
assertEquals(ResourcePolicyDecision.ALLOW,
|
||||
resourceDecision(canonical, RequestPrincipal.iam("arn:aws:iam::123456789012:user/alice")));
|
||||
assertEquals(ResourcePolicyDecision.ALLOW, resourceDecision(canonical, RequestPrincipal.roleSession(
|
||||
"arn:aws:sts::123456789012:assumed-role/App/s", "arn:aws:iam::123456789012:role/team/App")));
|
||||
assertEquals(ResourcePolicyDecision.NEUTRAL,
|
||||
resourceDecision(canonical, RequestPrincipal.iam("arn:aws:iam::210987654321:user/alice")));
|
||||
assertEquals(ResourcePolicyDecision.NEUTRAL,
|
||||
resourceDecision(canonical, RequestPrincipal.service("sns.amazonaws.com")));
|
||||
assertEquals(ResourcePolicyDecision.NEUTRAL, resourceDecision(canonical, RequestPrincipal.anonymous()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyAGrantNamingTheUserExactlyIsDirect() {
|
||||
// A grant names a user directly by its exact ARN, or by "*" with aws:PrincipalArn pinned to
|
||||
// it. One that reaches the user through a pattern is not direct, and a Deny still wins.
|
||||
RequestPrincipal alice = RequestPrincipal.iam("arn:aws:iam::123456789012:user/alice");
|
||||
Map<String, List<String>> ctx = Map.of("aws:PrincipalArn", List.of(alice.arn()));
|
||||
String exact = resourcePolicy("Allow", "\"Principal\":{\"AWS\":\"arn:aws:iam::123456789012:user/alice\"}");
|
||||
String pinned = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"*","Resource":"*",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"arn:aws:iam::123456789012:user/alice"}}}]}""";
|
||||
String pattern = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"*","Resource":"*",
|
||||
"Condition":{"ArnLike":{"aws:PrincipalArn":"arn:aws:iam::123456789012:user/*"}}}]}""";
|
||||
String pinnedDeny = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Principal":"*","Action":"*","Resource":"*",
|
||||
"Condition":{"StringEquals":{"aws:PrincipalArn":"arn:aws:iam::123456789012:user/alice"}}},
|
||||
{"Effect":"Deny","Principal":"*","Action":"*","Resource":"*",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"arn:aws:iam::123456789012:user/alice"}}}]}""";
|
||||
|
||||
assertEquals(ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER,
|
||||
evaluator.evaluateResourcePolicyFor(List.of(exact), alice, "s3:GetObject", "*", ctx));
|
||||
assertEquals(ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER,
|
||||
evaluator.evaluateResourcePolicyFor(List.of(pinned), alice, "s3:GetObject", "*", ctx));
|
||||
assertEquals(ResourcePolicyDecision.ALLOW,
|
||||
evaluator.evaluateResourcePolicyFor(List.of(pattern), alice, "s3:GetObject", "*", ctx));
|
||||
assertEquals(ResourcePolicyDecision.EXPLICIT_DENY,
|
||||
evaluator.evaluateResourcePolicyFor(List.of(pinnedDeny), alice, "s3:GetObject", "*", ctx));
|
||||
}
|
||||
|
||||
private static String resourcePolicy(String effect, String principal) {
|
||||
return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"" + effect + "\"," + principal
|
||||
+ ",\"Action\":\"*\",\"Resource\":\"*\"}]}";
|
||||
|
||||
+92
@@ -241,6 +241,98 @@ class S3BucketPolicyEnforcementIntegrationTest {
|
||||
.then().statusCode(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBucketPolicyNamingTheCallersAccountGrantsACrossAccountCopy() {
|
||||
// The CopyObject source check matched a bucket policy's Principal against the caller's ARN as
|
||||
// a string, so a grant to the caller's account let it read the object but not copy it.
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String source = "bp-account-" + suffix;
|
||||
S3RequestSigner owner = sourceBucketInAccountA(source);
|
||||
S3RequestSigner reader = createAccountAdmin("reader-" + suffix, ACCOUNT_B).signer();
|
||||
String destination = "bp-account-dst-" + suffix;
|
||||
createBucketAs(destination, reader);
|
||||
|
||||
allowGetObject(owner, source, "{\"AWS\":\"" + ACCOUNT_B + "\"}");
|
||||
|
||||
assertReadAndCopy(reader, source, destination, 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBucketPolicyNamingARoleWithAPathGrantsItsSessions() {
|
||||
// S3's own checks matched a role session through a role ARN rebuilt from the session ARN,
|
||||
// which drops the role's path, so a grant naming role/team/<name> never reached it.
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String source = "bp-path-" + suffix;
|
||||
S3RequestSigner owner = sourceBucketInAccountA(source);
|
||||
S3RequestSigner session = sessionOfRoleUnderTeamPath("bp-path-role-" + suffix, ACCOUNT_B);
|
||||
String destination = "bp-path-dst-" + suffix;
|
||||
createBucketAs(destination, createAccountAdmin("dst-owner-" + suffix, ACCOUNT_B).signer());
|
||||
|
||||
allowGetObject(owner, source, "{\"AWS\":\"arn:aws:iam::" + ACCOUNT_B + ":role/team/bp-path-role-" + suffix + "\"}");
|
||||
|
||||
assertReadAndCopy(session, source, destination, 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBucketPolicyNamingAnAccountByItsCanonicalIdGrantsThatAccount() {
|
||||
// A bucket policy can name an account by its S3 canonical user ID (IAM User Guide, "AWS account
|
||||
// principals"), and Floci's canonical ID for an account is its account id.
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String source = "bp-canonical-" + suffix;
|
||||
S3RequestSigner owner = sourceBucketInAccountA(source);
|
||||
S3RequestSigner reader = createAccountAdmin("reader-" + suffix, ACCOUNT_B).signer();
|
||||
String destination = "bp-canonical-dst-" + suffix;
|
||||
createBucketAs(destination, reader);
|
||||
|
||||
allowGetObject(owner, source, "{\"CanonicalUser\":\"999988887777\"}");
|
||||
assertReadAndCopy(reader, source, destination, 403);
|
||||
|
||||
allowGetObject(owner, source, "{\"CanonicalUser\":\"" + ACCOUNT_B + "\"}");
|
||||
assertReadAndCopy(reader, source, destination, 200);
|
||||
}
|
||||
|
||||
/** Creates a bucket in account A holding {@code o.txt}, and returns its owner, an account-A administrator. */
|
||||
private static S3RequestSigner sourceBucketInAccountA(String bucket) {
|
||||
S3RequestSigner owner = createAccountAdmin("owner-" + bucket, ACCOUNT_A).signer();
|
||||
createBucketAs(bucket, owner);
|
||||
putObjectAs(bucket, "o.txt", "payload", owner);
|
||||
return owner;
|
||||
}
|
||||
|
||||
private static void allowGetObject(S3RequestSigner owner, String bucket, String principal) {
|
||||
putBucketPolicyAs(bucket, """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":%s,
|
||||
"Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*"}]}""".formatted(principal, bucket), owner);
|
||||
}
|
||||
|
||||
private static void assertReadAndCopy(S3RequestSigner caller, String source, String destination, int status) {
|
||||
given().filter(caller).when().get("/" + source + "/o.txt").then().statusCode(status);
|
||||
given().filter(caller).header("x-amz-copy-source", "/" + source + "/o.txt")
|
||||
.when().put("/" + destination + "/" + UUID.randomUUID() + ".txt").then().statusCode(status);
|
||||
}
|
||||
|
||||
/** A session of a role under the {@code /team/} path, allowed s3:* by its own policy. */
|
||||
private static S3RequestSigner sessionOfRoleUnderTeamPath(String role, String accountId) {
|
||||
given().formParam("Action", "CreateRole").formParam("RoleName", role).formParam("Path", "/team/")
|
||||
.formParam("AssumeRolePolicyDocument", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"AWS":"arn:aws:iam::%s:root"},"Action":"sts:AssumeRole"}]}""".formatted(accountId))
|
||||
.header("Authorization", auth(accountId, "iam")).when().post("/").then().statusCode(200);
|
||||
given().formParam("Action", "PutRolePolicy").formParam("RoleName", role).formParam("PolicyName", "s3")
|
||||
.formParam("PolicyDocument", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:*","Resource":"*"}]}""")
|
||||
.header("Authorization", auth(accountId, "iam")).when().post("/").then().statusCode(200);
|
||||
ExtractableResponse<Response> assumed = given().formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", "arn:aws:iam::" + accountId + ":role/team/" + role)
|
||||
.formParam("RoleSessionName", "s")
|
||||
.header("Authorization", auth(accountId, "sts")).when().post("/")
|
||||
.then().statusCode(200).extract();
|
||||
return S3RequestSigner.signedAs(
|
||||
assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId"),
|
||||
assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.SecretAccessKey"),
|
||||
assumed.path("AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void enforcesBucketPolicyForSignedCallers() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
-131
@@ -1,11 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.services.s3.S3PublicAccessEvaluator.PublicAccessDecision.ALLOW;
|
||||
@@ -167,134 +164,6 @@ class S3PublicAccessEvaluatorTest {
|
||||
"s3:GetObject", OBJECT_ARN, Map.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactAwsPrincipalIsReportedAsADirectGrant() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":{"AWS":"%s"},
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""".formatted(userArn);
|
||||
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of());
|
||||
|
||||
assertEquals(ALLOW, evaluation.decision());
|
||||
assertTrue(evaluation.directPrincipalAllow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactPrincipalArnConditionIsReportedAsADirectGrant() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%s"}}
|
||||
}}""".formatted(userArn);
|
||||
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of("aws:PrincipalArn", userArn));
|
||||
|
||||
assertEquals(ALLOW, evaluation.decision());
|
||||
assertTrue(evaluation.directPrincipalAllow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactPrincipalArnConditionBypassesBoundaryImplicitDeny() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%s"}}
|
||||
}}""".formatted(userArn);
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of("aws:PrincipalArn", userArn));
|
||||
IamPolicyEvaluator.ResourcePolicyDecision resourceDecision =
|
||||
evaluation.directPrincipalAllow()
|
||||
? IamPolicyEvaluator.ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER
|
||||
: IamPolicyEvaluator.ResourcePolicyDecision.ALLOW;
|
||||
CallerContext caller = new CallerContext(
|
||||
List.of(), null,
|
||||
"""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"dynamodb:*","Resource":"*"}
|
||||
]}""");
|
||||
|
||||
assertEquals(IamPolicyEvaluator.Decision.ALLOW,
|
||||
new IamPolicyEvaluator(OBJECT_MAPPER).evaluateResolvedResourcePolicy(
|
||||
caller,
|
||||
resourceDecision,
|
||||
IamPolicyEvaluator.ResourceAccountRelationship.SAME_ACCOUNT,
|
||||
"s3:GetObject",
|
||||
OBJECT_ARN,
|
||||
Map.of("aws:PrincipalArn", List.of(userArn))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalArnConditionIsNotReportedAsADirectGrant() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":{"AWS":"*"},
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"ArnLike":{"aws:PrincipalArn":"arn:aws:iam::123456789012:user/*"}}
|
||||
}}""";
|
||||
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of("aws:PrincipalArn", userArn));
|
||||
|
||||
assertEquals(ALLOW, evaluation.decision());
|
||||
assertFalse(evaluation.directPrincipalAllow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchingDenyOverridesExactPrincipalArnCondition() {
|
||||
String userArn = "arn:aws:iam::123456789012:user/alice";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"StringEquals":{"aws:PrincipalArn":"%s"}}
|
||||
},
|
||||
{
|
||||
"Effect":"Deny",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%s"}}
|
||||
}
|
||||
]}""".formatted(userArn, userArn);
|
||||
|
||||
S3PublicAccessEvaluator.PrincipalPolicyEvaluation evaluation =
|
||||
S3PublicAccessEvaluator.principalPolicyEvaluation(
|
||||
OBJECT_MAPPER, policy, "AWS", userArn,
|
||||
"s3:GetObject", OBJECT_ARN, Map.of("aws:PrincipalArn", userArn));
|
||||
|
||||
assertEquals(DENY, evaluation.decision());
|
||||
assertFalse(evaluation.directPrincipalAllow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void principalConditionalDenyOnlyAppliesWhenConditionMatches() {
|
||||
String policy = """
|
||||
|
||||
Reference in new issue
Block a user