fix(s3): derive S3 host suffix matching from DNS config (#1843)

* fix(s3): derive S3 host suffix matching from DNS config

S3VirtualHostFilter hardcoded the wildcard-DNS suffixes (localhost.floci.io,
localhost.localstack.cloud) in two places: the bare service-host check
(isS3ServiceEndpointHost) and the virtual-hosted bucket fallback (isAwsS3Domain).
Both now derive their suffix set from the embedded DNS source of truth plus any
configured floci.dns.extra-suffixes, so a custom DNS suffix is recognised the same
way the builtins are.

Why: the two literal lists duplicated what the DNS server already knows and could
drift from it silently. Deriving from EmbeddedDnsServer.BUILTIN_SUFFIXES keeps them
in lockstep, and folding in extra-suffixes means a user migrating from LocalStack
via a custom suffix gets consistent S3 host handling (bucket routing and service
endpoint detection), not just the DNS resolution they get today.

How:
- buildServiceHostSuffixes() computes {localhost} + BUILTIN_SUFFIXES + extra-suffixes
  once per instance; isS3ServiceEndpointHost() checks membership instead of literals.
- matchesConfiguredSuffixHost() routes the virtual-hosted forms bucket.<suffix>,
  bucket.s3.<suffix> and bucket.s3.<region>.<suffix> for every configured suffix,
  replacing the hardcoded localstack/floci branches in isAwsS3Domain.
- EmbeddedDnsServer.BUILTIN_SUFFIXES widened to public so the filter reuses the list.

The virtual-host match now requires the full builtin suffix (.localhost.localstack.cloud)
rather than the bare parent domain (.localstack.cloud), matching LocalStack's actual
wildcard DNS (*.localhost.localstack.cloud).

* test(s3): derive DEFAULT_SUFFIXES from the production factory

The test constant hardcoded a copy of the builtin suffix set, which would
silently drift if a new builtin were added to EmbeddedDnsServer.BUILTIN_SUFFIXES.
Widen buildServiceHostSuffixes() to package-private and reuse it in the test so
the two stay in sync automatically.

* fix(s3): include the configured hostname in the derived suffix set

EmbeddedDnsServer resolves builtins + floci.hostname + floci.dns.extra-suffixes,
but the derived service-host set carried only two of the three. The configured
hostname reaches the filter as baseHostname, and matchesEndpointHost excludes the
bare s3.<host> remainder by design, so the bucket form one level down
(bucket.s3.<hostname>) matched nothing. DNS still resolves that host, so the
request reached Floci and was then handled path-style, binding the first path
segment as the bucket, rather than being refused.

Carry the hostname into buildServiceHostSuffixes so all three DNS inputs are
mirrored and a host that resolves by wildcard DNS routes under the same rules.
This commit is contained in:
TiM authored and GitHub committed 2026-08-13 22:45:37 -04:00
1 parent 97ac3cabb5
commit 188588b4eb
3 files changed
+188 -34

No files matched your search

@@ -66,7 +66,7 @@ public class EmbeddedDnsServer {
// NOT cover "*.X" (e.g. "localhost.floci.io" does NOT resolve bare "*.floci.io").
// localhost.localstack.cloud → localhost.localstack.cloud, *.localhost.localstack.cloud
// localhost.floci.io → localhost.floci.io, *.localhost.floci.io
static final List<String> BUILTIN_SUFFIXES = List.of(DEFAULT_SUFFIX, LOCALSTACK_SUFFIX);
public static final List<String> BUILTIN_SUFFIXES = List.of(DEFAULT_SUFFIX, LOCALSTACK_SUFFIX);
private volatile String serverIp;
private final SequencedSet<String> suffixes = new LinkedHashSet<>();
@@ -13,6 +13,10 @@ import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.ext.Provider;
import java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@Provider
@PreMatching
@@ -21,16 +25,45 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
private final String baseHostname;
/**
* Hostname suffixes for which a bare {@code s3.<suffix>} Host header is Floci's own
* S3 service endpoint (bucketless) rather than a bucket literally named {@code s3}.
* Derived from the same source of truth the embedded DNS server uses: the always-on
* builtins ({@code localhost.floci.io}, {@code localhost.localstack.cloud}) plus the
* configured {@code floci.hostname} and any {@code floci.dns.extra-suffixes},
* alongside plain {@code localhost}. Stored lowercase for case-insensitive matching.
*/
private final Set<String> serviceHostSuffixes;
@Inject
public S3VirtualHostFilter(EmulatorConfig config, ContainerDetector containerDetector) {
this.baseHostname = config.hostname()
.orElseGet(() -> containerDetector.isRunningInContainer()
? EmbeddedDnsServer.DEFAULT_SUFFIX
: extractHostnameFromUrl(config.baseUrl()));
this.serviceHostSuffixes = buildServiceHostSuffixes(config.hostname(), config.dns().extraSuffixes());
}
S3VirtualHostFilter() {
this.baseHostname = "localhost";
this.serviceHostSuffixes = buildServiceHostSuffixes(Optional.empty(), Optional.empty());
}
/**
* Builds the service-host suffix set from the DNS source of truth rather than
* re-hardcoding it: {@code {"localhost"}} plus the {@link EmbeddedDnsServer} builtins
* plus the configured hostname and any extra suffixes. The three configured inputs
* mirror what {@code EmbeddedDnsServer} makes resolvable, so a host that reaches Floci
* by wildcard DNS is routed by the same rules it resolved under. Package-private so
* tests reuse the same derivation instead of duplicating the builtin list.
*/
static Set<String> buildServiceHostSuffixes(Optional<String> hostname, Optional<List<String>> extraSuffixes) {
Set<String> suffixes = new HashSet<>();
suffixes.add("localhost");
EmbeddedDnsServer.BUILTIN_SUFFIXES.forEach(s -> suffixes.add(s.toLowerCase()));
hostname.ifPresent(h -> suffixes.add(h.toLowerCase()));
extraSuffixes.ifPresent(list -> list.forEach(s -> suffixes.add(s.toLowerCase())));
return Set.copyOf(suffixes);
}
@Override
@@ -59,7 +92,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return;
}
String bucket = extractBucket(host, baseHostname);
String bucket = extractBucket(host, baseHostname, serviceHostSuffixes);
if (bucket == null) return;
String path = uri.getRawPath();
@@ -125,7 +158,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
*
* Returns null if the host does not match a virtual-hosted pattern.
*/
static String extractBucket(String host, String baseHostname) {
static String extractBucket(String host, String baseHostname, Set<String> serviceHostSuffixes) {
if (host == null) {
return null;
}
@@ -147,7 +180,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
String firstLabel = hostname.substring(0, firstDot);
String remainder = hostname.substring(firstDot + 1);
if (isS3ServiceEndpointHost(firstLabel, remainder, baseHostname)) {
if (isS3ServiceEndpointHost(firstLabel, remainder, baseHostname, serviceHostSuffixes)) {
return null;
}
@@ -162,20 +195,45 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return firstLabel;
}
// Configured Floci DNS suffixes (builtins + floci.dns.extra-suffixes): route
// bucket.<suffix>, bucket.s3.<suffix> and bucket.s3.<region>.<suffix> the same
// way the always-on wildcard-DNS builtins resolve.
if (matchesConfiguredSuffixHost(remainder, serviceHostSuffixes)) {
return firstLabel;
}
return null;
}
private static boolean isS3ServiceEndpointHost(String firstLabel, String remainder, String baseHostname) {
/**
* Matches the virtual-hosted bucket forms for a configured DNS suffix {@code S}
* (a builtin, {@code floci.hostname}, or a {@code floci.dns.extra-suffix}): {@code bucket.S},
* {@code bucket.s3.S}, and the region-qualified {@code bucket.s3.<region>.S}.
* Plain {@code localhost} is skipped here — it keeps its dedicated
* {@link #matchesEndpointHost} handling via {@link #isAwsS3Domain}.
*/
private static boolean matchesConfiguredSuffixHost(String remainder, Set<String> serviceHostSuffixes) {
String lower = remainder.toLowerCase();
for (String suffix : serviceHostSuffixes) {
if ("localhost".equals(suffix)) {
continue;
}
if (lower.equals(suffix) || (lower.startsWith("s3.") && lower.endsWith("." + suffix))) {
return true;
}
}
return false;
}
static boolean isS3ServiceEndpointHost(String firstLabel, String remainder, String baseHostname,
Set<String> serviceHostSuffixes) {
if (!"s3".equalsIgnoreCase(firstLabel)) {
return false;
}
if (baseHostname != null && matchesEndpointHost(remainder, baseHostname)) {
return true;
}
String lowerRemainder = remainder.toLowerCase();
return "localhost".equals(lowerRemainder)
|| "localhost.localstack.cloud".equals(lowerRemainder)
|| "localhost.floci.io".equals(lowerRemainder);
return serviceHostSuffixes.contains(remainder.toLowerCase());
}
/**
@@ -230,7 +288,13 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return true;
}
/** Returns true for *.s3.amazonaws.com and other well-known S3 domains. */
/**
* Returns true for well-known AWS S3 domains and the always-on {@code localhost}
* endpoint. The Floci wildcard-DNS builtins ({@code localhost.floci.io},
* {@code localhost.localstack.cloud}) and any configured {@code floci.dns.extra-suffixes}
* are handled by {@link #matchesConfiguredSuffixHost} instead, so they stay derived
* from the DNS source of truth rather than hardcoded here.
*/
private static boolean isAwsS3Domain(String remainder) {
if ("s3.amazonaws.com".equals(remainder)) {
return true;
@@ -246,14 +310,6 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
if (matchesEndpointHost(remainder, "localhost")) {
return true;
}
// LocalStack public wildcard DNS: bucket.s3.localhost.localstack.cloud and bucket.localhost.localstack.cloud
if (remainder.endsWith(".localstack.cloud")) {
return remainder.startsWith("s3.") || "localhost.localstack.cloud".equals(remainder);
}
// Floci public wildcard DNS: bucket.s3.localhost.floci.io and bucket.localhost.floci.io → 127.0.0.1
if (remainder.endsWith(".localhost.floci.io") || "localhost.floci.io".equals(remainder)) {
return remainder.startsWith("s3.") || "localhost.floci.io".equals(remainder);
}
// S3 website endpoints: bucket.s3-website-<region>.amazonaws.com, bucket.s3-website-<region>.localhost, etc.
if (remainder.startsWith("s3-website")) {
return true;