mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): resolve virtual-hosted buckets whose names contain dots (#2493)
* fix(s3): resolve virtual-hosted buckets whose names contain dots
S3VirtualHostFilter.extractBucket split the Host header at the FIRST dot
and required the remainder to equal the endpoint host. S3 bucket names may
legally contain dots -- naming a website/log bucket after a domain is the
conventional pattern -- so for
Host: r5e817e.floci.example.com-logs.localhost
it read the bucket as "r5e817e" with remainder "floci.example.com-logs.
localhost", which matches no known endpoint host. extractBucket returned
null, the request silently degraded to a path-style request against "/",
and HEAD / answered 200 (the ListBuckets route).
User-visible symptom: the Terraform AWS provider calls HeadBucket before
CreateBucket, read that 200 as "bucket exists", and failed every dotted
bucket with BucketAlreadyExists for a bucket that had never existed.
Match the endpoint host as a SUFFIX instead: the bucket is everything in
front of ".<endpoint-host>", minus an optional trailing S3 qualifier
(s3, s3.<region>, s3.dualstack.<region>, s3-website-<region>). The same
correction is applied to every resolution path -- configured base hostname,
always-on localhost, configured DNS suffixes, and the AWS-domain fallback --
so bucket.<host>, bucket.s3.<host> and bucket.s3.<region>.<host> all keep
working with dotted names. Where several known suffixes match, the longest
wins, so bucket.localhost.floci.io is not read as "bucket.localhost".
Two things must not be swallowed by the wider matching:
* Service endpoints. The existing isS3ServiceEndpointHost guard still runs
first, and a prefix that IS a qualifier (s3.localhost,
s3.us-east-1.localhost, s3-website-us-east-1.localhost) resolves to no
bucket rather than to a bucket named "s3".
* Other services' virtual hosts. execute-api, lambda-url and friends share
the endpoint host, and <api-id>.execute-api.<host> would otherwise read as
a dotted bucket and hijack API Gateway requests. Hostnames carrying a
non-S3 service label are not buckets.
Path-style hosts, IPv4 authorities and port stripping are unchanged.
* fix(s3): stop suffix matching from swallowing other services' regional hosts
Suffix matching reads any multi-label host under a known endpoint host as a
dotted bucket. The label denylist added with it is not a sound guard: it has to
name every service, and the first one it misses is silently served as a bucket.
Measured against the previous commit, with baseHostname=localhost — every one of
these resolved to null on main:
123456789012.dkr.ecr.us-east-1.localhost -> 123456789012.dkr.ecr.us-east-1
search-mydomain-abc.us-east-1.es.localhost -> search-mydomain-abc.us-east-1.es
abc.iot.us-east-1.localhost -> abc.iot.us-east-1
myapp.appsync-api.us-east-1.localhost -> myapp.appsync-api.us-east-1
The first is the registry URI EcrRegistryManager hands out, so it is not
hypothetical.
Replace the enumeration with a structural rule: a hostname carrying a region
label in a non-leading position and no S3 qualifier is another service's
regional virtual host. A real S3 virtual host always carries s3, s3.<region> or
s3-website-<region>, and when it does the region is read from there instead — so
a bucket whose own name ends in a region stays addressable as
logs.us-east-1.s3.localhost.
Drop "elb" and "mwaa" from the denylist in the other direction. No filter in this
repository routes *.elb.<host> or *.mwaa.<host> by Host, and my.elb.logs is AWS's
own load-balancer access-log naming convention — a perfectly legal bucket the
guard was rejecting for nothing. The four remaining labels each name a filter
that does claim that hostname in a regionless form the region rule cannot catch.
S3VirtualHostFilterTest: 117 -> 131 tests.
* test(s3): pin what the remaining service-label denylist costs
An automated reviewer flagged that a bucket named my.execute-api.archive is
unreachable virtual-hosted. That is true, and it is the intended side of the
trade rather than an oversight, so assert it instead of leaving it implicit.
The remedy suggested — validate each service's hostname grammar and let anything
that does not match through — trades a recoverable failure for an unrecoverable
one. A bucket that must be addressed path-style still works, and worked no
better before this PR. An API Gateway request answered by S3 is a silent
cross-service hijack with no client-side remedy. Each of the four labels names a
filter in this repository that claims that hostname; the two that named nothing
were removed in the previous commit.
S3VirtualHostFilterTest: 131 -> 135 tests.
* fix(s3): treat an S3 qualifier as a complete tail, not any label
bucketFromPrefix scanned the labels in front of the endpoint host one at a
time and split the bucket at the LAST label matching s3 or s3-website*. An
"s3" label anywhere inside a dotted bucket name therefore truncated it:
Host: my.s3.archive.localhost -> bucket "my"
A request for the bucket my.s3.archive silently read and wrote a different,
existing bucket. That is worse in kind than the wrong 200 this branch set out
to fix -- the client gets a plausible success against the wrong data, with no
signal that anything went wrong.
A qualifier is a complete tail, not a label. isS3QualifierTail requires the
labels from index i onwards to account for the WHOLE remainder of the prefix
and to spell one of the endpoint forms the AWS General Reference publishes:
s3 s3-fips.<region> s3-accelerate
s3.<region> s3-fips.dualstack.<region> s3-accelerate.dualstack
s3.dualstack.<region> s3-<region> (legacy dash form)
s3-website s3-website.<region> (newer regions)
s3-website-<region> (older regions)
Nothing legal follows "s3" except a region, dualstack.<region>, or the end of
the name, so "s3.archive" is bucket text and my.s3.archive is the bucket. The
genuine forms still split, including for a bucket that contains an s3 label:
my.s3.archive.s3.us-east-1.localhost resolves to my.s3.archive.
Stating the grammar properly also widens what is recognised. s3-fips.<region>,
s3-accelerate, the legacy s3-<region> dash form and the newer s3-website.<region>
dot form were previously unrecognised; they resolve now. Note the website
endpoint spells its region with a dot in newer regions and a dash in older ones,
and both are in service.
Recognising s3-<region> needs to tell a region id from arbitrary text, so
AwsRegions gains KNOWN_IDS: every published AWS region id across the commercial,
GovCloud and China partitions. It is deliberately distinct from AwsRegions.ALL
and a superset of it -- ALL is what the emulator ADVERTISES through
DescribeRegions, KNOWN_IDS is what it RECOGNISES in a hostname. AwsRegionsTest
pins ALL as a subset of KNOWN_IDS.
The remaining ambiguity is narrower than before and is asserted: a bucket whose
own name IS a complete qualifier tail (foo.s3, foo.s3.us-east-1) is still read
as a qualified reference to foo. A bucket that merely contains s3 is not.
S3VirtualHostFilterTest: 135 -> 155 tests. AwsRegionsTest: new, 14 tests.
* fix(s3): match region IDs by name, not by a region-shaped pattern
The guard that keeps another service's regional virtual host from being served
as a dotted bucket tested each non-leading label against
[a-z]{2}-[a-z-]+-\d+
That is the shape of an AWS region id. It is also the shape of perfectly
ordinary bucket labels. my-cd-1 matches it, so the legal bucket data.my-cd-1
was rejected, fell back to path-style, and answered HEAD / with 200 -- exactly
the false "bucket exists" this branch was opened to eliminate, reintroduced for
a different class of name. eu-team-2, us-west-9, ap-corp-1 and no-such-region-12
all match it too.
Test against AwsRegions.KNOWN_IDS instead: the finite list of region ids AWS
actually publishes. data.my-cd-1, reports.eu-team-2, assets.us-west-9 and
logs.ap-corp-1.archive now resolve as buckets, while
123456789012.dkr.ecr.us-east-1.localhost and search-x.eu-north-1.es.localhost
are still refused -- including for regions outside the set the emulator
advertises.
Position within the host was considered as an extra discriminator and rejected:
it does not separate the two cases. The region label is last in
<acct>.dkr.ecr.<region> but medial in <domain>.<region>.es, so no positional
rule covers both without also covering bucket names.
Two residual costs, both accepted deliberately and documented at the call site:
* A bucket carrying a REAL region id in a non-leading position (data.us-east-1)
is still unreachable in the unqualified virtual-hosted form. It stays
reachable qualified (data.us-east-1.s3.localhost) and path-style, and it was
equally unreachable before this branch. Recoverable, and asserted.
* A regional service host in a region AWS launches after KNOWN_IDS was last
updated would be read as a bucket. That is the maintenance cost of a list over
a pattern, and the direction in which the pattern was safer. It is bounded:
the failure is a wrong route rather than data loss, and the four
NON_S3_SERVICE_LABELS entries still cover the services Floci routes by Host.
The list is preferred because the pattern's failure mode is the one this branch
exists to eliminate -- a legal bucket silently answering 200 from the service
root -- while the list's failure mode only appears for a region AWS has not
launched yet.
S3VirtualHostFilterTest: 155 -> 167 tests.
This commit is contained in:
1 parent
bd36b02728
commit
ea2b62fb95
4 files changed
+767
-58
No files matched your search
@@ -0,0 +1,47 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class AwsRegionsTest {
|
||||
|
||||
/**
|
||||
* {@code ALL} is what the emulator advertises; {@code KNOWN_IDS} is what it recognises. The
|
||||
* second must contain the first, or DescribeRegions could name a region that hostname parsing
|
||||
* refuses to read back.
|
||||
*/
|
||||
@Test
|
||||
void everyAdvertisedRegionIsAKnownRegionId() {
|
||||
for (String region : AwsRegions.ALL) {
|
||||
assertTrue(AwsRegions.KNOWN_IDS.contains(region),
|
||||
region + " is advertised by DescribeRegions but is not a known region id");
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"us-east-1", "eu-west-1", "ap-northeast-3", "us-gov-west-1",
|
||||
"cn-northwest-1", "il-central-1", "US-EAST-1"})
|
||||
void realRegionIdsAreRecognised(String label) {
|
||||
assertTrue(AwsRegions.isRegionId(label));
|
||||
}
|
||||
|
||||
/**
|
||||
* The point of an id list over a pattern: these all match the region <em>shape</em>
|
||||
* {@code [a-z]{2}-[a-z-]+-\d+} and none of them is a region. Treating them as regions is what
|
||||
* made {@code data.my-cd-1} unreachable as an S3 bucket.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"my-cd-1", "eu-team-2", "us-west-9", "ap-corp-1", "no-such-region-12"})
|
||||
void regionShapedStringsThatAreNotRegionsAreRejected(String label) {
|
||||
assertFalse(AwsRegions.isRegionId(label));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullIsNotARegionId() {
|
||||
assertFalse(AwsRegions.isRegionId(null));
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -307,4 +308,390 @@ class S3VirtualHostFilterTest {
|
||||
// ...but the s3-website authority survives, so downstream website detection still fires.
|
||||
assertEquals("my-bucket.s3-website-us-east-1.localhost:4566", newUri.getAuthority());
|
||||
}
|
||||
|
||||
// --- Dotted bucket names: the bucket is everything before the endpoint suffix (#s3-vhost) ---
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
// Single-label bucket — unchanged behaviour
|
||||
"my-bucket.localhost, localhost, my-bucket",
|
||||
"my-bucket.localhost:4566, localhost, my-bucket",
|
||||
// Dotted bucket: a website bucket named after a domain
|
||||
"www.example.com.localhost, localhost, www.example.com",
|
||||
"www.example.com.localhost:4566, localhost, www.example.com",
|
||||
// Deeply dotted bucket
|
||||
"a.b.c.d.localhost, localhost, a.b.c.d",
|
||||
"a.b.c.d.localhost:4611, localhost, a.b.c.d",
|
||||
// Dots AND hyphens — the exact shape from the Terraform BucketAlreadyExists report
|
||||
"r5e817e.floci.example.com-logs.localhost, localhost, r5e817e.floci.example.com-logs",
|
||||
"one.dot-nonexistent.localhost, localhost, one.dot-nonexistent",
|
||||
"a.b.c-nonexistent.localhost, localhost, a.b.c-nonexistent",
|
||||
// Dotted bucket against a multi-label configured base hostname
|
||||
"www.example.com.floci.internal, floci.internal, www.example.com",
|
||||
"www.example.com.floci.default.svc.cluster.local, floci.default.svc.cluster.local, www.example.com",
|
||||
// Dotted bucket, region-qualified vhost form
|
||||
"www.example.com.s3.us-east-1.localhost, localhost, www.example.com",
|
||||
"www.example.com.s3.us-east-1.localhost:4566, localhost, www.example.com",
|
||||
"www.example.com.s3.amazonaws.com, localhost, www.example.com",
|
||||
"www.example.com.s3.us-east-1.amazonaws.com, localhost, www.example.com",
|
||||
"www.example.com.s3.dualstack.us-east-1.amazonaws.com, localhost, www.example.com",
|
||||
// Dotted bucket on the wildcard-DNS builtins
|
||||
"www.example.com.localhost.floci.io, localhost, www.example.com",
|
||||
"www.example.com.s3.localhost.floci.io, localhost, www.example.com",
|
||||
"www.example.com.s3.us-east-1.localhost.localstack.cloud, localhost, www.example.com",
|
||||
// Dotted bucket on a website endpoint
|
||||
"www.example.com.s3-website-us-east-1.localhost, localhost, www.example.com",
|
||||
"www.example.com.s3-website.localhost, localhost, www.example.com",
|
||||
// Case-insensitive suffix matching keeps the bucket's own case
|
||||
"www.Example.com.LOCALHOST, localhost, www.Example.com",
|
||||
})
|
||||
void extractsDottedBucketNames(String host, String baseHostname, String expectedBucket) {
|
||||
assertEquals(expectedBucket, S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void headOnDottedBucketIsNotDegradedToPathStyle() {
|
||||
// Regression: extractBucket split at the FIRST dot, so a dotted bucket's remainder
|
||||
// ("floci.example.com-logs.localhost") did not match baseHostname "localhost" and the
|
||||
// request degraded to path-style "/" — where HEAD answers 200. The Terraform AWS
|
||||
// provider HEADs a bucket before creating it and read that 200 as "already exists",
|
||||
// failing with BucketAlreadyExists for a bucket that never existed.
|
||||
assertEquals("floci.example.com-logs", S3VirtualHostFilter.extractBucket(
|
||||
"floci.example.com-logs.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
assertEquals("floci.example.com-logs", S3VirtualHostFilter.extractBucket(
|
||||
"floci.example.com-logs.localhost:4566", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
// --- The service-endpoint reading always wins over the new suffix matching ---
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
// s3.<endpoint> is Floci's own S3 endpoint, never a bucket named "s3"
|
||||
"s3.localhost, localhost",
|
||||
"s3.localhost:4566, localhost",
|
||||
"s3.floci.internal, floci.internal",
|
||||
// ...including the region-qualified service endpoint, which the guard alone misses
|
||||
"s3.us-east-1.localhost, localhost",
|
||||
"s3.us-east-1.localhost:4566, localhost",
|
||||
"s3.eu-west-2.localhost.floci.io, localhost",
|
||||
"s3.dualstack.us-east-1.amazonaws.com, localhost",
|
||||
"s3.amazonaws.com, localhost",
|
||||
"s3.us-east-1.amazonaws.com, localhost",
|
||||
// Bare website endpoint with no bucket in front
|
||||
"s3-website-us-east-1.localhost, localhost",
|
||||
// A non-S3 amazonaws.com host is not a bucket
|
||||
"foo.amazonaws.com, localhost",
|
||||
"queue.amazonaws.com:443, localhost",
|
||||
// Dotted hosts that still end in nothing we know
|
||||
"www.example.com.custom.internal, localhost",
|
||||
"a.b.c.d, localhost",
|
||||
})
|
||||
void serviceEndpointsAndUnknownSuffixesStayBucketless(String host, String baseHostname) {
|
||||
assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void longestKnownSuffixWinsSoBucketDoesNotAbsorbTheEndpoint() {
|
||||
// "localhost" and "localhost.floci.io" are both known; the longer one must match,
|
||||
// otherwise the bucket would come out as "my-bucket.localhost".
|
||||
assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
|
||||
"my-bucket.localhost.floci.io", "localhost", DEFAULT_SUFFIXES));
|
||||
assertEquals("www.example.com", S3VirtualHostFilter.extractBucket(
|
||||
"www.example.com.localhost.localstack.cloud", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dottedBucketsRouteOnConfiguredExtraSuffixes() {
|
||||
Set<String> withExtra = Set.of(
|
||||
"localhost", "localhost.floci.io", "localhost.localstack.cloud", "localhost.example.internal");
|
||||
assertEquals("www.example.com", S3VirtualHostFilter.extractBucket(
|
||||
"www.example.com.localhost.example.internal", "localhost", withExtra));
|
||||
assertEquals("www.example.com", S3VirtualHostFilter.extractBucket(
|
||||
"www.example.com.s3.localhost.example.internal:4566", "localhost", withExtra));
|
||||
assertEquals("www.example.com", S3VirtualHostFilter.extractBucket(
|
||||
"www.example.com.s3.us-east-1.localhost.example.internal", "localhost", withExtra));
|
||||
// Still bucketless for the bare service endpoint on that suffix
|
||||
assertNull(S3VirtualHostFilter.extractBucket(
|
||||
"s3.us-east-1.localhost.example.internal", "localhost", withExtra));
|
||||
// And no accidental match when the suffix is not configured
|
||||
assertNull(S3VirtualHostFilter.extractBucket(
|
||||
"www.example.com.localhost.example.internal", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ipv4AuthorityIsNeverABucketEvenWithADottedShape() {
|
||||
assertNull(S3VirtualHostFilter.extractBucket("192.168.1.1", "localhost", DEFAULT_SUFFIXES));
|
||||
assertNull(S3VirtualHostFilter.extractBucket("127.0.0.1:4566", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterRewritesPathForADottedBucket() {
|
||||
URI requestUri = URI.create("http://www.example.com.localhost:4566/index.html");
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
when(uriInfo.getRequestUri()).thenReturn(requestUri);
|
||||
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
|
||||
when(ctx.getUriInfo()).thenReturn(uriInfo);
|
||||
when(ctx.getHeaderString("Host")).thenReturn("www.example.com.localhost:4566");
|
||||
|
||||
new S3VirtualHostFilter().filter(ctx);
|
||||
|
||||
ArgumentCaptor<URI> rewritten = ArgumentCaptor.forClass(URI.class);
|
||||
verify(ctx).setRequestUri(rewritten.capture());
|
||||
assertEquals("/www.example.com/index.html", rewritten.getValue().getRawPath());
|
||||
}
|
||||
|
||||
// --- Other services' virtual-host schemes must not be swallowed as dotted buckets ---
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
// API Gateway: <api-id>.execute-api.<region>.<endpoint> and the wildcard-DNS forms
|
||||
"abc123.execute-api.localhost, localhost",
|
||||
"abc123.execute-api.localhost:4566, localhost",
|
||||
"abc123.execute-api.ap-northeast-2.localhost:4566, localhost",
|
||||
"abc123.execute-api.localhost.floci.io, localhost",
|
||||
"abc123.execute-api.localhost.localstack.cloud, localhost",
|
||||
"AbC123.execute-api.localhost.floci.io, localhost",
|
||||
"abc123.execute-api.us-east-1.amazonaws.com, localhost",
|
||||
// Lambda function URLs
|
||||
"url-id.lambda-url.us-east-1.localhost, localhost",
|
||||
"url-id.lambda-url.localhost:4566, localhost",
|
||||
// CloudFront distribution domains routed at the Floci endpoint
|
||||
"e123.cloudfront.localhost, localhost",
|
||||
})
|
||||
void otherServicesVirtualHostsAreNotBuckets(String host, String baseHostname) {
|
||||
assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceLabelInFirstPositionKeepsItsPreExistingBucketReading() {
|
||||
// emr-serverless.<endpoint> has no id in front; it was read as a bucket before the
|
||||
// dotted-bucket fix and still is, so this change alters nothing for it.
|
||||
assertEquals("emr-serverless",
|
||||
S3VirtualHostFilter.extractBucket("emr-serverless.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every regional AWS service virtual-hosts as {@code <id>.<service>.<region>.<endpoint>}, and
|
||||
* suffix matching reads all of them as a dotted bucket unless something stops it. A label
|
||||
* denylist cannot: it has to name each service, and the first one it misses is silently served
|
||||
* as an S3 bucket. The structural rule is that a real S3 virtual host never puts a bare region
|
||||
* label against the endpoint host — {@code s3}, {@code s3.<region>} or {@code s3-website-<region>}
|
||||
* is always there instead.
|
||||
*
|
||||
* <p>{@code <account>.dkr.ecr.<region>.localhost} is not hypothetical: it is the registry URI
|
||||
* {@code EcrRegistryManager} hands out.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
// ECR registry, as EcrRegistryManager emits it
|
||||
"123456789012.dkr.ecr.us-east-1.localhost, localhost",
|
||||
"123456789012.dkr.ecr.us-east-1.localhost:5100, localhost",
|
||||
"123456789012.dkr.ecr.eu-central-1.localhost.floci.io, localhost",
|
||||
// OpenSearch / Elasticsearch domain endpoints
|
||||
"search-mydomain-abc.us-east-1.es.localhost, localhost",
|
||||
// IoT data endpoints
|
||||
"abc123.iot.us-east-1.localhost, localhost",
|
||||
// AppSync
|
||||
"myapp.appsync-api.us-east-1.localhost, localhost",
|
||||
// Cognito hosted UI
|
||||
"mydomain.auth.eu-west-2.localhost, localhost",
|
||||
// A service Floci does not model yet still must not be swallowed
|
||||
"anything.some-future-service.ap-northeast-2.localhost, localhost",
|
||||
})
|
||||
void regionalServiceVirtualHostsAreNotDottedBuckets(String host, String baseHostname) {
|
||||
assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/**
|
||||
* The other direction: the guard must not cost legal bucket names. {@code my.elb.logs} is
|
||||
* AWS's own load-balancer access-log naming convention, and no filter in this repository
|
||||
* routes {@code *.elb.<endpoint>} or {@code *.mwaa.<endpoint>} by Host, so neither label
|
||||
* belongs in the denylist.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"my.elb.logs.localhost, my.elb.logs",
|
||||
"my.elb.logs.localhost:4566, my.elb.logs",
|
||||
"airflow.mwaa.dags.localhost, airflow.mwaa.dags",
|
||||
"my.elb.logs.s3.us-east-1.localhost, my.elb.logs",
|
||||
})
|
||||
void bucketNamesCarryingServiceWordsStillResolve(String host, String expectedBucket) {
|
||||
assertEquals(expectedBucket, S3VirtualHostFilter.extractBucket(host, "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/**
|
||||
* The region rule only applies when no S3 qualifier is present, so a bucket whose own name
|
||||
* ends in a region label is still addressable in the qualified forms.
|
||||
*/
|
||||
@Test
|
||||
void aBucketWhoseNameEndsInARegionLabelIsStillAddressableWhenQualified() {
|
||||
assertEquals("logs.us-east-1", S3VirtualHostFilter.extractBucket(
|
||||
"logs.us-east-1.s3.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
assertEquals("logs.us-east-1", S3VirtualHostFilter.extractBucket(
|
||||
"logs.us-east-1.s3.us-east-1.amazonaws.com", "localhost", DEFAULT_SUFFIXES));
|
||||
// Unqualified, it reads as another service's regional host — the documented trade-off.
|
||||
assertNull(S3VirtualHostFilter.extractBucket(
|
||||
"logs.us-east-1.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/** A single-label bucket is unaffected by the region rule, region-shaped or not. */
|
||||
@Test
|
||||
void singleLabelBucketsAreUntouchedByTheRegionRule() {
|
||||
assertEquals("us-east-1", S3VirtualHostFilter.extractBucket(
|
||||
"us-east-1.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
|
||||
"my-bucket.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/**
|
||||
* The cost of the four remaining denylist labels, pinned rather than left implicit: a bucket
|
||||
* whose name carries one of them in a non-leading position is not reachable virtual-hosted and
|
||||
* falls back to path-style, which is what it did before this change too.
|
||||
*
|
||||
* <p>That is the deliberate side of the trade. The alternative failure — validating each
|
||||
* service's hostname grammar and letting anything that does not match through — is a silent
|
||||
* cross-service hijack: S3 answering a request meant for API Gateway. A bucket that is
|
||||
* awkward to address virtual-hosted is recoverable by the client; an API that intermittently
|
||||
* returns S3 responses is not. Each label here names a filter in this repository that claims
|
||||
* that hostname, so the set stays small and justified.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"my.execute-api.archive.localhost, localhost",
|
||||
"my.lambda-url.archive.localhost, localhost",
|
||||
"my.emr-serverless.archive.localhost, localhost",
|
||||
"my.cloudfront.archive.localhost, localhost",
|
||||
})
|
||||
void bucketNamesCarryingADenylistedServiceLabelAreDeliberatelyNotReachable(String host, String baseHostname) {
|
||||
assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
// --- Regression: an internal "s3" label must not truncate the bucket ---
|
||||
|
||||
/**
|
||||
* A qualifier is a complete tail, not a label that may appear anywhere.
|
||||
*
|
||||
* <p>Scanning labels one at a time and splitting at the last {@code s3} reads
|
||||
* {@code my.s3.archive.localhost} as the bucket {@code my}: a request for
|
||||
* {@code my.s3.archive} silently reads and writes a <em>different, existing</em> bucket.
|
||||
* That is cross-bucket misrouting — strictly worse than the wrong 200 this filter was
|
||||
* written to fix, because the client gets a plausible success against the wrong data.
|
||||
*
|
||||
* <p>Nothing legal follows {@code s3} in an endpoint host except a region,
|
||||
* {@code dualstack.<region>}, or the end of the name, so an {@code s3} label followed by an
|
||||
* arbitrary label is part of the bucket.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"my.s3.archive.localhost, my.s3.archive",
|
||||
"my.s3.archive.localhost:4566, my.s3.archive",
|
||||
"www.s3.example.com.localhost, www.s3.example.com",
|
||||
"my.s3.archive.localhost.floci.io, my.s3.archive",
|
||||
// s3-website is a qualifier head too, and gets the same treatment
|
||||
"data.s3-website.archive.localhost, data.s3-website.archive",
|
||||
// ...and the region-shaped tail is not a region id, so it is bucket text as well
|
||||
"my.s3.not-a-region.localhost, my.s3.not-a-region",
|
||||
})
|
||||
void anInternalS3LabelDoesNotTruncateTheBucket(String host, String expectedBucket) {
|
||||
assertEquals(expectedBucket, S3VirtualHostFilter.extractBucket(host, "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/** The same host, stated as the misrouting it would otherwise be. */
|
||||
@Test
|
||||
void aDottedBucketContainingS3IsNeverResolvedToAShorterBucket() {
|
||||
String bucket = S3VirtualHostFilter.extractBucket(
|
||||
"my.s3.archive.localhost", "localhost", DEFAULT_SUFFIXES);
|
||||
assertNotEquals("my", bucket, "bucket my.s3.archive must not resolve to the bucket my");
|
||||
assertEquals("my.s3.archive", bucket);
|
||||
}
|
||||
|
||||
/**
|
||||
* A real qualifier still splits, including for a dotted bucket that itself contains an
|
||||
* {@code s3} label — {@code my.s3.archive} addressed region-qualified.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"my.s3.localhost, my",
|
||||
"my.s3.us-east-1.localhost, my",
|
||||
"my.s3.archive.s3.localhost, my.s3.archive",
|
||||
"my.s3.archive.s3.us-east-1.localhost, my.s3.archive",
|
||||
"my.s3.archive.s3.dualstack.eu-west-1.localhost, my.s3.archive",
|
||||
})
|
||||
void aCompleteQualifierTailStillSplitsTheBucket(String host, String expectedBucket) {
|
||||
assertEquals(expectedBucket, S3VirtualHostFilter.extractBucket(host, "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/**
|
||||
* The published S3 endpoint qualifier forms, from the Amazon S3 endpoint tables in the AWS
|
||||
* General Reference. The website endpoint spells its region with a dot in newer regions and a
|
||||
* dash in older ones, and both are in service, so both are accepted.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"www.example.com.s3-website.eu-west-2.localhost, www.example.com",
|
||||
"www.example.com.s3-website-eu-west-1.localhost, www.example.com",
|
||||
"my.bucket.s3-fips.us-east-1.localhost, my.bucket",
|
||||
"my.bucket.s3-fips.dualstack.us-east-1.amazonaws.com, my.bucket",
|
||||
"my.bucket.s3-accelerate.amazonaws.com, my.bucket",
|
||||
"my.bucket.s3-accelerate.dualstack.amazonaws.com, my.bucket",
|
||||
"my.bucket.s3-us-west-2.amazonaws.com, my.bucket",
|
||||
"my.bucket.s3.dualstack.ap-northeast-3.amazonaws.com, my.bucket",
|
||||
})
|
||||
void everyPublishedQualifierFormResolvesTheBucket(String host, String expectedBucket) {
|
||||
assertEquals(expectedBucket, S3VirtualHostFilter.extractBucket(host, "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
// --- Regression: the region guard must reject region IDS, not region SHAPES ---
|
||||
|
||||
/**
|
||||
* {@code [a-z]{2}-[a-z-]+-\d+} is the shape of an AWS region id, but it is also the shape of
|
||||
* perfectly ordinary bucket labels. Matching by shape rejected {@code data.my-cd-1} — a legal
|
||||
* bucket — dropped it back to path-style, and restored the exact false "bucket exists" 200
|
||||
* this filter exists to prevent. The guard has to test against the finite list of real region
|
||||
* ids instead.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"data.my-cd-1.localhost, data.my-cd-1",
|
||||
"data.my-cd-1.localhost:4566, data.my-cd-1",
|
||||
"reports.eu-team-2.localhost, reports.eu-team-2",
|
||||
"assets.us-west-9.localhost, assets.us-west-9",
|
||||
"logs.ap-corp-1.archive.localhost, logs.ap-corp-1.archive",
|
||||
"backup.no-such-region-12.localhost, backup.no-such-region-12",
|
||||
})
|
||||
void regionShapedBucketLabelsThatAreNotRegionIdsStillResolve(String host, String expectedBucket) {
|
||||
assertEquals(expectedBucket, S3VirtualHostFilter.extractBucket(host, "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/**
|
||||
* The guard it replaces is still doing its job: a real region id in a non-leading label, with
|
||||
* no S3 qualifier, is another service's regional virtual host. Tightening to real ids must not
|
||||
* weaken this — including for regions outside the set the emulator advertises.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"123456789012.dkr.ecr.us-east-1.localhost, localhost",
|
||||
"search-x.eu-north-1.es.localhost, localhost",
|
||||
"abc.iot.me-central-1.localhost, localhost",
|
||||
"abc.transfer.us-gov-west-1.localhost, localhost",
|
||||
"abc.service.cn-northwest-1.localhost, localhost",
|
||||
})
|
||||
void realRegionIdsStillMarkAnotherServicesRegionalHost(String host, String baseHostname) {
|
||||
assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
/**
|
||||
* The residual cost of the region rule, pinned rather than left implicit: a bucket whose name
|
||||
* carries a <em>real</em> region id in a non-leading position is still not reachable in the
|
||||
* unqualified virtual-hosted form. It stays reachable qualified and path-style, and it was
|
||||
* equally unreachable before this filter existed — so the cost is a recoverable one, paid to
|
||||
* avoid an unrecoverable cross-service hijack.
|
||||
*/
|
||||
@Test
|
||||
void aBucketNamedAfterARealRegionIsStillTheDocumentedCost() {
|
||||
assertNull(S3VirtualHostFilter.extractBucket(
|
||||
"data.us-east-1.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
assertEquals("data.us-east-1", S3VirtualHostFilter.extractBucket(
|
||||
"data.us-east-1.s3.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user