mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(s3): enforce source resource policies on object copies (#3539)
* fix(s3): enforce source resource policies on object copies * fix(s3): combine source identity and resource policies --------- Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
1 parent
0f7df0fd19
commit
19917ed9ed
7 files changed
+423
-19
No files matched your search
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;
|
||||
import io.github.hectorvent.floci.services.iam.IamActionRegistry;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
|
||||
import io.github.hectorvent.floci.services.iam.ScpProvider;
|
||||
@@ -44,7 +45,7 @@ import java.util.regex.Pattern;
|
||||
*
|
||||
* <p>Evaluates the caller's identity policies, optional session policy, and optional
|
||||
* permissions boundary. Resource-based policies (S3 bucket policy, Lambda resource
|
||||
* policy, etc.) are not yet supplied to this filter.
|
||||
* policy, etc.) are not yet supplied to the primary request-filter path.
|
||||
*
|
||||
* <p>Reads the signing credential from either the {@code Authorization} header or, for a
|
||||
* presigned URL, the {@code X-Amz-Credential} query parameter - both request shapes get the
|
||||
@@ -252,6 +253,20 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
* ambient account, resolve the credential's actual owner instead of the default account.
|
||||
*/
|
||||
public void authorizeAdditionalResource(String authorizationHeader, String action, String resource) {
|
||||
authorizeAdditionalResource(
|
||||
authorizationHeader, action, resource, ResourcePolicyDecision.NEUTRAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorizes a secondary resource using an already principal-filtered resource-policy
|
||||
* decision. This preserves explicit-deny precedence while allowing either the identity or
|
||||
* resource policy to provide the base grant.
|
||||
*/
|
||||
public void authorizeAdditionalResource(
|
||||
String authorizationHeader,
|
||||
String action,
|
||||
String resource,
|
||||
ResourcePolicyDecision resourcePolicyDecision) {
|
||||
if (!config.services().iam().enforcementEnabled()) {
|
||||
return;
|
||||
}
|
||||
@@ -295,7 +310,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
conditionContext.put("aws:PrincipalArn", List.of(principalArn.get()));
|
||||
}
|
||||
|
||||
Decision decision = evaluator.evaluate(caller, null, action, resource, conditionContext);
|
||||
Decision decision = evaluator.evaluateResolvedResourcePolicy(
|
||||
caller, resourcePolicyDecision, action, resource, conditionContext);
|
||||
if (decision != Decision.DENY) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -40,6 +40,12 @@ public class IamPolicyEvaluator {
|
||||
|
||||
public enum Decision { ALLOW, DENY }
|
||||
|
||||
public enum ResourcePolicyDecision {
|
||||
ALLOW,
|
||||
EXPLICIT_DENY,
|
||||
NEUTRAL
|
||||
}
|
||||
|
||||
public enum SimulationDecision {
|
||||
ALLOWED("allowed"),
|
||||
EXPLICIT_DENY("explicitDeny"),
|
||||
@@ -89,6 +95,49 @@ public class IamPolicyEvaluator {
|
||||
List<PolicyStatement> boundaryStmts = caller.boundaryPolicyDocument() == null
|
||||
? null : parseAll(List.of(caller.boundaryPolicyDocument()));
|
||||
|
||||
boolean resourceExplicitDeny = anyExplicitDeny(resourceStmts, action, resource, ctx);
|
||||
boolean resourceAllow = anyExplicitAllow(resourceStmts, action, resource, ctx);
|
||||
return evaluateParsed(
|
||||
caller, identityStmts, sessionStmts, boundaryStmts,
|
||||
resourceExplicitDeny, resourceAllow, action, resource, ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates an already principal-filtered resource-policy decision together with the
|
||||
* caller's identity policies and permission ceilings.
|
||||
*/
|
||||
public Decision evaluateResolvedResourcePolicy(
|
||||
CallerContext caller,
|
||||
ResourcePolicyDecision resourcePolicyDecision,
|
||||
String action,
|
||||
String resource,
|
||||
Map<String, List<String>> conditionCtx) {
|
||||
Map<String, List<String>> ctx = normalizeConditionContext(conditionCtx);
|
||||
List<PolicyStatement> identityStmts = parseAll(caller.identityPolicies());
|
||||
List<PolicyStatement> sessionStmts = caller.sessionPolicyDocument() == null
|
||||
? null : parseAll(List.of(caller.sessionPolicyDocument()));
|
||||
List<PolicyStatement> boundaryStmts = caller.boundaryPolicyDocument() == null
|
||||
? null : parseAll(List.of(caller.boundaryPolicyDocument()));
|
||||
ResourcePolicyDecision resolvedDecision = resourcePolicyDecision == null
|
||||
? ResourcePolicyDecision.NEUTRAL : resourcePolicyDecision;
|
||||
return evaluateParsed(
|
||||
caller, identityStmts, sessionStmts, boundaryStmts,
|
||||
resolvedDecision == ResourcePolicyDecision.EXPLICIT_DENY,
|
||||
resolvedDecision == ResourcePolicyDecision.ALLOW,
|
||||
action, resource, ctx);
|
||||
}
|
||||
|
||||
private Decision evaluateParsed(
|
||||
CallerContext caller,
|
||||
List<PolicyStatement> identityStmts,
|
||||
List<PolicyStatement> sessionStmts,
|
||||
List<PolicyStatement> boundaryStmts,
|
||||
boolean resourceExplicitDeny,
|
||||
boolean resourceAllow,
|
||||
String action,
|
||||
String resource,
|
||||
Map<String, List<String>> ctx) {
|
||||
|
||||
// 0. Service control policies gate everything: the action must be allowed at EVERY
|
||||
// organization level and explicitly denied at none, before identity policies are
|
||||
// even consulted. An empty level means FullAWSAccess semantics and is skipped;
|
||||
@@ -99,7 +148,7 @@ public class IamPolicyEvaluator {
|
||||
|
||||
// 1. Explicit deny in ANY policy → DENY immediately
|
||||
if (anyExplicitDeny(identityStmts, action, resource, ctx)
|
||||
|| anyExplicitDeny(resourceStmts, action, resource, ctx)
|
||||
|| resourceExplicitDeny
|
||||
|| (sessionStmts != null && anyExplicitDeny(sessionStmts, action, resource, ctx))
|
||||
|| (boundaryStmts != null && anyExplicitDeny(boundaryStmts, action, resource, ctx))) {
|
||||
return Decision.DENY;
|
||||
@@ -107,7 +156,6 @@ public class IamPolicyEvaluator {
|
||||
|
||||
// 2. Base grant: identity OR resource-based policy must allow
|
||||
boolean identityAllow = anyExplicitAllow(identityStmts, action, resource, ctx);
|
||||
boolean resourceAllow = anyExplicitAllow(resourceStmts, action, resource, ctx);
|
||||
if (!identityAllow && !resourceAllow) {
|
||||
return Decision.DENY;
|
||||
}
|
||||
|
||||
@@ -2185,6 +2185,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
return Optional.empty();
|
||||
}
|
||||
String roleArn = session.getRoleArn();
|
||||
if (roleArn == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String roleName = roleArn.contains("/") ? roleArn.substring(roleArn.lastIndexOf('/') + 1) : "UnknownRole";
|
||||
String accountId = AwsArnUtils.accountOrDefault(roleArn, regionResolver.getAccountId());
|
||||
return Optional.of(AwsArnUtils.Arn.of("sts", "", accountId, "assumed-role/" + roleName + "/floci-session").toString());
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsQueryHandler;
|
||||
import io.github.hectorvent.floci.services.s3.model.Bucket;
|
||||
@@ -785,7 +786,8 @@ public class S3Controller {
|
||||
if (uploadId != null && partNumber != null) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
if (copySource != null && !copySource.isEmpty()) {
|
||||
return handleUploadPartCopy(copySource, bucket, key, uploadId, partNumber, httpHeaders);
|
||||
return handleUploadPartCopy(
|
||||
copySource, bucket, key, uploadId, partNumber, httpHeaders, authorization);
|
||||
}
|
||||
byte[] partData = decodeAwsChunked(body, contentEncoding, contentSha256);
|
||||
validateChecksumHeaders(httpHeaders, partData, getChecksumAlgorithm(httpHeaders));
|
||||
@@ -807,7 +809,7 @@ public class S3Controller {
|
||||
|
||||
if (copySource != null && !copySource.isEmpty()) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
return handleCopyObject(copySource, bucket, key, contentType, httpHeaders);
|
||||
return handleCopyObject(copySource, bucket, key, contentType, httpHeaders, authorization);
|
||||
}
|
||||
|
||||
Map<String, String> inlineTags = parseInlineTaggingHeader(tagging);
|
||||
@@ -2588,10 +2590,11 @@ public class S3Controller {
|
||||
// --- Helpers ---
|
||||
|
||||
private Response handleCopyObject(String copySource, String destBucket, String destKey,
|
||||
String contentType, HttpHeaders httpHeaders) {
|
||||
String contentType, HttpHeaders httpHeaders,
|
||||
S3Service.RequestAuthorization authorization) {
|
||||
CopySourceRef sourceObject = parseCopySource(copySource);
|
||||
String sourceBucket = sourceObject.bucket();
|
||||
authorizeCopySourceRead(httpHeaders, sourceBucket, sourceObject.objectKey());
|
||||
authorizeCopySourceRead(httpHeaders, sourceObject, authorization);
|
||||
String copyContentEncoding = toPersistedContentEncoding(httpHeaders.getHeaderString("Content-Encoding"));
|
||||
String copyContentDisposition = httpHeaders.getHeaderString("Content-Disposition");
|
||||
String copyCacheControl = httpHeaders.getHeaderString("Cache-Control");
|
||||
@@ -2668,10 +2671,11 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
private Response handleUploadPartCopy(String copySource, String destBucket, String destKey,
|
||||
String uploadId, int partNumber, HttpHeaders httpHeaders) {
|
||||
String uploadId, int partNumber, HttpHeaders httpHeaders,
|
||||
S3Service.RequestAuthorization authorization) {
|
||||
CopySourceRef sourceObject = parseCopySource(copySource);
|
||||
String sourceBucket = sourceObject.bucket();
|
||||
authorizeCopySourceRead(httpHeaders, sourceBucket, sourceObject.objectKey());
|
||||
authorizeCopySourceRead(httpHeaders, sourceObject, authorization);
|
||||
String copySourceRange = httpHeaders.getHeaderString("x-amz-copy-source-range");
|
||||
String eTag = s3Service.uploadPartCopy(destBucket, destKey, uploadId, partNumber,
|
||||
sourceBucket, sourceObject.objectKey(), sourceObject.versionId(), copySourceRange,
|
||||
@@ -3776,10 +3780,18 @@ public class S3Controller {
|
||||
* allowed to write to the destination bucket must not be able to exfiltrate an object it cannot read.
|
||||
* A no-op when IAM enforcement is disabled, matching {@link IamEnforcementFilter}'s own bypass rules.
|
||||
*/
|
||||
private void authorizeCopySourceRead(HttpHeaders httpHeaders, String sourceBucket, String sourceKey) {
|
||||
String resource = S3PublicAccessEvaluator.objectArn(sourceBucket, sourceKey);
|
||||
private void authorizeCopySourceRead(HttpHeaders httpHeaders, CopySourceRef source,
|
||||
S3Service.RequestAuthorization authorization) {
|
||||
String action = source.versionId() == null ? "s3:GetObject" : "s3:GetObjectVersion";
|
||||
String resource = S3PublicAccessEvaluator.objectArn(source.bucket(), source.objectKey());
|
||||
ResourcePolicyDecision resourcePolicyDecision =
|
||||
s3Service.signedPrincipalResourcePolicyDecision(
|
||||
source.bucket(), action, resource, authorization);
|
||||
iamEnforcementFilter.authorizeAdditionalResource(
|
||||
httpHeaders.getHeaderString("Authorization"), "s3:GetObject", resource);
|
||||
httpHeaders.getHeaderString("Authorization"), action, resource,
|
||||
resourcePolicyDecision);
|
||||
s3Service.authorizeGetObject(
|
||||
source.bucket(), source.objectKey(), source.versionId(), authorization);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
@@ -898,11 +899,13 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
: RequestAuthorization.unsigned();
|
||||
|
||||
if (requestAuthorization.signed()) {
|
||||
if (isKnownAccessKey(requestAuthorization)) {
|
||||
return;
|
||||
if (!isKnownAccessKey(requestAuthorization)) {
|
||||
throw new AwsException("InvalidAccessKeyId",
|
||||
"The AWS Access Key Id you provided does not exist in our records.", 403);
|
||||
}
|
||||
throw new AwsException("InvalidAccessKeyId",
|
||||
"The AWS Access Key Id you provided does not exist in our records.", 403);
|
||||
authorizeSignedPrincipalPolicyDeny(
|
||||
bucketName, action, resourceArn, requestAuthorization);
|
||||
return;
|
||||
}
|
||||
|
||||
Bucket bucket = bucketStore.get(bucketName)
|
||||
@@ -926,6 +929,52 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
throw new AwsException("AccessDenied", "Access Denied", 403);
|
||||
}
|
||||
|
||||
private void authorizeSignedPrincipalPolicyDeny(
|
||||
String bucketName,
|
||||
String action,
|
||||
String resourceArn,
|
||||
RequestAuthorization authorization) {
|
||||
if (signedPrincipalResourcePolicyDecision(
|
||||
bucketName, action, resourceArn, authorization)
|
||||
== ResourcePolicyDecision.EXPLICIT_DENY) {
|
||||
throw new AwsException("AccessDenied", "Access Denied", 403);
|
||||
}
|
||||
}
|
||||
|
||||
ResourcePolicyDecision signedPrincipalResourcePolicyDecision(
|
||||
String bucketName,
|
||||
String action,
|
||||
String resourceArn,
|
||||
RequestAuthorization authorization) {
|
||||
if (authorization == null || !authorization.signed()
|
||||
|| LEGACY_ACCESS_KEY_ID.equals(authorization.accessKeyId()) || iamService == null) {
|
||||
return ResourcePolicyDecision.NEUTRAL;
|
||||
}
|
||||
|
||||
Optional<String> principalArn = iamService.resolveCallerArn(authorization.accessKeyId());
|
||||
if (principalArn.isEmpty()) {
|
||||
return ResourcePolicyDecision.NEUTRAL;
|
||||
}
|
||||
|
||||
Bucket bucket = bucketStore.get(bucketName)
|
||||
.orElseThrow(() -> new AwsException(
|
||||
"NoSuchBucket", "The specified bucket does not exist.", 404));
|
||||
S3PublicAccessEvaluator.PublicAccessDecision policyDecision =
|
||||
S3PublicAccessEvaluator.principalPolicyDecision(
|
||||
objectMapper,
|
||||
bucket.getPolicy(),
|
||||
"AWS",
|
||||
principalArn.get(),
|
||||
action,
|
||||
resourceArn,
|
||||
Map.of("aws:PrincipalArn", principalArn.get()));
|
||||
return switch (policyDecision) {
|
||||
case ALLOW -> ResourcePolicyDecision.ALLOW;
|
||||
case DENY -> ResourcePolicyDecision.EXPLICIT_DENY;
|
||||
case NEUTRAL -> ResourcePolicyDecision.NEUTRAL;
|
||||
};
|
||||
}
|
||||
|
||||
private boolean readableObjectExists(String bucketName, String key) {
|
||||
ensureBucketExists(bucketName);
|
||||
return resolveObject(objectKey(bucketName, key))
|
||||
|
||||
Reference in new issue
Block a user