Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): limit CORS credentials header to S3-routed requests (#5375)
Matheus Oliveira9698c216c5d1
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.javaindex 8a27bcf8d7..11ffe8f1bd 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@@ -1360,6 +1360,9 @@ public class S3Controller {.header("Access-Control-Allow-Origin", cors.allowedOrigin()).header("Access-Control-Allow-Methods", String.join(", ", cors.allowedMethods()));+ if (!"*".equals(cors.allowedOrigin())) {+ builder.header("Access-Control-Allow-Credentials", "true");+ }if (cors.maxAgeSeconds() > 0) {builder.header("Access-Control-Max-Age", cors.maxAgeSeconds());}diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3CorsFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3CorsFilter.javaindex 08bf3358d7..65c1135433 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3CorsFilter.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3CorsFilter.java@@ -4,6 +4,8 @@ import jakarta.inject.Inject;import jakarta.ws.rs.container.ContainerRequestContext;import jakarta.ws.rs.container.ContainerResponseContext;import jakarta.ws.rs.container.ContainerResponseFilter;+import jakarta.ws.rs.container.ResourceInfo;+import jakarta.ws.rs.core.Context;import jakarta.ws.rs.core.MultivaluedMap;import jakarta.ws.rs.ext.Provider;@@ -22,13 +24,22 @@ import java.util.Optional;@Providerpublic class S3CorsFilter implements ContainerResponseFilter {+ private final S3Service s3Service;+ private final ResourceInfo resourceInfo;+@Inject- S3Service s3Service;+ public S3CorsFilter(S3Service s3Service, @Context ResourceInfo resourceInfo) {+ this.s3Service = s3Service;+ this.resourceInfo = resourceInfo;+ }@Overridepublic void filter(ContainerRequestContext requestContext,ContainerResponseContext responseContext) {+ // Only apply CORS headers when the request is routed to the S3 controller+ if (!S3SignatureFilterScope.routedToS3(resourceInfo)) return;+// Only handle actual requests — preflights are processed by the OPTIONS endpointsif ("OPTIONS".equalsIgnoreCase(requestContext.getMethod())) return;@@ -48,6 +59,9 @@ public class S3CorsFilter implements ContainerResponseFilter {// putSingle replaces any value already set by a resource method or earlier filter,// preventing duplicate Access-Control-Allow-Origin / Expose-Headers entries.headers.putSingle("Access-Control-Allow-Origin", cors.allowedOrigin());+ if (!"*".equals(cors.allowedOrigin())) {+ headers.putSingle("Access-Control-Allow-Credentials", "true");+ }// Merge "Origin" into Vary without duplicating it; Vary may already carry other// tokens (e.g. "Accept-Encoding") added by the JAX-RS runtime or other filters.@@ -77,4 +91,4 @@ public class S3CorsFilter implements ContainerResponseFilter {String bucket = slash > 0 ? p.substring(0, slash) : p;return bucket.isEmpty() ? null : bucket;}-}\ No newline at end of file+}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3CorsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3CorsIntegrationTest.javaindex a7c24baa50..48e6b0b3c5 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3CorsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3CorsIntegrationTest.java@@ -165,7 +165,8 @@ class S3CorsIntegrationTest {.options("/" + BUCKET + "/some-key").then().statusCode(200)- .header("Access-Control-Allow-Origin", equalTo("*"));+ .header("Access-Control-Allow-Origin", equalTo("*"))+ .header("Access-Control-Allow-Credentials", nullValue());}@Test@@ -260,7 +261,8 @@ class S3CorsIntegrationTest {.get("/" + BUCKET + "/hello.txt").then().statusCode(200)- .header("Access-Control-Allow-Origin", equalTo("*"));+ .header("Access-Control-Allow-Origin", equalTo("*"))+ .header("Access-Control-Allow-Credentials", nullValue());}@Test@@ -324,9 +326,23 @@ class S3CorsIntegrationTest {.then().statusCode(200).header("Access-Control-Allow-Origin", equalTo("https://example.com"))+ .header("Access-Control-Allow-Credentials", equalTo("true")).header("Access-Control-Max-Age", equalTo("600"));}+ @Test+ @Order(38)+ void actualGetRequestSpecificOriginReceivesAllowCredentials() {+ given()+ .header("Origin", "https://example.com")+ .when()+ .get("/" + BUCKET + "/hello.txt")+ .then()+ .statusCode(200)+ .header("Access-Control-Allow-Origin", equalTo("https://example.com"))+ .header("Access-Control-Allow-Credentials", equalTo("true"));+ }+@Test@Order(32)void optionsPreflightSpecificOriginReturnsAllExposeHeaders() {@@ -478,7 +494,8 @@ class S3CorsIntegrationTest {.options("/" + BUCKET + "/k").then().statusCode(200)- .header("Access-Control-Allow-Origin", equalTo("http://foo.example.com"));+ .header("Access-Control-Allow-Origin", equalTo("http://foo.example.com"))+ .header("Access-Control-Allow-Credentials", equalTo("true"));}@Test@@ -638,4 +655,108 @@ class S3CorsIntegrationTest {org.junit.jupiter.api.Assertions.assertEquals(1, count,"Access-Control-Allow-Origin must appear exactly once");}-}\ No newline at end of file++ // ── Regression: filter must not apply to non-S3 routes ──────────────────────++ @Test+ @Order(80)+ void putCorsConfigOnExecuteApiBucket() {+ String executeApiCorsXml = "<CORSConfiguration>" ++ " <CORSRule>" ++ " <AllowedOrigin>https://example.com</AllowedOrigin>" ++ " <AllowedMethod>GET</AllowedMethod>" ++ " <AllowedHeader>*</AllowedHeader>" ++ " <MaxAgeSeconds>600</MaxAgeSeconds>" ++ " </CORSRule>" ++ "</CORSConfiguration>";+ given()+ .when()+ .put("/execute-api")+ .then()+ .statusCode(200);++ given()+ .contentType("application/xml")+ .body(executeApiCorsXml)+ .when()+ .put("/execute-api?cors")+ .then()+ .statusCode(200);+ }++ @Test+ @Order(81)+ void nonS3RouteWithMatchingBucketNameDoesNotReceiveCorsCredentials() {+ // Request to API Gateway path format /execute-api/{apiId}/{stage}/...+ // should not get S3 CORS headers even if a bucket named "execute-api" has matching CORS+ given()+ .header("Origin", "https://example.com")+ .when()+ .get("/execute-api/abc123/prod/some-path")+ .then()+ .statusCode(404) // API Gateway returns 404 for unknown paths+ .header("Access-Control-Allow-Origin", nullValue())+ .header("Access-Control-Allow-Credentials", nullValue());+ }++ @Test+ @Order(82)+ void nonS3RouteWithMatchingBucketNameDoesNotReceiveCorsCredentialsOnOptions() {+ // OPTIONS to API Gateway path should not get S3 CORS preflight headers+ given()+ .header("Origin", "https://example.com")+ .header("Access-Control-Request-Method", "GET")+ .when()+ .options("/execute-api/abc123/prod/some-path")+ .then()+ .statusCode(404)+ .header("Access-Control-Allow-Origin", nullValue())+ .header("Access-Control-Allow-Credentials", nullValue());+ }++ // ── S3 bucket named execute-api still works ─────────────────────────────────++ @Test+ @Order(83)+ void s3BucketNamedExecuteApiGetsCorsCredentialsOnActualRequest() {+ // S3 request to bucket "execute-api" (not API Gateway path) should get credentials+ given()+ .header("Origin", "https://example.com")+ .when()+ .get("/execute-api/some-key")+ .then()+ .statusCode(404) // object doesn't exist, but CORS headers should be present+ .header("Access-Control-Allow-Origin", equalTo("https://example.com"))+ .header("Access-Control-Allow-Credentials", equalTo("true"));+ }++ @Test+ @Order(84)+ void s3BucketNamedExecuteApiWildcardOriginGetsNoCredentials() {+ // Wildcard origin on S3 bucket "execute-api" should not get credentials+ String wildcardCorsXml = "<CORSConfiguration>" ++ " <CORSRule>" ++ " <AllowedOrigin>*</AllowedOrigin>" ++ " <AllowedMethod>GET</AllowedMethod>" ++ " <AllowedHeader>*</AllowedHeader>" ++ " <MaxAgeSeconds>600</MaxAgeSeconds>" ++ " </CORSRule>" ++ "</CORSConfiguration>";+ given()+ .contentType("application/xml")+ .body(wildcardCorsXml)+ .when()+ .put("/execute-api?cors")+ .then()+ .statusCode(200);++ given()+ .header("Origin", "https://anything.com")+ .when()+ .get("/execute-api/some-key")+ .then()+ .statusCode(404)+ .header("Access-Control-Allow-Origin", equalTo("*"))+ .header("Access-Control-Allow-Credentials", nullValue());+ }+}