mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): verify header and presigned POST signatures under validate-signatures (#4786)
* fix(s3): verify header and presigned POST signatures under validate-signatures floci.auth.validate-signatures verified only presigned URL signatures. A header-signed request or a presigned POST with a forged signature or an unknown access key was accepted unless enforce-auth was on, and enforce-auth also turns on bucket-policy evaluation, so there was no way to authenticate S3 requests without authorizing them. S3 verifies a signature before, and independently of, deciding from policies and ACLs whether the caller may act. validate-signatures now does the first step for every placement: S3HeaderSignatureFilter runs under either flag, and a browser POST that carries SigV4 form fields is verified. Bucket policies are still not evaluated, and unsigned requests are still left to enforce-auth, which decides anonymous access from policies and ACLs as AWS does. docs/services/s3.md now says what each flag verifies. * chore(s3): keep validatePresignedPostAuth's Javadoc on its method * docs(s3): clarify multi-account credentials under signature verification * docs(s3): update docs/configuration/multi-account.md Signed-off-by: Abhigya <abhigyakrishna21@gmail.com> --------- Signed-off-by: Abhigya <abhigyakrishna21@gmail.com>
This commit is contained in:
1 parent
fa4d4663a2
commit
8f904afa5a
9 files changed
+325
-21
No files matched your search
+6
-6
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.testutil.S3RequestSigner;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
@@ -34,8 +35,7 @@ class PreSignedUrlIntegrationTest {
|
||||
|
||||
private static final String BUCKET = "presign-test-bucket";
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/s3/aws4_request";
|
||||
private static final S3RequestSigner LOCAL_SIGNER = S3RequestSigner.signedAs("test", "test");
|
||||
|
||||
@Inject
|
||||
PreSignedUrlGenerator presignGenerator;
|
||||
@@ -220,7 +220,7 @@ class PreSignedUrlIntegrationTest {
|
||||
// Must be a signed request per AWS spec (response-* params require Authorization or presigned URL).
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.get("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22file.txt%22")
|
||||
.then()
|
||||
@@ -233,7 +233,7 @@ class PreSignedUrlIntegrationTest {
|
||||
void getObjectAppliesAllResponseOverrides() {
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.get("/" + BUCKET + "/disposition-file.txt"
|
||||
+ "?response-content-type=application%2Fpdf"
|
||||
@@ -269,7 +269,7 @@ class PreSignedUrlIntegrationTest {
|
||||
void headObjectAppliesResponseContentDispositionOverride() {
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.head("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22head.txt%22")
|
||||
.then()
|
||||
@@ -295,7 +295,7 @@ class PreSignedUrlIntegrationTest {
|
||||
void rangeRequestAppliesResponseContentDispositionOverride() {
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.filter(LOCAL_SIGNER)
|
||||
.header("Range", "bytes=0-3")
|
||||
.when()
|
||||
.get("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22range.txt%22")
|
||||
|
||||
+253
@@ -0,0 +1,253 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
|
||||
import io.github.hectorvent.floci.testutil.S3RequestSigner;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* {@code floci.auth.validate-signatures} authenticates every signed S3 request, whichever
|
||||
* placement carries the signature, without authorizing it: a forged or unknown credential is
|
||||
* refused, while bucket policies are not evaluated and unsigned requests pass, as they do with
|
||||
* {@code enforce-auth} off. Shares {@link PreSignedUrlIntegrationTest}'s profile, which covers the
|
||||
* presigned query-string placement.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(PreSignedUrlIntegrationTest.PresignValidationProfile.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class S3ValidateSignaturesIntegrationTest {
|
||||
|
||||
private static final String BUCKET = "validate-signatures-bucket";
|
||||
private static final String KEY = "object.txt";
|
||||
private static final String CREDENTIAL_DATE = "20260101";
|
||||
private static final String AMZ_DATE = CREDENTIAL_DATE + "T000000Z";
|
||||
private static final S3RequestSigner LOCAL_SIGNER = S3RequestSigner.signedAs("test", "test");
|
||||
private static final String DENY_ALL_POLICY = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Deny","Principal":"*",
|
||||
"Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*"}]}
|
||||
""".formatted(BUCKET);
|
||||
|
||||
private static String userAccessKeyId;
|
||||
private static String userSecretKey;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void headerSignedRequestsWithAKnownKeyAreAccepted() {
|
||||
given().filter(LOCAL_SIGNER).when().put("/" + BUCKET).then().statusCode(200);
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.body("signed body")
|
||||
.when()
|
||||
.put("/" + BUCKET + "/" + KEY)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.get("/" + BUCKET + "/" + KEY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("signed body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void headerSignatureThatDoesNotVerifyIsRejected() {
|
||||
given()
|
||||
.filter(LOCAL_SIGNER.withSignature("0".repeat(64)))
|
||||
.when()
|
||||
.get("/" + BUCKET + "/" + KEY)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("SignatureDoesNotMatch"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void headerSignedRequestWithAnUnknownKeyIsRejected() {
|
||||
given()
|
||||
.filter(S3RequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret"))
|
||||
.when()
|
||||
.get("/" + BUCKET + "/" + KEY)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("InvalidAccessKeyId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void headerSignedBodyThatDoesNotMatchItsHashIsRejected() {
|
||||
given()
|
||||
.filter(LOCAL_SIGNER.withContentSha256("0".repeat(64)))
|
||||
.body("tampered body")
|
||||
.when()
|
||||
.put("/" + BUCKET + "/tampered.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Error.Code", equalTo("XAmzContentSHA256Mismatch"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void unsignedRequestsAreLeftToEnforceAuth() {
|
||||
given().body("anonymous body").when().put("/" + BUCKET + "/anonymous.txt").then().statusCode(200);
|
||||
given().when().get("/" + BUCKET + "/anonymous.txt").then().statusCode(200).body(equalTo("anonymous body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void verifiedCallerIsNotAuthorizedAgainstTheBucketPolicy() {
|
||||
createIamUser("validate-signatures-user");
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.body(DENY_ALL_POLICY)
|
||||
.when()
|
||||
.put("/" + BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.get("/" + BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("\"Deny\""));
|
||||
|
||||
given()
|
||||
.filter(S3RequestSigner.signedAs(userAccessKeyId, userSecretKey))
|
||||
.when()
|
||||
.get("/" + BUCKET + "/" + KEY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("signed body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void presignedPostWithAGenuineSignatureIsAccepted() {
|
||||
String key = "post-genuine.txt";
|
||||
String policy = postPolicy(key);
|
||||
String credential = "test/" + CREDENTIAL_DATE + "/us-east-1/s3/aws4_request";
|
||||
|
||||
given()
|
||||
.multiPart("key", key)
|
||||
.multiPart("policy", policy)
|
||||
.multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
|
||||
.multiPart("x-amz-credential", credential)
|
||||
.multiPart("x-amz-date", AMZ_DATE)
|
||||
.multiPart("x-amz-signature", signPolicy(policy, "test"))
|
||||
.multiPart("file", key, "posted".getBytes(StandardCharsets.UTF_8), "text/plain")
|
||||
.when()
|
||||
.post("/" + BUCKET)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void presignedPostWithAForgedSignatureIsRejected() {
|
||||
String key = "post-forged.txt";
|
||||
String policy = postPolicy(key);
|
||||
|
||||
given()
|
||||
.multiPart("key", key)
|
||||
.multiPart("policy", policy)
|
||||
.multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
|
||||
.multiPart("x-amz-credential", "test/" + CREDENTIAL_DATE + "/us-east-1/s3/aws4_request")
|
||||
.multiPart("x-amz-date", AMZ_DATE)
|
||||
.multiPart("x-amz-signature", "0".repeat(64))
|
||||
.multiPart("file", key, "forged".getBytes(StandardCharsets.UTF_8), "text/plain")
|
||||
.when()
|
||||
.post("/" + BUCKET)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("SignatureDoesNotMatch"));
|
||||
|
||||
given().filter(LOCAL_SIGNER).when().get("/" + BUCKET + "/" + key).then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(22)
|
||||
void presignedPostWithASignatureButNoPolicyIsRejected() {
|
||||
given()
|
||||
.multiPart("key", "post-partial.txt")
|
||||
.multiPart("x-amz-signature", "0".repeat(64))
|
||||
.multiPart("file", "post-partial.txt", "partial".getBytes(StandardCharsets.UTF_8), "text/plain")
|
||||
.when()
|
||||
.post("/" + BUCKET)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(23)
|
||||
void unsignedPostIsLeftToEnforceAuth() {
|
||||
given()
|
||||
.multiPart("key", "post-anonymous.txt")
|
||||
.multiPart("file", "post-anonymous.txt", "anonymous".getBytes(StandardCharsets.UTF_8), "text/plain")
|
||||
.when()
|
||||
.post("/" + BUCKET)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
|
||||
private static void createIamUser(String userName) {
|
||||
String authorization = "AWS4-HMAC-SHA256 Credential=test/" + CREDENTIAL_DATE
|
||||
+ "/us-east-1/iam/aws4_request, SignedHeaders=host, Signature=unused";
|
||||
given()
|
||||
.formParam("Action", "CreateUser")
|
||||
.formParam("UserName", userName)
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
XmlPath key = given()
|
||||
.formParam("Action", "CreateAccessKey")
|
||||
.formParam("UserName", userName)
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.xmlPath();
|
||||
userAccessKeyId = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
|
||||
userSecretKey = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey");
|
||||
}
|
||||
|
||||
private static String postPolicy(String key) {
|
||||
String expiration = DateTimeFormatter.ISO_INSTANT.format(
|
||||
Instant.now().plusSeconds(3600).atZone(ZoneOffset.UTC));
|
||||
String policy = """
|
||||
{"expiration": "%s", "conditions": [{"bucket": "%s"}, {"key": "%s"}]}
|
||||
""".formatted(expiration, BUCKET, key);
|
||||
return Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String signPolicy(String policy, String secretKey) {
|
||||
try {
|
||||
byte[] signingKey = SigV4RequestValidator.deriveSigningKey(secretKey, CREDENTIAL_DATE, "us-east-1", "s3");
|
||||
return SigV4RequestValidator.hexEncode(SigV4RequestValidator.hmacSha256(signingKey, policy));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("could not sign the POST policy", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user