diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java index 362d982156..3db376e687 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java @@ -44,6 +44,13 @@ public class PreSignedUrlFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) { + // A browser preflight reuses the target request's presigned URL, so its OPTIONS method + // must not be verified against a signature created for the follow-up PUT/GET request. + // The dedicated S3 OPTIONS resource performs the bucket CORS evaluation instead. + if (isCorsPreflight(requestContext)) { + return; + } + var queryParams = requestContext.getUriInfo().getQueryParameters(); // Only process if this is a pre-signed URL request @@ -144,6 +151,16 @@ public class PreSignedUrlFilter implements ContainerRequestFilter { } } + private static boolean isCorsPreflight(ContainerRequestContext requestContext) { + return "OPTIONS".equalsIgnoreCase(requestContext.getMethod()) + && hasText(requestContext.getHeaderString("Origin")) + && hasText(requestContext.getHeaderString("Access-Control-Request-Method")); + } + + private static boolean hasText(String value) { + return value != null && !value.isBlank(); + } + private boolean verifySigV4Signature(ContainerRequestContext requestContext, String signature, String secretKey) { try { diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java index fa22619f48..d19d9f42d3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java @@ -2,6 +2,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.restassured.specification.RequestSpecification; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTestProfile; import io.quarkus.test.junit.TestProfile; @@ -1959,6 +1960,109 @@ class S3AuthEnforcementIntegrationTest { .body(containsString("AuthorizationHeaderMalformed")); } + @Test + @Order(54) + void presignedPutCorsPreflightSkipsOnlyPreflightSignatureValidation() { + String bucket = "auth-presigned-cors-" + Long.toUnsignedString(System.nanoTime(), 36); + String key = "upload.txt"; + String path = "/" + bucket + "/" + key; + String signature = presignedSignature("PUT", path, "test", "test", "3600"); + String tamperedSignature = signature.substring(0, signature.length() - 1) + + (signature.endsWith("0") ? "1" : "0"); + String corsConfiguration = """ + + + https://app.example.com + PUT + content-type + 600 + + + """; + + try { + given() + .filter(LOCAL_SIGNER) + .when() + .put("/" + bucket) + .then() + .statusCode(200); + + given() + .filter(LOCAL_SIGNER) + .contentType("application/xml") + .body(corsConfiguration) + .when() + .put("/" + bucket + "?cors") + .then() + .statusCode(200); + + presignedRequest(signature) + .header("Origin", "https://app.example.com") + .header("Access-Control-Request-Method", "PUT") + .header("Access-Control-Request-Headers", "content-type") + .when() + .options(path) + .then() + .statusCode(200) + .header("Access-Control-Allow-Origin", equalTo("https://app.example.com")) + .header("Access-Control-Allow-Methods", containsString("PUT")) + .header("Access-Control-Allow-Headers", equalTo("content-type")) + .header("Access-Control-Max-Age", equalTo("600")); + + // OPTIONS alone is not a CORS preflight and must not turn a PUT signature into a + // general authentication bypass. + presignedRequest(signature) + .when() + .options(path) + .then() + .statusCode(403) + .body(containsString("SignatureDoesNotMatch")); + + // Origin without Access-Control-Request-Method is not a preflight either. + presignedRequest(signature) + .header("Origin", "https://app.example.com") + .when() + .options(path) + .then() + .statusCode(403) + .body(containsString("SignatureDoesNotMatch")); + + presignedRequest(tamperedSignature) + .header("Origin", "https://app.example.com") + .contentType("text/plain") + .body("tampered") + .when() + .put(path) + .then() + .statusCode(403) + .body(containsString("SignatureDoesNotMatch")); + + presignedRequest(signature) + .header("Origin", "https://app.example.com") + .contentType("text/plain") + .body("uploaded") + .when() + .put(path) + .then() + .statusCode(200) + .header("Access-Control-Allow-Origin", equalTo("https://app.example.com")); + } finally { + given().filter(LOCAL_SIGNER).when().delete(path); + given().filter(LOCAL_SIGNER).when().delete("/" + bucket); + } + } + + private static RequestSpecification presignedRequest(String signature) { + return given() + .queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256") + .queryParam("X-Amz-Credential", credential("test")) + .queryParam("X-Amz-Date", SIGNING_TIMESTAMP) + .queryParam("X-Amz-Expires", "3600") + .queryParam("X-Amz-SignedHeaders", "host") + .queryParam("X-Amz-Signature", signature); + } + private static HttpResponse putWithoutContentType(String pathAndQuery, byte[] body, S3RequestSigner signer) throws Exception { URI uri = URI.create("http://localhost:" + io.restassured.RestAssured.port + "/" + pathAndQuery);