From 377c09733a0fd32f941b47ff325fd3d5e168d93e Mon Sep 17 00:00:00 2001
From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
Date: Sat, 17 Oct 2026 23:31:36 -0400
Subject: [PATCH] fix(s3): check a presigned copy's source read against its
credential (#5891)
Under IAM enforcement, CopyObject and UploadPartCopy also need
s3:GetObject on the copy source, or s3:GetObjectVersion for a version,
and S3Controller checks it itself, since the source appears only in the
x-amz-copy-source header. It passed IAM the Authorization header, which
a presigned request does not have, so IAM skipped the check. A user
allowed to write one place could presign a copy of any object they
cannot read into it.
The check now uses the request's credential, the header's or a
presigned URL's query credential, as the copied-tags check next to it
already does. The S3 user guide says "The capabilities of a presigned
URL are limited by the permissions of the user who created it."
The test signer's presignedQuery also signs the parameters already in
its URI's query, such as an UploadPartCopy's upload id, so a presigned
part copy verifies, and refuses a URI that is already presigned. docs/services/iam.md lists the copy source check.
---
docs/services/iam.md | 3 +
.../floci/services/s3/S3Controller.java | 19 ++--
...ObjectSourcePermissionIntegrationTest.java | 100 ++++++++++++++++++
.../floci/testutil/S3RequestSigner.java | 20 +++-
.../floci/testutil/S3RequestSignerTest.java | 45 ++++++++
5 files changed, 177 insertions(+), 10 deletions(-)
create mode 100644 src/test/java/io/github/hectorvent/floci/testutil/S3RequestSignerTest.java
diff --git a/docs/services/iam.md b/docs/services/iam.md
index 0feb8c5832..2137703516 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -1232,6 +1232,9 @@ An S3 object operation is checked as the action the S3 documentation gives it:
`SelectObjectContent` as `s3:GetObject`, or `s3:GetObjectVersion` when it names a version.
- **Attributes:** `GetObjectAttributes` needs `s3:GetObject` and `s3:GetObjectAttributes`, as its
API reference says.
+- **Copying:** `CopyObject` and `UploadPartCopy` also need `s3:GetObject` on the source object, or
+ `s3:GetObjectVersion` when `x-amz-copy-source` names a version, as their API references say. It
+ is checked against the request's credential, a presigned URL's included.
- **Object lock:**
- Object retention and legal hold use their own actions: `s3:GetObjectRetention`,
`s3:PutObjectRetention`, `s3:GetObjectLegalHold` and `s3:PutObjectLegalHold`.
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
index 841e4f3ffc..4a2c26edbd 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
@@ -859,8 +859,8 @@ public class S3Controller {
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
int parsedPartNumber = PartRange.parseUploadPartNumber(partNumber);
if (copySource != null && !copySource.isEmpty()) {
- return handleUploadPartCopy(
- copySource, bucket, key, uploadId, parsedPartNumber, httpHeaders, authorization);
+ return handleUploadPartCopy(copySource, bucket, key, uploadId, parsedPartNumber,
+ httpHeaders, uriInfo, authorization);
}
requireDeclaredSizeWithin(httpHeaders, contentEncoding, contentSha256, S3Service.MAX_PART_SIZE);
Part part = s3Service.storePart(bucket, key, uploadId, parsedPartNumber,
@@ -3067,7 +3067,7 @@ public class S3Controller {
ObjectLockHeaders lock) {
CopySourceRef sourceObject = parseCopySource(copySource);
String sourceBucket = sourceObject.bucket();
- authorizeCopySourceRead(httpHeaders, sourceObject, authorization);
+ authorizeCopySourceRead(httpHeaders, uriInfo, sourceObject, authorization);
authorizeCopiedTags(httpHeaders, uriInfo, sourceObject, destBucket, destKey, authorization);
String copyContentEncoding = toPersistedContentEncoding(httpHeaders.getHeaderString("Content-Encoding"));
String copyContentDisposition = httpHeaders.getHeaderString("Content-Disposition");
@@ -3155,10 +3155,10 @@ public class S3Controller {
private Response handleUploadPartCopy(String copySource, String destBucket, String destKey,
String uploadId, int partNumber, HttpHeaders httpHeaders,
- S3Service.RequestAuthorization authorization) {
+ UriInfo uriInfo, S3Service.RequestAuthorization authorization) {
CopySourceRef sourceObject = parseCopySource(copySource);
String sourceBucket = sourceObject.bucket();
- authorizeCopySourceRead(httpHeaders, sourceObject, authorization);
+ authorizeCopySourceRead(httpHeaders, uriInfo, 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,
@@ -4376,9 +4376,11 @@ public class S3Controller {
* 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.
+ * IAM checks it against the request's credential, the header's or a presigned URL's, as the filter
+ * checks the destination. A no-op when IAM enforcement is disabled, matching
+ * {@link IamEnforcementFilter}'s own bypass rules.
*/
- private void authorizeCopySourceRead(HttpHeaders httpHeaders, CopySourceRef source,
+ private void authorizeCopySourceRead(HttpHeaders httpHeaders, UriInfo uriInfo, CopySourceRef source,
S3Service.RequestAuthorization authorization) {
String action = source.versionId() == null ? "s3:GetObject" : "s3:GetObjectVersion";
String resource = S3PublicAccessEvaluator.objectArn(
@@ -4386,8 +4388,7 @@ public class S3Controller {
S3Service.SignedPrincipalResourcePolicyEvaluation resourcePolicyEvaluation =
s3Service.signedPrincipalResourcePolicyDecision(
source.bucket(), action, resource, authorization);
- iamEnforcementFilter.authorizeAdditionalResource(
- httpHeaders.getHeaderString("Authorization"), action, resource,
+ iamEnforcementFilter.authorizeAdditionalResource(iamCredential(httpHeaders, uriInfo), action, resource,
resourcePolicyEvaluation.decision(), resourcePolicyEvaluation.resourceOwnerAccountId());
s3Service.authorizeGetObject(
source.bucket(), source.objectKey(), source.versionId(), authorization);
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3CopyObjectSourcePermissionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3CopyObjectSourcePermissionIntegrationTest.java
index a6ab6a2df1..ef1cb47b47 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3CopyObjectSourcePermissionIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3CopyObjectSourcePermissionIntegrationTest.java
@@ -4,8 +4,12 @@ import io.github.hectorvent.floci.testing.S3IamEnforcementProfile;
import io.github.hectorvent.floci.testutil.S3RequestSigner;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
+import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
+import java.net.URI;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
import java.util.UUID;
import static io.restassured.RestAssured.given;
@@ -438,6 +442,102 @@ class S3CopyObjectSourcePermissionIntegrationTest {
.body(containsString("AccessDenied"));
}
+ @Test
+ void presignedCopyObjectIsDeniedWhenCallerCannotReadTheSource() throws Exception {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String sourceBucket = "presigned-copy-source-" + suffix;
+ String destBucket = "presigned-copy-dest-" + suffix;
+ String userName = "presigned-copy-write-only-" + suffix;
+
+ createBucketAsRoot(sourceBucket);
+ createBucketAsRoot(destBucket);
+ putObjectAsRoot(sourceBucket, "private-secret.txt", "top secret payload");
+
+ UserCredentials caller = createUser(userName);
+ putUserPolicy(userName, "DestWriteOnly", """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%1$s/*"}
+ ]}""".formatted(destBucket));
+
+ String path = "/" + destBucket + "/exfiltrated.txt";
+ given()
+ .urlEncodingEnabled(false)
+ .header("x-amz-copy-source", "/" + sourceBucket + "/private-secret.txt")
+ .when()
+ .put(path + "?" + presignedPut(caller, path))
+ .then()
+ .statusCode(403)
+ .body(containsString("is not authorized to perform: s3:GetObject"));
+ }
+
+ @Test
+ void presignedCopyObjectSucceedsWhenCallerCanReadSourceAndWriteDestination() throws Exception {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String sourceBucket = "presigned-copy-source-ok-" + suffix;
+ String destBucket = "presigned-copy-dest-ok-" + suffix;
+ String userName = "presigned-copy-read-write-" + suffix;
+
+ createBucketAsRoot(sourceBucket);
+ createBucketAsRoot(destBucket);
+ putObjectAsRoot(sourceBucket, "allowed.txt", "not secret");
+
+ UserCredentials caller = createUser(userName);
+ putUserPolicy(userName, "SourceReadDestWrite", """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:GetObject","Resource":"arn:aws:s3:::%1$s/*"},
+ {"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%2$s/*"}
+ ]}""".formatted(sourceBucket, destBucket));
+
+ String path = "/" + destBucket + "/copied.txt";
+ given()
+ .urlEncodingEnabled(false)
+ .header("x-amz-copy-source", "/" + sourceBucket + "/allowed.txt")
+ .when()
+ .put(path + "?" + presignedPut(caller, path))
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ void presignedUploadPartCopyIsDeniedWhenCallerCannotReadTheSource() throws Exception {
+ String suffix = UUID.randomUUID().toString().substring(0, 8);
+ String sourceBucket = "presigned-part-copy-source-" + suffix;
+ String destBucket = "presigned-part-copy-dest-" + suffix;
+ String userName = "presigned-part-copy-write-only-" + suffix;
+
+ createBucketAsRoot(sourceBucket);
+ createBucketAsRoot(destBucket);
+ putObjectAsRoot(sourceBucket, "private-secret.txt", "top secret payload");
+
+ UserCredentials caller = createUser(userName);
+ putUserPolicy(userName, "DestWriteOnly", """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%1$s/*"}
+ ]}""".formatted(destBucket));
+
+ String uploadId = initiateMultipartUploadAsRoot(destBucket, "exfiltrated.txt");
+
+ String path = "/" + destBucket + "/exfiltrated.txt";
+ String part = "?partNumber=1&uploadId=" + URLEncoder.encode(uploadId, StandardCharsets.UTF_8);
+ given()
+ .urlEncodingEnabled(false)
+ .header("x-amz-copy-source", "/" + sourceBucket + "/private-secret.txt")
+ .when()
+ .put(path + "?" + presignedPut(caller, path + part))
+ .then()
+ .statusCode(403)
+ .body(containsString("is not authorized to perform: s3:GetObject"));
+ }
+
+ /**
+ * The query of a URL {@code caller} presigns for a PUT to {@code pathAndQuery}, which carries the
+ * caller's credential instead of an Authorization header.
+ */
+ private static String presignedPut(UserCredentials caller, String pathAndQuery) throws Exception {
+ return caller.signer().presignedQuery(
+ "PUT", URI.create("http://localhost:" + RestAssured.port + pathAndQuery), 300);
+ }
+
private static void createBucketAsRoot(String bucket) {
createBucketAsRoot(bucket, ROOT_SIGNER);
}
diff --git a/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSigner.java b/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSigner.java
index 3bacf4a31e..e31e111bf8 100644
--- a/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSigner.java
+++ b/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSigner.java
@@ -19,7 +19,9 @@ import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
+import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -41,6 +43,9 @@ public final class S3RequestSigner implements Filter {
private static final String SERVICE = "s3";
private static final DateTimeFormatter AMZ_DATE =
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
+ /** The query parameters a presigned URL carries, lowercased: a URI holding one is already signed. */
+ private static final Set PRESIGN_PARAMETERS = Set.of("x-amz-algorithm", "x-amz-credential",
+ "x-amz-date", "x-amz-expires", "x-amz-security-token", "x-amz-signedheaders", "x-amz-signature");
private final String accessKeyId;
private final String secretKey;
@@ -181,9 +186,19 @@ public final class S3RequestSigner implements Filter {
* The query string of a presigned URL for {@code method} on {@code uri}, written the way an AWS
* SDK presigner writes it: the {@code X-Amz-*} parameters, {@code X-Amz-Security-Token} when a
* session token is given, {@code host} as the only signed header, an unsigned payload, and the
- * signature last. Send it with URL encoding off, since it is already encoded.
+ * signature last. Parameters already in {@code uri}'s query, such as an upload id, are signed too
+ * and lead the result, so send it in place of that query. A {@code uri} that is already presigned
+ * is refused, since signing over its signature would give a URL no verifier accepts. Send the
+ * result with URL encoding off, since it is already encoded.
*/
public String presignedQuery(String method, URI uri, int expiresSeconds) throws Exception {
+ String uriQuery = uri.getRawQuery() == null ? "" : uri.getRawQuery();
+ for (String pair : uriQuery.split("&")) {
+ String name = URLDecoder.decode(pair.split("=", 2)[0], StandardCharsets.UTF_8);
+ if (PRESIGN_PARAMETERS.contains(name.toLowerCase(Locale.ROOT))) {
+ throw new IllegalArgumentException("already presigned, it carries " + name + ": " + uri);
+ }
+ }
Instant at = signedAt != null ? signedAt : Instant.now();
String amzDate = AMZ_DATE.format(at);
String scopeDate = amzDate.substring(0, 8);
@@ -201,6 +216,9 @@ public final class S3RequestSigner implements Filter {
String query = params.entrySet().stream()
.map(param -> uriEncode(param.getKey()) + "=" + uriEncode(param.getValue()))
.collect(Collectors.joining("&"));
+ if (!uriQuery.isEmpty()) {
+ query = uriQuery + "&" + query;
+ }
String rawPath = uri.getRawPath() == null || uri.getRawPath().isEmpty() ? "/" : uri.getRawPath();
String canonicalRequest = method + "\n"
diff --git a/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSignerTest.java b/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSignerTest.java
new file mode 100644
index 0000000000..8528f4e27d
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSignerTest.java
@@ -0,0 +1,45 @@
+package io.github.hectorvent.floci.testutil;
+
+import org.junit.jupiter.api.Test;
+
+import java.net.URI;
+import java.time.Instant;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class S3RequestSignerTest {
+
+ private static final S3RequestSigner SIGNER =
+ S3RequestSigner.signedAs("AKIDEXAMPLE", "secret").signedAt(Instant.parse("2026-10-17T00:00:00Z"));
+
+ @Test
+ void presignedQuerySignsTheUrisOwnQueryAndLeadsWithIt() throws Exception {
+ URI plain = URI.create("http://localhost:4566/bucket/key");
+ URI part = URI.create("http://localhost:4566/bucket/key?partNumber=1&uploadId=abc");
+
+ String plainQuery = SIGNER.presignedQuery("PUT", plain, 300);
+ String partQuery = SIGNER.presignedQuery("PUT", part, 300);
+
+ assertTrue(partQuery.startsWith("partNumber=1&uploadId=abc&X-Amz-Algorithm="), partQuery);
+ assertEquals(plainQuery.substring(0, plainQuery.indexOf("&X-Amz-Signature=")),
+ partQuery.substring("partNumber=1&uploadId=abc&".length(), partQuery.indexOf("&X-Amz-Signature=")));
+ assertNotEquals(signature(plainQuery), signature(partQuery), "the upload id is part of what is signed");
+ }
+
+ @Test
+ void presignedQueryRefusesAUriThatIsAlreadyPresigned() throws Exception {
+ URI uri = URI.create("http://localhost:4566/bucket/key");
+ URI presigned = URI.create(uri + "?" + SIGNER.presignedQuery("GET", uri, 300));
+
+ IllegalArgumentException e =
+ assertThrows(IllegalArgumentException.class, () -> SIGNER.presignedQuery("GET", presigned, 300));
+ assertTrue(e.getMessage().contains("X-Amz-Algorithm"), e.getMessage());
+ }
+
+ private static String signature(String query) {
+ return query.substring(query.indexOf("&X-Amz-Signature=") + "&X-Amz-Signature=".length());
+ }
+}