fix(s3,sns): preserve root service-host routing (#1625)

* fix(s3): preserve ListBuckets on S3 service hosts

- Treat bare s3.* service endpoint hosts as bucketless S3 requests

- Keep virtual-hosted bucket parsing for bucket.s3.* and bucket.localhost forms

- Add parser and integration coverage for LocalStack and Floci S3 service host domains

* fix(sns): route SubscribeURL confirmations to SNS

- Route SNS ConfirmSubscription GET requests on / through the SNS query handler before normal S3 root handling
- Preserve S3 ListBuckets behavior for non-SNS root GET requests
- Add SNS HTTP delivery regressions for inline 200 confirmation and SubscribeURL GET confirmation

* test(sns): fix race in inline-confirm HTTP delivery test

The confirming-webhook handler recorded the received request before running
confirmSubscription(), so awaitRequests() unblocked and the test published
before the subscription was actually confirmed, delivering 0 notifications
under load (flaky on CI). Record the request only after confirmSubscription()
completes, making awaitRequests() a real barrier. Log the confirmation failure
instead of swallowing it so a genuine failure is diagnosable.

* fix(sns): route UnsubscribeURL confirmations to SNS

UnsubscribeURL is emitted as a plain GET link against the service root,
the same endpoint collision with S3 ListBuckets that SubscribeURL had.
Extend the root-GET delegation to forward Action=Unsubscribe to the SNS
query handler alongside ConfirmSubscription, closing the whole class of
gap rather than a single action.

These links are unsigned, so the region cannot come from the request auth
header. Derive it from the region-bearing ARN the link carries
(SubscriptionArn for Unsubscribe, TopicArn for ConfirmSubscription), so a
subscription in a non-default region resolves instead of returning NotFound.

- Delegate Unsubscribe GETs on / to SnsQueryHandler in S3Controller
- Recover the SNS region from the link ARN via AwsArnUtils.regionOrDefault
- Add a regression in us-west-2: the UnsubscribeURL GET reaches SNS
  (UnsubscribeResponse, not ListBuckets) and the subscription is removed
This commit is contained in:
TiM authored and GitHub committed 2026-07-02 23:44:14 -05:00
1 parent 345e83c34d
commit 2a36522094
5 files changed
+295 -1

No files matched your search

@@ -2,11 +2,13 @@ package io.github.hectorvent.floci.services.s3;
import static io.github.hectorvent.floci.services.s3.S3RequestParser.hasQueryParam;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.sns.SnsQueryHandler;
import io.github.hectorvent.floci.services.s3.model.Bucket;
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesParts;
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;
@@ -29,6 +31,7 @@ import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.StreamingOutput;
import jakarta.ws.rs.core.UriInfo;
@@ -86,17 +89,20 @@ public class S3Controller {
private final S3Service s3Service;
private final S3SelectService s3SelectService;
private final RegionResolver regionResolver;
private final SnsQueryHandler snsQueryHandler;
private final io.quarkus.vertx.http.runtime.CurrentVertxRequest currentVertxRequest;
private final io.github.hectorvent.floci.services.floci.ui.UiPages uiPages;
@Inject
public S3Controller(S3Service s3Service, S3SelectService s3SelectService,
RegionResolver regionResolver,
SnsQueryHandler snsQueryHandler,
io.quarkus.vertx.http.runtime.CurrentVertxRequest currentVertxRequest,
io.github.hectorvent.floci.services.floci.ui.UiPages uiPages) {
this.s3Service = s3Service;
this.s3SelectService = s3SelectService;
this.regionResolver = regionResolver;
this.snsQueryHandler = snsQueryHandler;
this.currentVertxRequest = currentVertxRequest;
this.uiPages = uiPages;
}
@@ -106,10 +112,26 @@ public class S3Controller {
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_HTML})
public Response listBuckets(@HeaderParam("X-Amz-Target") String target,
@HeaderParam("Accept") String accept) {
@HeaderParam("Accept") String accept,
@Context HttpHeaders httpHeaders,
@Context UriInfo uriInfo) {
if (target != null) {
return null;
}
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String action = queryParams.getFirst("Action");
// SNS emits SubscribeURL/UnsubscribeURL as plain GET links against the
// service root, which collides with the S3 ListBuckets endpoint. Delegate
// those confirmation/unsubscribe GETs to SNS before falling through to S3.
// The links are unsigned (no Authorization header), so the region must come
// from the region-bearing ARN they carry, not the request headers.
if ("ConfirmSubscription".equals(action) || "Unsubscribe".equals(action)) {
String snsArn = "Unsubscribe".equals(action)
? queryParams.getFirst("SubscriptionArn")
: queryParams.getFirst("TopicArn");
String region = AwsArnUtils.regionOrDefault(snsArn, regionResolver.resolveRegion(httpHeaders));
return snsQueryHandler.handle(action, queryParams, region);
}
// A browser hitting the root endpoint (Accept: text/html) gets the Floci
// landing page; SDK/CLI callers (no Accept, */*, or an XML/JSON Accept) fall
// through to the normal S3 ListBuckets behavior untouched.
@@ -115,6 +115,10 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
String firstLabel = hostname.substring(0, firstDot);
String remainder = hostname.substring(firstDot + 1);
if (isS3ServiceEndpointHost(firstLabel, remainder, baseHostname)) {
return null;
}
// Primary: remainder must match the configured base hostname,
// either directly or in the AWS region-qualified s3.<region>.<host> form.
if (baseHostname != null && matchesEndpointHost(remainder, baseHostname)) {
@@ -129,6 +133,19 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return null;
}
private static boolean isS3ServiceEndpointHost(String firstLabel, String remainder, String baseHostname) {
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);
}
/**
* Matches a hostname directly or its region-qualified s3.&lt;region&gt;.&lt;hostname&gt; variant.
* Example: with hostname="localhost", both "localhost" and "s3.us-east-1.localhost" match.