mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudfront): forward origin custom headers (#1832)
* feat(cloudfront): serve distribution requests from S3 and custom origins * feat(cloudfront): forward origin custom headers * fix(cloudfront): omit Allow-Credentials and Vary for wildcard S3 CORS rules withS3CorsHeaders emitted Access-Control-Allow-Credentials: true for every matched rule, including one whose AllowedOrigin is "*" (where evaluateCors echoes "*"). The CORS specification forbids that pairing and browsers reject it for credentialed requests, so the emulator produced a response that fails in a browser while passing against Floci. Verified against real S3 rather than inferred. Bucket with AllowedOrigins ["*"]: Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET Access-Control-Max-Age: 3000 and with a concrete AllowedOrigin it additionally returns Access-Control-Allow-Credentials: true Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method Both preflight and actual GET behave identically. Vary is now also conditional: the wildcard response is byte-identical for every origin, so there is nothing to vary on, and real S3 omits it there too. * chore: re-run CI The Build and Test failure on the previous head was RuntimeApiServerTest's extension/stop race, a pre-existing flake unrelated to this change — it also failed on #1820, #1823 and on #2131's own branch (the PR that tried to de-flake it). Reproduced locally under `--cpus=1` and fixed separately. * fix(cloudfront): wildcard aliases match exactly one subdomain level wildcardAliasMatches did a plain suffix check, so "*.example.com" also matched a.b.example.com. AWS is explicit that a wildcard alternate domain name replaces the wildcard at that level only: "you can't add alternate domain names that are at levels higher or lower than the wildcard. For example, you can't add the alternate domain names example.com or marketing.product.example.com." https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html Practical effect: a distribution owning "*.example.com" would answer for a.b.example.com, which on AWS belongs to whoever owns "*.b.example.com" — or to nobody. Require the label standing in for the wildcard to contain no dot. wildcardAliasesMatchSubdomainsAndPreferMoreSpecificNames asserted the old behaviour (a.b.example.test resolving to the *.example.test distribution); it now asserts no match, plus the same for a deeper wildcard. * test(dynamodb): retain Quarkus port for cleanup * fix(cloudfront): isolate mocked storage backends * fix(ec2): wire managed prefix list storage in public IP test * fix(cloudfront): allow authorization origin custom headers * fix(cloudfront): preserve S3 preflight CORS headers
This commit is contained in:
1 parent
520c0f4da2
commit
f2ad05ce22
12 files changed
+979
-17
No files matched your search
+5
@@ -73,6 +73,9 @@ class CloudFormationCloudFrontDistributionIntegrationTest {
|
||||
"Id": "s3-origin",
|
||||
"DomainName": "%s.s3.us-east-1.amazonaws.com",
|
||||
"OriginAccessControlId": "%s",
|
||||
"OriginCustomHeaders": [
|
||||
{"HeaderName": "X-Origin-Verify", "HeaderValue": "cfn-secret"}
|
||||
],
|
||||
"S3OriginConfig": { "OriginAccessIdentity": "" }
|
||||
}
|
||||
],
|
||||
@@ -128,6 +131,8 @@ class CloudFormationCloudFrontDistributionIntegrationTest {
|
||||
assertEquals(
|
||||
oac.getId(),
|
||||
provisioned.getConfig().getOrigins().getFirst().getOriginAccessControlId());
|
||||
assertEquals(List.of(Map.of("HeaderName", "X-Origin-Verify", "HeaderValue", "cfn-secret")),
|
||||
provisioned.getConfig().getOrigins().getFirst().getCustomHeaders());
|
||||
assertEquals("60669652-455b-4ae9-85a4-c4c02393f86c",
|
||||
provisioned.getConfig().getDefaultCacheBehavior().getResponseHeadersPolicyId());
|
||||
assertEquals(List.of("67f7725c-6f97-4210-82d7-5512b31e9d03"),
|
||||
|
||||
+16
-2
@@ -50,8 +50,11 @@ class CloudFrontCustomOriginServingTest {
|
||||
void omitsQueryByDefaultAndForwardsResponseHeadersAndHeadMetadata() throws Exception {
|
||||
AtomicReference<String> receivedQuery = new AtomicReference<>();
|
||||
AtomicReference<String> receivedPath = new AtomicReference<>();
|
||||
AtomicReference<String> receivedCustomHeader = new AtomicReference<>();
|
||||
AtomicReference<String> receivedExpectHeader = new AtomicReference<>();
|
||||
originServer = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
originServer.createContext("/", exchange -> respond(exchange, receivedQuery, receivedPath));
|
||||
originServer.createContext("/", exchange ->
|
||||
respond(exchange, receivedQuery, receivedPath, receivedCustomHeader, receivedExpectHeader));
|
||||
originServer.start();
|
||||
|
||||
Origin customOrigin = new Origin();
|
||||
@@ -59,6 +62,11 @@ class CloudFrontCustomOriginServingTest {
|
||||
// The embedded port is deliberately wrong. CustomOriginConfig.HTTPPort is authoritative.
|
||||
customOrigin.setDomainName("127.0.0.1:1");
|
||||
customOrigin.setCustomOriginConfig(customOriginConfig(originServer.getAddress().getPort()));
|
||||
customOrigin.setCustomHeaders(List.of(
|
||||
new LinkedHashMap<>(Map.of(
|
||||
"HeaderName", "X-Origin-Verify", "HeaderValue", "shared-secret-42")),
|
||||
new LinkedHashMap<>(Map.of(
|
||||
"HeaderName", "Expect", "HeaderValue", "100-continue"))));
|
||||
|
||||
DistributionConfig config = new DistributionConfig();
|
||||
config.setEnabled(true);
|
||||
@@ -84,6 +92,8 @@ class CloudFrontCustomOriginServingTest {
|
||||
.header("X-Origin-Header", equalTo("preserved"));
|
||||
|
||||
assertNull(receivedQuery.get());
|
||||
assertEquals("shared-secret-42", receivedCustomHeader.get());
|
||||
assertEquals("100-continue", receivedExpectHeader.get());
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
@@ -211,9 +221,13 @@ class CloudFrontCustomOriginServingTest {
|
||||
}
|
||||
|
||||
private static void respond(HttpExchange exchange, AtomicReference<String> receivedQuery,
|
||||
AtomicReference<String> receivedPath) throws IOException {
|
||||
AtomicReference<String> receivedPath,
|
||||
AtomicReference<String> receivedCustomHeader,
|
||||
AtomicReference<String> receivedExpectHeader) throws IOException {
|
||||
receivedQuery.set(exchange.getRequestURI().getRawQuery());
|
||||
receivedPath.set(exchange.getRequestURI().getRawPath());
|
||||
receivedCustomHeader.set(exchange.getRequestHeaders().getFirst("X-Origin-Verify"));
|
||||
receivedExpectHeader.set(exchange.getRequestHeaders().getFirst("Expect"));
|
||||
exchange.getResponseHeaders().add("Content-Type", "text/plain");
|
||||
exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "https://viewer.example");
|
||||
exchange.getResponseHeaders().add("Cache-Control", "public, max-age=60");
|
||||
|
||||
+384
@@ -781,6 +781,390 @@ class CloudFrontDistributionServingTest {
|
||||
assertDistributionWasNotCreated(callerReference);
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundTripsOriginCustomHeadersThroughTheApi() {
|
||||
// The distribution parser must capture and re-serialize origin CustomHeaders so a read-back
|
||||
// (get-distribution) matches what was configured, rather than dropping them.
|
||||
String body = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
|
||||
<CallerReference>cf-custom-headers-roundtrip</CallerReference>
|
||||
<Origins>
|
||||
<Quantity>1</Quantity>
|
||||
<Items>
|
||||
<Origin>
|
||||
<Id>secured</Id>
|
||||
<DomainName>api.internal.example</DomainName>
|
||||
<OriginPath></OriginPath>
|
||||
<CustomHeaders>
|
||||
<Quantity>1</Quantity>
|
||||
<Items>
|
||||
<OriginCustomHeader>
|
||||
<HeaderName>X-Origin-Verify</HeaderName>
|
||||
<HeaderValue>shared-secret-42</HeaderValue>
|
||||
</OriginCustomHeader>
|
||||
</Items>
|
||||
</CustomHeaders>
|
||||
<CustomOriginConfig>
|
||||
<HTTPPort>80</HTTPPort>
|
||||
<HTTPSPort>443</HTTPSPort>
|
||||
<OriginProtocolPolicy>https-only</OriginProtocolPolicy>
|
||||
</CustomOriginConfig>
|
||||
</Origin>
|
||||
</Items>
|
||||
</Origins>
|
||||
<DefaultCacheBehavior>
|
||||
<TargetOriginId>secured</TargetOriginId>
|
||||
<ViewerProtocolPolicy>https-only</ViewerProtocolPolicy>
|
||||
</DefaultCacheBehavior>
|
||||
<Comment>custom-headers round-trip</Comment>
|
||||
<Enabled>true</Enabled>
|
||||
</DistributionConfig>
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/2020-05-31/distribution")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body(containsString("<OriginCustomHeader>"))
|
||||
.body(containsString("<HeaderName>X-Origin-Verify</HeaderName>"))
|
||||
.body(containsString("<HeaderValue>shared-secret-42</HeaderValue>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsProhibitedOriginCustomHeaders() {
|
||||
String body = """
|
||||
<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
|
||||
<CallerReference>cf-prohibited-custom-header</CallerReference>
|
||||
<Origins><Quantity>1</Quantity><Items><Origin>
|
||||
<Id>secured</Id><DomainName>example.com</DomainName>
|
||||
<CustomHeaders><Quantity>1</Quantity><Items><OriginCustomHeader>
|
||||
<HeaderName>Host</HeaderName><HeaderValue>internal.example</HeaderValue>
|
||||
</OriginCustomHeader></Items></CustomHeaders>
|
||||
<CustomOriginConfig><HTTPPort>80</HTTPPort><HTTPSPort>443</HTTPSPort>
|
||||
<OriginProtocolPolicy>https-only</OriginProtocolPolicy></CustomOriginConfig>
|
||||
</Origin></Items></Origins>
|
||||
<DefaultCacheBehavior><TargetOriginId>secured</TargetOriginId>
|
||||
<ViewerProtocolPolicy>https-only</ViewerProtocolPolicy></DefaultCacheBehavior>
|
||||
<Enabled>true</Enabled>
|
||||
</DistributionConfig>
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/2020-05-31/distribution")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidArgument</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsAuthorizationOriginCustomHeaders() {
|
||||
String body = distributionWithOriginCustomHeaders(1, """
|
||||
<OriginCustomHeader>
|
||||
<HeaderName>Authorization</HeaderName>
|
||||
<HeaderValue>Bearer secret</HeaderValue>
|
||||
</OriginCustomHeader>
|
||||
""").replace("cf-custom-header-validation", "cf-authorization-custom-header");
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/2020-05-31/distribution")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body(containsString("<HeaderName>Authorization</HeaderName>"))
|
||||
.body(containsString("<HeaderValue>Bearer secret</HeaderValue>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsInconsistentOriginCustomHeaderQuantity() {
|
||||
String body = distributionWithOriginCustomHeaders(2, """
|
||||
<OriginCustomHeader>
|
||||
<HeaderName>X-Origin-Verify</HeaderName>
|
||||
<HeaderValue>shared-secret</HeaderValue>
|
||||
</OriginCustomHeader>
|
||||
""");
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/2020-05-31/distribution")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InconsistentQuantities</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializesEmptyOriginCustomHeadersWithZeroQuantity() {
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(distributionWithOriginCustomHeaders(0, "", false))
|
||||
.when()
|
||||
.post("/2020-05-31/distribution")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body(containsString("<CustomHeaders><Quantity>0</Quantity></CustomHeaders>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedOriginCustomHeaderStructure() {
|
||||
String item = """
|
||||
<OriginCustomHeader>
|
||||
<HeaderName>X-Origin-Verify</HeaderName>
|
||||
<HeaderValue>shared-secret</HeaderValue>
|
||||
</OriginCustomHeader>
|
||||
""";
|
||||
List<String> malformedBodies = List.of(
|
||||
distributionWithOriginCustomHeaders(1, item, false),
|
||||
distributionWithOriginCustomHeaders(1, """
|
||||
<Header>
|
||||
<HeaderName>X-Origin-Verify</HeaderName>
|
||||
<HeaderValue>shared-secret</HeaderValue>
|
||||
</Header>
|
||||
"""),
|
||||
distributionWithOriginCustomHeaders(1, """
|
||||
<OriginCustomHeader>
|
||||
<HeaderName><Value>X-Origin-Verify</Value></HeaderName>
|
||||
<HeaderValue>shared-secret</HeaderValue>
|
||||
</OriginCustomHeader>
|
||||
"""));
|
||||
|
||||
for (String body : malformedBodies) {
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/2020-05-31/distribution")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidArgument</Code>"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsTooManyOriginCustomHeadersWithAwsErrorCode() {
|
||||
StringBuilder items = new StringBuilder();
|
||||
for (int i = 0; i < 31; i++) {
|
||||
items.append("""
|
||||
<OriginCustomHeader>
|
||||
<HeaderName>X-Origin-%d</HeaderName>
|
||||
<HeaderValue>value</HeaderValue>
|
||||
</OriginCustomHeader>
|
||||
""".formatted(i));
|
||||
}
|
||||
String body = distributionWithOriginCustomHeaders(31, items.toString());
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/2020-05-31/distribution")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>TooManyOriginCustomHeaders</Code>"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A rule whose {@code AllowedOrigin} is {@code *} echoes {@code *}, and the CORS specification
|
||||
* forbids pairing that with {@code Access-Control-Allow-Credentials: true} — browsers reject the
|
||||
* combination for credentialed requests, so emitting it would break in the browser while passing
|
||||
* here. Verified against real S3: a wildcard rule returns only Allow-Origin, Allow-Methods and
|
||||
* Max-Age, with no Allow-Credentials and no Vary; a concrete origin returns both (covered by
|
||||
* {@link #appliesConfiguredOriginHeaderToS3CorsResponses()}).
|
||||
*/
|
||||
@Test
|
||||
void wildcardCorsOriginOmitsCredentialsAndVary() {
|
||||
String suffix = suffix();
|
||||
String bucket = "cf-origin-header-cors-wild-" + suffix;
|
||||
createBucket(bucket);
|
||||
putObject(bucket, "asset.txt", "cors-body", "text/plain");
|
||||
s3Service.putBucketCors(bucket, """
|
||||
<CORSConfiguration>
|
||||
<CORSRule>
|
||||
<AllowedOrigin>*</AllowedOrigin>
|
||||
<AllowedMethod>GET</AllowedMethod>
|
||||
<AllowedMethod>HEAD</AllowedMethod>
|
||||
<MaxAgeSeconds>600</MaxAgeSeconds>
|
||||
</CORSRule>
|
||||
</CORSConfiguration>
|
||||
""");
|
||||
|
||||
Origin origin = s3Origin("s3-origin", bucket);
|
||||
origin.setCustomHeaders(List.of(new LinkedHashMap<>(Map.of(
|
||||
"HeaderName", "Origin",
|
||||
"HeaderValue", "https://configured.example"))));
|
||||
DistributionConfig cfg = new DistributionConfig();
|
||||
cfg.setEnabled(true);
|
||||
cfg.setOrigins(List.of(origin));
|
||||
DefaultCacheBehavior behavior = defaultBehavior(origin.getId());
|
||||
behavior.setAllowedMethods(List.of("GET", "HEAD", "OPTIONS"));
|
||||
cfg.setDefaultCacheBehavior(behavior);
|
||||
Distribution dist = cloudFrontService.createDistribution(distribution(cfg), Map.of());
|
||||
|
||||
given()
|
||||
.header("Host", dist.getDomainName())
|
||||
.header("Origin", "https://viewer.example")
|
||||
.when()
|
||||
.get("/asset.txt")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Access-Control-Allow-Origin", equalTo("*"))
|
||||
.header("Access-Control-Allow-Methods", equalTo("GET, HEAD"))
|
||||
.header("Access-Control-Max-Age", equalTo("600"))
|
||||
.header("Access-Control-Allow-Credentials", nullValue())
|
||||
.header("Vary", nullValue());
|
||||
|
||||
given()
|
||||
.header("Host", dist.getDomainName())
|
||||
.header("Origin", "https://viewer.example")
|
||||
.header("Access-Control-Request-Method", "GET")
|
||||
.when()
|
||||
.options("/asset.txt")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Access-Control-Allow-Origin", equalTo("*"))
|
||||
.header("Access-Control-Allow-Methods", equalTo("GET, HEAD"))
|
||||
.header("Access-Control-Max-Age", equalTo("600"))
|
||||
.header("Access-Control-Allow-Credentials", nullValue())
|
||||
.header("Vary", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void appliesConfiguredOriginHeaderToS3CorsResponses() {
|
||||
String suffix = suffix();
|
||||
String bucket = "cf-origin-header-cors-" + suffix;
|
||||
createBucket(bucket);
|
||||
putObject(bucket, "asset.txt", "cors-body", "text/plain");
|
||||
s3Service.putBucketCors(bucket, """
|
||||
<CORSConfiguration>
|
||||
<CORSRule>
|
||||
<AllowedOrigin>https://configured.example</AllowedOrigin>
|
||||
<AllowedMethod>GET</AllowedMethod>
|
||||
<AllowedMethod>HEAD</AllowedMethod>
|
||||
<ExposeHeader>ETag</ExposeHeader>
|
||||
<MaxAgeSeconds>600</MaxAgeSeconds>
|
||||
</CORSRule>
|
||||
</CORSConfiguration>
|
||||
""");
|
||||
|
||||
Origin origin = s3Origin("s3-origin", bucket);
|
||||
origin.setCustomHeaders(List.of(new LinkedHashMap<>(Map.of(
|
||||
"HeaderName", "Origin",
|
||||
"HeaderValue", "https://configured.example"))));
|
||||
DistributionConfig cfg = new DistributionConfig();
|
||||
cfg.setEnabled(true);
|
||||
cfg.setOrigins(List.of(origin));
|
||||
cfg.setDefaultCacheBehavior(defaultBehavior(origin.getId()));
|
||||
Distribution dist = cloudFrontService.createDistribution(distribution(cfg), Map.of());
|
||||
|
||||
given()
|
||||
.header("Host", dist.getDomainName())
|
||||
.header("Origin", "https://viewer.example")
|
||||
.when()
|
||||
.get("/asset.txt")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Access-Control-Allow-Origin", equalTo("https://configured.example"))
|
||||
.header("Access-Control-Allow-Methods", equalTo("GET, HEAD"))
|
||||
.header("Access-Control-Allow-Credentials", equalTo("true"))
|
||||
.header("Access-Control-Max-Age", equalTo("600"))
|
||||
.header("Access-Control-Expose-Headers", equalTo("ETag"))
|
||||
.header("Vary", containsString("Origin"))
|
||||
.header("Vary", containsString("Access-Control-Request-Headers"))
|
||||
.header("Vary", containsString("Access-Control-Request-Method"));
|
||||
|
||||
given()
|
||||
.header("Host", dist.getDomainName())
|
||||
.header("Origin", "https://viewer.example")
|
||||
.when()
|
||||
.head("/asset.txt")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Access-Control-Allow-Origin", equalTo("https://configured.example"))
|
||||
.header("Access-Control-Allow-Methods", equalTo("GET, HEAD"))
|
||||
.header("Access-Control-Allow-Credentials", equalTo("true"))
|
||||
.header("Access-Control-Max-Age", equalTo("600"))
|
||||
.header("Access-Control-Expose-Headers", equalTo("ETag"))
|
||||
.header("Vary", containsString("Origin"))
|
||||
.header("Vary", containsString("Access-Control-Request-Headers"))
|
||||
.header("Vary", containsString("Access-Control-Request-Method"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void appliesConfiguredOriginHeaderToS3CorsPreflight() {
|
||||
String suffix = suffix();
|
||||
String bucket = "cf-origin-header-preflight-" + suffix;
|
||||
createBucket(bucket);
|
||||
s3Service.putBucketCors(bucket, """
|
||||
<CORSConfiguration>
|
||||
<CORSRule>
|
||||
<AllowedOrigin>https://configured.example</AllowedOrigin>
|
||||
<AllowedMethod>GET</AllowedMethod>
|
||||
<AllowedHeader>Authorization</AllowedHeader>
|
||||
</CORSRule>
|
||||
</CORSConfiguration>
|
||||
""");
|
||||
|
||||
Origin origin = s3Origin("s3-origin", bucket);
|
||||
origin.setCustomHeaders(List.of(new LinkedHashMap<>(Map.of(
|
||||
"HeaderName", "Origin",
|
||||
"HeaderValue", "https://configured.example"))));
|
||||
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", "Authorization")
|
||||
.when().options("/asset.txt")
|
||||
.then().statusCode(200)
|
||||
.header("Access-Control-Allow-Origin", equalTo("https://configured.example"))
|
||||
.header("Access-Control-Allow-Methods", equalTo("GET"))
|
||||
.header("Access-Control-Allow-Headers", equalTo("Authorization"))
|
||||
.header("Access-Control-Allow-Credentials", equalTo("true"))
|
||||
.header("Vary", containsString("Origin"))
|
||||
.header("Vary", containsString("Access-Control-Request-Headers"))
|
||||
.header("Vary", containsString("Access-Control-Request-Method"));
|
||||
}
|
||||
|
||||
private static String distributionWithOriginCustomHeaders(int quantity, String items) {
|
||||
return distributionWithOriginCustomHeaders(quantity, items, true);
|
||||
}
|
||||
|
||||
private static String distributionWithOriginCustomHeaders(
|
||||
int quantity, String items, boolean includeItems) {
|
||||
String itemList = includeItems ? "<Items>" + items + "</Items>" : items;
|
||||
return """
|
||||
<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2020-05-31/">
|
||||
<CallerReference>cf-custom-header-validation</CallerReference>
|
||||
<Origins><Quantity>1</Quantity><Items><Origin>
|
||||
<Id>secured</Id><DomainName>example.com</DomainName>
|
||||
<CustomHeaders><Quantity>%d</Quantity>%s</CustomHeaders>
|
||||
<CustomOriginConfig><HTTPPort>80</HTTPPort><HTTPSPort>443</HTTPSPort>
|
||||
<OriginProtocolPolicy>https-only</OriginProtocolPolicy></CustomOriginConfig>
|
||||
</Origin></Items></Origins>
|
||||
<DefaultCacheBehavior><TargetOriginId>secured</TargetOriginId>
|
||||
<ViewerProtocolPolicy>https-only</ViewerProtocolPolicy></DefaultCacheBehavior>
|
||||
<Enabled>true</Enabled>
|
||||
</DistributionConfig>
|
||||
""".formatted(quantity, itemList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void appliesResponseHeadersPolicyToServedResponses() {
|
||||
String suffix = suffix();
|
||||
|
||||
+32
@@ -28,6 +28,7 @@ import java.security.Security;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@@ -141,6 +142,37 @@ class CloudFrontOriginHttpClientTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void originHeadersReplaceSameNamedRequestHeaders() throws Exception {
|
||||
AtomicReference<List<String>> receivedValues = new AtomicReference<>();
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/", exchange -> {
|
||||
receivedValues.set(exchange.getRequestHeaders().get("X-Origin-Verify"));
|
||||
exchange.sendResponseHeaders(200, -1);
|
||||
exchange.close();
|
||||
});
|
||||
server.start();
|
||||
|
||||
try (CloudFrontOriginHttpClient client = new CloudFrontOriginHttpClient(
|
||||
resolver(InetAddress.getByName("127.0.0.1")), List.of("origin.invalid"))) {
|
||||
HttpRequest request = HttpRequest.newBuilder(java.net.URI.create(
|
||||
"http://origin.invalid:" + server.getAddress().getPort() + "/"))
|
||||
.timeout(Duration.ofSeconds(5))
|
||||
.header("X-Origin-Verify", "viewer-value")
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
client.send(
|
||||
request,
|
||||
Map.of("X-Origin-Verify", "configured-value"),
|
||||
HttpResponse.BodyHandlers.ofByteArray());
|
||||
|
||||
assertEquals(List.of("configured-value"), receivedValues.get());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpsPinningPreservesSniAndHostnameVerification() throws Exception {
|
||||
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
|
||||
|
||||
+44
@@ -531,6 +531,49 @@ class CloudFrontServiceTest {
|
||||
assertEquals(409, keyGroupError.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsInvalidOriginCustomHeaderNamesAndValues() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
List<List<Map<String, String>>> invalidHeaders = List.of(
|
||||
List.of(header("X-Duplicate", "one"), header("x-duplicate", "two")),
|
||||
List.of(header("X-Amz-Reserved", "value")),
|
||||
List.of(header("X-Edge-Reserved", "value")),
|
||||
List.of(header("Bad Header", "value")),
|
||||
List.of(header("", "value")),
|
||||
List.of(header("X-Missing-Value", null)),
|
||||
List.of(header("X-Injected", "value\r\nInjected: true")),
|
||||
List.of(header("X".repeat(257), "value")),
|
||||
List.of(header("X-Too-Long", "v".repeat(1_784))),
|
||||
List.of(
|
||||
header("X-Quota-1", "v".repeat(1_783)),
|
||||
header("X-Quota-2", "v".repeat(1_783)),
|
||||
header("X-Quota-3", "v".repeat(1_783)),
|
||||
header("X-Quota-4", "v".repeat(1_783)),
|
||||
header("X-Quota-5", "v".repeat(1_783)),
|
||||
header("X-Quota-6", "v".repeat(1_783))));
|
||||
|
||||
for (int i = 0; i < invalidHeaders.size(); i++) {
|
||||
Distribution candidate = distribution(false, List.of());
|
||||
Origin origin = new Origin();
|
||||
origin.setCustomHeaders(invalidHeaders.get(i));
|
||||
candidate.getConfig().setOrigins(List.of(origin));
|
||||
|
||||
AwsException error = assertThrows(
|
||||
AwsException.class,
|
||||
() -> service.createDistribution(candidate, Map.of()),
|
||||
"validation case " + i);
|
||||
assertEquals("InvalidArgument", error.getErrorCode(), "validation case " + i);
|
||||
assertEquals(400, error.getHttpStatus(), "validation case " + i);
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, String> header(String name, String value) {
|
||||
Map<String, String> header = new LinkedHashMap<>();
|
||||
header.put("HeaderName", name);
|
||||
header.put("HeaderValue", value);
|
||||
return header;
|
||||
}
|
||||
|
||||
@Test
|
||||
void exposesCanonicalManagedResponseHeadersPolicies() {
|
||||
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
|
||||
@@ -704,6 +747,7 @@ class CloudFrontServiceTest {
|
||||
assertEquals(code, error.getErrorCode());
|
||||
return error;
|
||||
}
|
||||
|
||||
private static Distribution distribution(boolean enabled, List<String> aliases) {
|
||||
DistributionConfig config = new DistributionConfig();
|
||||
config.setEnabled(enabled);
|
||||
|
||||
Reference in new issue
Block a user