fix(s3): match CORS AllowedHeader wildcards such as x-amz-* (#6447)

This commit is contained in:
Wleeaf authored and GitHub committed 2026-10-24 19:51:32 -04:00
1 parent bc18cbbdaa
commit 794792c14b
5 files changed
+121 -11

No files matched your search

@@ -1181,6 +1181,48 @@ class CloudFrontDistributionServingTest {
.header("Vary", containsString("Access-Control-Request-Method"));
}
@Test
void listsRequestedHeadersForS3CorsPreflightMatchedByPattern() {
String suffix = suffix();
String bucket = "cf-pattern-header-preflight-" + suffix;
createBucket(bucket);
s3Service.putBucketCors(bucket, """
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>https://viewer.example</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>x-amz-*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
""");
Origin origin = s3Origin("s3-origin", bucket);
DefaultCacheBehavior behavior = defaultBehavior(origin.getId());
behavior.setAllowedMethods(List.of("GET", "HEAD", "OPTIONS"));
DistributionConfig config = new DistributionConfig();
config.setEnabled(true);
config.setOrigins(List.of(origin));
config.setDefaultCacheBehavior(behavior);
Distribution distribution =
cloudFrontService.createDistribution(distribution(config), Map.of());
given()
.header("Host", distribution.getDomainName())
.header("Origin", "https://viewer.example")
.header("Access-Control-Request-Method", "GET")
.header("Access-Control-Request-Headers", "x-amz-meta-owner,x-amz-date")
.when().options("/asset.txt")
.then().statusCode(200)
.header("Access-Control-Allow-Headers", equalTo("x-amz-meta-owner, x-amz-date"));
given()
.header("Host", distribution.getDomainName())
.header("Origin", "https://viewer.example")
.header("Access-Control-Request-Method", "GET")
.when().options("/asset.txt")
.then().statusCode(200)
.header("Access-Control-Allow-Headers", nullValue());
}
@Test
void appliesConfiguredOriginHeaderToS3CorsPreflight() {
String suffix = suffix();
@@ -759,4 +759,58 @@ class S3CorsIntegrationTest {
.header("Access-Control-Allow-Origin", equalTo("*"))
.header("Access-Control-Allow-Credentials", nullValue());
}
// ── AllowedHeader wildcards ──────────────────────────────────────────────
@Test
@Order(85)
void allowedHeaderWithWildcardMatchesHeadersByPrefix() {
String bucket = "cors-header-wildcard-bucket";
String corsXml = "<CORSConfiguration>" +
" <CORSRule>" +
" <AllowedOrigin>https://example.com</AllowedOrigin>" +
" <AllowedMethod>PUT</AllowedMethod>" +
" <AllowedHeader>x-amz-*</AllowedHeader>" +
" <AllowedHeader>Content-*</AllowedHeader>" +
" </CORSRule>" +
"</CORSConfiguration>";
given().when().put("/" + bucket).then().statusCode(200);
given().contentType("application/xml").body(corsXml)
.when()
.put("/" + bucket + "?cors")
.then()
.statusCode(200);
// Browsers send header names in lower case; each one is allowed by name.
given()
.header("Origin", "https://example.com")
.header("Access-Control-Request-Method", "PUT")
.header("Access-Control-Request-Headers", "content-type,x-amz-meta-owner,x-amz-date")
.when()
.options("/" + bucket + "/upload-key")
.then()
.statusCode(200)
.header("Access-Control-Allow-Origin", equalTo("https://example.com"))
.header("Access-Control-Allow-Headers", equalTo("content-type, x-amz-meta-owner, x-amz-date"));
// A header no pattern covers is still refused.
given()
.header("Origin", "https://example.com")
.header("Access-Control-Request-Method", "PUT")
.header("Access-Control-Request-Headers", "content-type,authorization")
.when()
.options("/" + bucket + "/upload-key")
.then()
.statusCode(403);
// A preflight that asks for no headers gets no Access-Control-Allow-Headers.
given()
.header("Origin", "https://example.com")
.header("Access-Control-Request-Method", "PUT")
.when()
.options("/" + bucket + "/upload-key")
.then()
.statusCode(200)
.header("Access-Control-Allow-Headers", nullValue());
}
}