fix(s3): authorize read on copyobject/uploadpartcopy source (#3450)

CopyObject and UploadPartCopy only checked s3:PutObject on the
destination, letting a write-only caller copy any source object it
could not otherwise read via x-amz-copy-source.

* Fix #2333

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-09-11 23:02:37 -04:00
1 parent 35233787a8
commit bb58176974
3 files changed
+282 -1

No files matched your search

@@ -217,6 +217,76 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
}
}
/**
* Authorizes a single (action, resource) pair for the caller identified by an
* Authorization header, following the same identity resolution and bypass rules
* as {@link #filter}. Callers use this for a secondary resource that never appears
* in the request URL and so is invisible to {@link ResourceArnBuilder} - such as
* the CopyObject/UploadPartCopy source object, which arrives only in the
* {@code x-amz-copy-source} header.
*
* <p>Returns normally when the action is allowed, or when enforcement does not
* apply to this request (enforcement disabled, no Authorization header, root or
* unknown access key). Throws {@link AwsException} with the same AccessDenied
* shape as {@link #filter} when the caller's policies deny the action.
*/
public void authorizeAdditionalResource(String authorizationHeader, String action, String resource) {
if (!config.services().iam().enforcementEnabled()) {
return;
}
if (authorizationHeader == null) {
return;
}
String akid = accountResolver.extractAccessKeyId(authorizationHeader);
if (akid == null || "test".equals(akid)) {
return;
}
if (extractCredentialScope(authorizationHeader) == null) {
return;
}
String accountId = requestContext.getAccountId() == null
? accountResolver.resolve(authorizationHeader)
: requestContext.getAccountId();
List<List<String>> scpLevels = scpProvider.isResolvable()
? scpProvider.get().effectiveScpLevels(accountId) : null;
boolean accountRootPrincipal = false;
CallerContext caller = iamService.resolveCallerContext(akid);
if (caller == null) {
if (scpLevels == null || !akid.equals(accountId)) {
return;
}
caller = CallerContext.of(List.of(ROOT_ALLOW_ALL));
accountRootPrincipal = true;
}
if (scpLevels != null) {
caller = caller.withScpLevels(scpLevels);
}
Map<String, List<String>> conditionContext = null;
Optional<String> principalArn = accountRootPrincipal
? Optional.of("arn:aws:iam::" + accountId + ":root")
: iamService.resolveCallerArn(akid);
if (principalArn.isPresent()) {
conditionContext = new HashMap<>();
conditionContext.put("aws:PrincipalArn", List.of(principalArn.get()));
}
Decision decision = evaluator.evaluate(caller, null, action, resource, conditionContext);
if (decision != Decision.DENY) {
return;
}
LOG.infov("IAM enforcement DENY: akid={0} action={1} resource={2}", akid, action, resource);
throw new AwsException("AccessDenied",
"User: arn:aws:iam::" + accountId + ":user/" + akid
+ " is not authorized to perform: " + action
+ " on resource: \"" + resource + "\""
+ " because no identity-based policy allows the " + action + " action",
403);
}
/**
* Best-effort CloudTrail emission for S3 access denials. Without this hook,
* denied requests get aborted before {@code S3Controller}'s try/catch sees
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AccountResolver;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.IamEnforcementFilter;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -94,6 +95,7 @@ public class S3Controller {
private final AccountResolver accountResolver;
private final RequestContext requestContext;
private final IamService iamService;
private final IamEnforcementFilter iamEnforcementFilter;
@Inject
public S3Controller(S3Service s3Service, S3SelectService s3SelectService,
@@ -104,7 +106,8 @@ public class S3Controller {
CloudTrailService cloudTrailService,
AccountResolver accountResolver,
RequestContext requestContext,
IamService iamService) {
IamService iamService,
IamEnforcementFilter iamEnforcementFilter) {
this.s3Service = s3Service;
this.s3SelectService = s3SelectService;
this.regionResolver = regionResolver;
@@ -115,6 +118,7 @@ public class S3Controller {
this.accountResolver = accountResolver;
this.requestContext = requestContext;
this.iamService = iamService;
this.iamEnforcementFilter = iamEnforcementFilter;
}
private void emitCloudTrailEvent(String eventName, String bucket, String key,
@@ -2556,6 +2560,7 @@ public class S3Controller {
String contentType, HttpHeaders httpHeaders) {
CopySourceRef sourceObject = parseCopySource(copySource);
String sourceBucket = sourceObject.bucket();
authorizeCopySourceRead(httpHeaders, sourceBucket, sourceObject.objectKey());
String copyContentEncoding = toPersistedContentEncoding(httpHeaders.getHeaderString("Content-Encoding"));
String copyContentDisposition = httpHeaders.getHeaderString("Content-Disposition");
String copyCacheControl = httpHeaders.getHeaderString("Cache-Control");
@@ -2635,6 +2640,7 @@ public class S3Controller {
String uploadId, int partNumber, HttpHeaders httpHeaders) {
CopySourceRef sourceObject = parseCopySource(copySource);
String sourceBucket = sourceObject.bucket();
authorizeCopySourceRead(httpHeaders, sourceBucket, sourceObject.objectKey());
String copySourceRange = httpHeaders.getHeaderString("x-amz-copy-source-range");
String eTag = s3Service.uploadPartCopy(destBucket, destKey, uploadId, partNumber,
sourceBucket, sourceObject.objectKey(), sourceObject.versionId(), copySourceRange,
@@ -3699,6 +3705,19 @@ public class S3Controller {
return Response.ok().build();
}
/**
* Authorizes {@code s3:GetObject} on the CopyObject/UploadPartCopy source, which only ever appears
* in the {@code x-amz-copy-source} header and is otherwise invisible to the request-level IAM filter
* that authorizes the destination from the URL path. Real S3 requires both permissions: a caller
* 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);
iamEnforcementFilter.authorizeAdditionalResource(
httpHeaders.getHeaderString("Authorization"), "s3:GetObject", resource);
}
/**
* Splits a raw {@code x-amz-copy-source} header into decoded source bucket, object key and optional
* {@code versionId}. Shared by {@code CopyObject} and {@code UploadPartCopy}.