mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(s3): match CORS AllowedHeader wildcards such as x-amz-* (#6447)
This commit is contained in:
1 parent
bc18cbbdaa
commit
794792c14b
5 files changed
+121
-11
No files matched your search
+2
-3
@@ -742,9 +742,8 @@ public class CloudFrontServingController {
|
||||
putHeader(headers, "Access-Control-Max-Age",
|
||||
Integer.toString(cors.maxAgeSeconds()));
|
||||
}
|
||||
if (!cors.allowedHeaders().isEmpty()) {
|
||||
putHeader(headers, "Access-Control-Allow-Headers",
|
||||
String.join(", ", cors.allowedHeaders()));
|
||||
if (cors.allowHeaders() != null) {
|
||||
putHeader(headers, "Access-Control-Allow-Headers", cors.allowHeaders());
|
||||
}
|
||||
if (!cors.exposeHeaders().isEmpty()) {
|
||||
putHeader(headers, "Access-Control-Expose-Headers",
|
||||
|
||||
@@ -1505,11 +1505,8 @@ public class S3Controller {
|
||||
if (cors.maxAgeSeconds() > 0) {
|
||||
builder.header("Access-Control-Max-Age", cors.maxAgeSeconds());
|
||||
}
|
||||
if (!cors.allowedHeaders().isEmpty()) {
|
||||
String hdrs = cors.allowedHeaders().contains("*")
|
||||
? "*"
|
||||
: String.join(", ", cors.allowedHeaders());
|
||||
builder.header("Access-Control-Allow-Headers", hdrs);
|
||||
if (cors.allowHeaders() != null) {
|
||||
builder.header("Access-Control-Allow-Headers", cors.allowHeaders());
|
||||
}
|
||||
if (!cors.exposeHeaders().isEmpty()) {
|
||||
builder.header("Access-Control-Expose-Headers", String.join(", ", cors.exposeHeaders()));
|
||||
|
||||
@@ -5483,10 +5483,14 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
return bucket.getCorsConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* The CORS rule a request matched. {@code allowHeaders} is the preflight's
|
||||
* Access-Control-Allow-Headers value, or null when the preflight asked for no headers.
|
||||
*/
|
||||
public record CorsEvalResult(
|
||||
String allowedOrigin,
|
||||
List<String> allowedMethods,
|
||||
List<String> allowedHeaders,
|
||||
String allowHeaders,
|
||||
List<String> exposeHeaders,
|
||||
int maxAgeSeconds
|
||||
) {}
|
||||
@@ -5539,16 +5543,30 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
if (requestHeaders != null && !requestHeaders.isEmpty()) {
|
||||
boolean headersOk = allowedHeaders.contains("*")
|
||||
|| requestHeaders.stream().allMatch(rh ->
|
||||
allowedHeaders.stream().anyMatch(ah -> ah.equalsIgnoreCase(rh)));
|
||||
allowedHeaders.stream().anyMatch(ah -> matchesCorsHeader(ah, rh)));
|
||||
if (!headersOk) continue;
|
||||
}
|
||||
|
||||
String echoOrigin = allowedOrigins.contains("*") ? "*" : origin;
|
||||
return Optional.of(new CorsEvalResult(echoOrigin, allowedMethods, allowedHeaders, exposeHeaders, maxAge));
|
||||
// S3 sends back the headers the preflight asked for, and no Access-Control-Allow-Headers
|
||||
// when it asked for none. An AllowedHeader such as x-amz-* is a pattern, not a header name
|
||||
// a browser can match, so the requested names are listed; a bare * still answers *.
|
||||
String allowHeaders = requestHeaders == null || requestHeaders.isEmpty() ? null
|
||||
: allowedHeaders.contains("*") ? "*" : String.join(", ", requestHeaders);
|
||||
return Optional.of(new CorsEvalResult(echoOrigin, allowedMethods, allowHeaders, exposeHeaders, maxAge));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches an AllowedHeader against a header named in Access-Control-Request-Headers. Header
|
||||
* names are case-insensitive, and like an AllowedOrigin an AllowedHeader may hold one {@code *}
|
||||
* wildcard, as in {@code x-amz-*}.
|
||||
*/
|
||||
private static boolean matchesCorsHeader(String pattern, String header) {
|
||||
return matchesCorsOrigin(pattern.toLowerCase(Locale.ROOT), header.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches an AllowedOrigin pattern against a concrete Origin header value.
|
||||
*
|
||||
|
||||
+42
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user