mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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.
This commit is contained in:
1 parent
63cf09a396
commit
377c09733a
5 files changed
+177
-10
No files matched your search
+100
@@ -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("<Code>AccessDenied</Code>"));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@@ -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<String> 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"
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user