fix(s3): limit CORS credentials header to S3-routed requests (#5375)

Add S3SignatureFilterScope.routedToS3 check to S3CorsFilter so
Access-Control-Allow-Credentials is only applied to requests routed
to S3Controller, not to other services like API Gateway's
/execute-api/ paths.

Add regression tests for:
- non-S3 routes with matching bucket names (API Gateway /execute-api/... paths)
- S3 bucket named 'execute-api' still receives correct CORS headers

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-11 18:47:28 -04:00
1 parent a8374ee2ad
commit 9698c216c5
3 files changed
+144 -6

No files matched your search

@@ -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());
}
@@ -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;
@Provider
public 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;
}
@Override
public 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 endpoints
if ("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;
}
}
}
@@ -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");
}
}
// ── 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());
}
}