Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(core): refuse a SigV4 scope region that no AWS partition publishes (#4393)

Hector Ventura6fcfc0450ad7
diff --git a/README.md b/README.md
index 821da6dd87..02052703ae 100644
--- a/README.md
+++ b/README.md
@@ -81,7 +81,7 @@ This short demo shows the CLI flow: start Floci, export the local AWS environmen
https://github.com/user-attachments/assets/b55714dc-ef36-40ae-a734-cd2cadc288a8
-All AWS services are available at `http://localhost:4566`. Any region works. Credentials can be any non-empty values unless you explicitly enable stricter service-specific auth checks.
+All AWS services are available at `http://localhost:4566`. Any [published AWS region](https://floci.io/floci/configuration/partitions/) works, in every partition. Credentials can be any non-empty values unless you explicitly enable stricter service-specific auth checks.
<details>
<summary>Prefer Docker Compose?</summary>
diff --git a/docs/configuration/advanced/application-yml.md b/docs/configuration/advanced/application-yml.md
index 86352a6bf5..aa2bb8dd10 100644
--- a/docs/configuration/advanced/application-yml.md
+++ b/docs/configuration/advanced/application-yml.md
@@ -42,6 +42,7 @@ floci:
default-account-id: "000000000000"
partitions:
# id: aws # Pin the partition explicitly; derived from default-region when unset
+ allow-unknown-regions: false # true serves a scope region no partition publishes or matches by pattern
storage:
mode: memory # memory | persistent | hybrid | wal
diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md
index 5e88eb590d..e9f4f2a771 100644
--- a/docs/configuration/environment-variables.md
+++ b/docs/configuration/environment-variables.md
@@ -12,6 +12,7 @@ Floci is configured exclusively through environment variables. Every option belo
| `FLOCI_HOSTNAME` | _(none)_ | Overrides only the hostname part of `FLOCI_BASE_URL`. Set to the Compose service name (e.g. `floci`) so other containers can reach Floci by DNS |
| `FLOCI_DEFAULT_REGION` | `us-east-1` | AWS region used in ARNs and API responses when a request names none. Also selects the deployment's partition (`cn-north-1` means `aws-cn`), see [AWS Partitions](./partitions.md) |
| `FLOCI_PARTITIONS_ID` | _(derived)_ | Pins the partition (`aws`, `aws-cn`, `aws-us-gov`, `aws-iso`, `aws-iso-b`, `aws-iso-e`, `aws-iso-f`, `aws-eusc`). Startup refuses a value that contradicts `FLOCI_DEFAULT_REGION` |
+| `FLOCI_PARTITIONS_ALLOW_UNKNOWN_REGIONS` | `false` | Accept a request signed for a region no AWS partition publishes or admits by its region pattern (`polygondwanaland-west-1`), giving the label its own namespace. Off, such a request is refused with a 400, as moto and LocalStack refuse it. See [AWS Partitions](./partitions.md#which-partition-a-request-belongs-to) |
| `FLOCI_DEFAULT_ACCOUNT_ID` | `000000000000` | Fallback account ID used in ARNs when the request's access key is not exactly 12 digits. When the access key IS 12 digits, it is used directly as the account ID — see [Multi-Account Isolation](./multi-account.md) |
| `FLOCI_DEFAULT_AVAILABILITY_ZONE` | `us-east-1a` | Availability zone reported in EC2 and other responses |
diff --git a/docs/configuration/partitions.md b/docs/configuration/partitions.md
index d1bcbe0afd..559d437474 100644
--- a/docs/configuration/partitions.md
+++ b/docs/configuration/partitions.md
@@ -47,6 +47,17 @@ resources: China IAM always signs `cn-north-1` even from Ningxia, GovCloud IAM s
`<partition>-global` pseudo-regions the SDKs accept (`aws-global`, `aws-cn-global`, ...)
resolve to their partition as well; `aws-eusc` publishes none.
+A scope region that no partition publishes or admits by its region pattern, such as
+`polygondwanaland-west-1`, is refused with a 400: an S3-signed request gets S3's
+`AuthorizationHeaderMalformed` ("the region '...' is wrong"), every other service gets
+`InvalidSignatureException`. On AWS such a request never resolves a host; moto
+(`MOTO_ALLOW_NONEXISTENT_REGION`) and LocalStack (`ALLOW_NONSTANDARD_REGIONS`) refuse it too.
+The pattern rule is the AWS SDKs' own, so a region AWS launches after the vendored data was
+refreshed (`eu-south-9`, say) is still served; it is looser than S3's `LocationConstraint` enum,
+which stays published-only. Set `FLOCI_PARTITIONS_ALLOW_UNKNOWN_REGIONS=true`
+(`floci.partitions.allow-unknown-regions`) to serve any label with its own namespace, as Floci
+did before.
+
## What changes per partition
- **ARNs**: regional resources carry the partition of their region (`arn:aws-cn:sqs:cn-north-1:...`);
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index a8558cbf5d..0856df3f9f 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -73,6 +73,16 @@ public interface EmulatorConfig {
*/
interface PartitionsConfig {
Optional<String> id();
+
+ /**
+ * Accept a request whose SigV4 credential scope names a region that no partition
+ * publishes or admits by its region pattern ({@code polygondwanaland-west-1}). Refused by
+ * default, as moto ({@code MOTO_ALLOW_NONEXISTENT_REGION}) and LocalStack
+ * ({@code ALLOW_NONSTANDARD_REGIONS}) do, because on AWS such a request never resolves a
+ * host; set this to give every label its own region namespace, as Floci did before.
+ */
+ @WithDefault("false")
+ boolean allowUnknownRegions();
}
/**
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AccountContextFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/AccountContextFilter.java
index bf06347265..6527806150 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/AccountContextFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AccountContextFilter.java
@@ -1,14 +1,19 @@
package io.github.hectorvent.floci.core.common;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.services.s3.PreSignedUrlFilter;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.Provider;
+import org.jboss.logging.Logger;
import java.util.Optional;
+import java.util.Set;
/**
* Populates {@link RequestContext} with the account ID and region derived from
@@ -33,20 +38,31 @@ public class AccountContextFilter implements ContainerRequestFilter {
*/
public static final String PINNED_ACCOUNT_PROPERTY = AccountContextFilter.class.getName() + ".accountId";
+ private static final Logger LOG = Logger.getLogger(AccountContextFilter.class);
+
+ /** The signing names S3 answers to; an S3 client expects S3's XML error document. */
+ private static final Set<String> S3_SIGNING_NAMES = Set.of("s3", "s3express");
+
private final AccountResolver accountResolver;
private final RegionResolver regionResolver;
private final RequestContext requestContext;
private final SessionAccountLookup sessionAccountLookup;
+ // Lazily resolved: JAX-RS providers are instantiated before runtime config mappings exist
+ // (the AwsProtocolClaimFilter pattern). jakarta.inject.Provider is qualified inline because
+ // this class also carries the JAX-RS @Provider annotation.
+ private final jakarta.inject.Provider<EmulatorConfig> configProvider;
@Inject
public AccountContextFilter(AccountResolver accountResolver,
RegionResolver regionResolver,
RequestContext requestContext,
- SessionAccountLookup sessionAccountLookup) {
+ SessionAccountLookup sessionAccountLookup,
+ jakarta.inject.Provider<EmulatorConfig> configProvider) {
this.accountResolver = accountResolver;
this.regionResolver = regionResolver;
this.requestContext = requestContext;
this.sessionAccountLookup = sessionAccountLookup;
+ this.configProvider = configProvider;
}
@Override
@@ -61,14 +77,18 @@ public class AccountContextFilter implements ContainerRequestFilter {
if (auth != null && !auth.isEmpty()) {
String akid = accountResolver.extractAccessKeyId(auth);
requestContext.setAccountId(resolveAccount(akid, accountResolver.resolve(auth)));
- applyRegion(regionResolver.resolveRegionFromAuth(auth));
+ String region = regionResolver.resolveRegionFromAuth(auth);
+ applyRegion(region);
+ rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceName(auth));
} else {
String credential = ctx.getUriInfo().getQueryParameters().getFirst("X-Amz-Credential");
if (credential != null && !credential.isEmpty()) {
String akid = accountResolver.extractPresignedAccessKeyId(credential);
requestContext.setAccountId(
resolveAccount(akid, accountResolver.resolveFromPresignedCredential(credential)));
- applyRegion(regionResolver.resolveRegionFromPresignedCredential(credential));
+ String region = regionResolver.resolveRegionFromPresignedCredential(credential);
+ applyRegion(region);
+ rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceNameFromCredential(credential));
} else {
requestContext.setAccountId(accountResolver.resolve(null));
applyRegion(regionResolver.resolveRegionFromAuth(null));
@@ -86,6 +106,43 @@ public class AccountContextFilter implements ContainerRequestFilter {
requestContext.setPartition(regionResolver.partitionForRegion(region));
}
+ /**
+ * A scope region no partition publishes or admits by its region pattern is refused, as moto
+ * and LocalStack refuse it by default: on AWS such a label never resolves a host, and served
+ * here it would mint ARNs and a storage namespace for a region that does not exist. The
+ * region and partition stay on the request context so the error mappers see them. S3
+ * clients get S3's own XML {@code AuthorizationHeaderMalformed} (the wording S3 and minio use
+ * for a wrong scope region). Everyone else gets {@code InvalidSignatureException}, as a Query
+ * {@code <ErrorResponse>} for a form-encoded request and as JSON otherwise: this filter runs
+ * before the protocol claim, and a Query SDK cannot parse a JSON error body.
+ */
+ private void rejectUnknownRegion(ContainerRequestContext ctx, String region, Optional<String> signingName) {
+ if (RegionResolver.isKnownRegion(region) || configProvider.get().partitions().allowUnknownRegions()) {
+ return;
+ }
+ LOG.debugv("Refusing request signed for unknown region {0} (service {1}): {2} {3}",
+ region, signingName.orElse("?"), ctx.getMethod(), ctx.getUriInfo().getPath());
+ if (signingName.isPresent() && S3_SIGNING_NAMES.contains(signingName.get())) {
+ ctx.abortWith(PreSignedUrlFilter.errorResponse(400, "AuthorizationHeaderMalformed",
+ "The authorization header is malformed; the region '" + region
+ + "' is wrong; expecting a region AWS publishes."));
+ return;
+ }
+ String message = "Region '" + region + "' is not a region in any AWS partition. Sign the request "
+ + "for a published region, or set floci.partitions.allow-unknown-regions=true.";
+ if (isFormEncoded(ctx.getMediaType())) {
+ ctx.abortWith(AwsQueryResponse.error("InvalidSignatureException", message, null, 400));
+ return;
+ }
+ ctx.abortWith(AwsProtocolClaimFilter.errorResponse(400, "InvalidSignatureException", message));
+ }
+
+ private static boolean isFormEncoded(MediaType mediaType) {
+ return mediaType != null
+ && "application".equalsIgnoreCase(mediaType.getType())
+ && "x-www-form-urlencoded".equalsIgnoreCase(mediaType.getSubtype());
+ }
+
/**
* Applies the account-resolution precedence. A 12-digit AKID is already reflected in
* {@code resolvedDefault} (the account or default returned by {@link AccountResolver});
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsProtocolClaimFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsProtocolClaimFilter.java
index 7d873896cb..abb1fb33c4 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsProtocolClaimFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsProtocolClaimFilter.java
@@ -108,12 +108,17 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {
}
static Response unknownOperationResponse(int status, String message) {
+ return errorResponse(status, "UnknownOperationException", message);
+ }
+
+ /** A JSON error every SDK maps to its typed or generic service exception by {@code code}. */
+ static Response errorResponse(int status, String code, String message) {
return Response.status(status)
.type(MediaType.APPLICATION_JSON)
- .header("x-amzn-query-error", "UnknownOperationException;Sender")
+ .header("x-amzn-query-error", code + ";Sender")
// rest-json SDKs resolve the error code from this header before the body __type
- .header("X-Amzn-Errortype", "UnknownOperationException")
- .entity(new AwsErrorResponse("UnknownOperationException", message))
+ .header("X-Amzn-Errortype", code)
+ .entity(new AwsErrorResponse(code, message))
.build();
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java
index 003b8b9059..8a703e0f6e 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java
@@ -86,6 +86,11 @@ public class RegionResolver {
return resolveRegionFromAuth(headers.getHeaderString("Authorization"));
}
+ /**
+ * The region label the SigV4 credential scope names, unvalidated: {@code AccountContextFilter}
+ * decides whether an unknown label is served or refused (see {@link #isKnownRegion}), and
+ * needs the raw label either way so the rejection can name it.
+ */
public String resolveRegionFromAuth(String authorizationHeader) {
if (authorizationHeader == null || authorizationHeader.isEmpty()) {
return defaultRegion;
@@ -94,6 +99,16 @@ public class RegionResolver {
return matcher.find() ? matcher.group(1) : defaultRegion;
}
+ /**
+ * True when some partition publishes {@code region} or its region pattern admits it, which
+ * is the AWS SDKs' own rule: a region launched after the vendored data was refreshed still
+ * resolves by pattern. A {@code <partition>-global} pseudo-region counts. Null and blank do
+ * not.
+ */
+ public static boolean isKnownRegion(String region) {
+ return AwsPartitions.forRegion(region).isPresent();
+ }
+
/**
* Resolves the region from an X-Amz-Credential value found in
* presigned URL query parameters.
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
index 826f98076a..33af99116c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
@@ -342,7 +342,8 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
return encoded.toString();
}
- static Response errorResponse(int status, String code, String message) {
+ /** S3's XML error document; also the shape the ingress filter uses to refuse an S3-signed request. */
+ public static Response errorResponse(int status, String code, String message) {
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("Error")
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
index 17d357960a..f70e923d35 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.s3;
import static io.github.hectorvent.floci.services.s3.S3RequestParser.hasQueryParam;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AccountResolver;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
@@ -97,6 +98,7 @@ public class S3Controller {
private final RequestContext requestContext;
private final IamService iamService;
private final IamEnforcementFilter iamEnforcementFilter;
+ private final EmulatorConfig config;
@Inject
public S3Controller(S3Service s3Service, S3SelectService s3SelectService,
@@ -108,7 +110,8 @@ public class S3Controller {
AccountResolver accountResolver,
RequestContext requestContext,
IamService iamService,
- IamEnforcementFilter iamEnforcementFilter) {
+ IamEnforcementFilter iamEnforcementFilter,
+ EmulatorConfig config) {
this.s3Service = s3Service;
this.s3SelectService = s3SelectService;
this.regionResolver = regionResolver;
@@ -120,6 +123,7 @@ public class S3Controller {
this.requestContext = requestContext;
this.iamService = iamService;
this.iamEnforcementFilter = iamEnforcementFilter;
+ this.config = config;
}
private void emitCloudTrailEvent(String eventName, String bucket, String key,
@@ -3354,6 +3358,7 @@ public class S3Controller {
* itself no-ops when IAM enforcement is disabled.
*/
String credential = lcFields.get("x-amz-credential");
+ rejectUnknownPostRegion(credential);
if (credential != null && !credential.isEmpty()) {
iamEnforcementFilter.authorizeAdditionalResource(
"Credential=" + credential, "s3:PutObject", S3PublicAccessEvaluator.objectArn(bucket, key));
@@ -3411,6 +3416,23 @@ public class S3Controller {
return response.build();
}
+ /**
+ * The presigned-POST counterpart of {@code AccountContextFilter}'s unknown-region refusal: the
+ * form's {@code x-amz-credential} never reaches that filter. minio runs this credential through
+ * the same scope parser as a signed header and answers a wrong region with
+ * {@code AuthorizationHeaderMalformed}.
+ */
+ private void rejectUnknownPostRegion(String credential) {
+ if (credential == null || credential.isEmpty() || config.partitions().allowUnknownRegions()) {
+ return;
+ }
+ String region = regionResolver.resolveRegionFromPresignedCredential(credential);
+ if (!RegionResolver.isKnownRegion(region)) {
+ throw new AwsException("AuthorizationHeaderMalformed", "The authorization header is malformed; "
+ + "the region '" + region + "' is wrong; expecting a region AWS publishes.", 400);
+ }
+ }
+
/**
* Enforces real S3 presigned-POST auth: a {@code policy} field must be present and
* SigV4-signed by a known secret key referenced through {@code x-amz-credential}, matching
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java
index e18009cec5..04b87915ed 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java
@@ -51,7 +51,8 @@ import java.util.Optional;
* <h2>Deliberate deviations from real AWS</h2>
* <ul>
* <li>The credential scope's region is not checked against the bucket's region. Floci resolves
- * a request's region <em>from</em> that scope, so pinning it would be circular.</li>
+ * a request's region <em>from</em> that scope, so pinning it would be circular. Whether the
+ * label is a region at all is checked upstream, in {@code AccountContextFilter}.</li>
* <li>The per-chunk signatures of an {@code aws-chunked} upload are not verified; only the seed
* signature over the headers is. The controller strips the chunk framing itself.</li>
* <li>The well-known local-dev {@code test}/{@code test} credential pair is honoured, mirroring
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 7035f80014..e351066351 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -138,6 +138,8 @@ floci:
partitions:
# id: aws # Partition served (aws, aws-cn, aws-us-gov, aws-iso, aws-iso-b, aws-iso-e, aws-iso-f, aws-eusc).
# Derived from default-region when unset. Env: FLOCI_PARTITIONS_ID
+ allow-unknown-regions: false # true accepts a SigV4 scope region no partition publishes or admits by its
+ # region pattern, giving it its own namespace. Env: FLOCI_PARTITIONS_ALLOW_UNKNOWN_REGIONS
# Shared mock-response config for Textract/Comprehend/Rekognition/Translate. See AiMockConfigLoader.
ai-mock-config-file: ${AI_MOCK_CONFIG:}
storage:
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AccountContextFilterTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AccountContextFilterTest.java
index 248b4c2524..a510d5efa4 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/AccountContextFilterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/AccountContextFilterTest.java
@@ -1,17 +1,25 @@
package io.github.hectorvent.floci.core.common;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
+import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class AccountContextFilterTest {
@@ -23,6 +31,7 @@ class AccountContextFilterTest {
private RegionResolver regionResolver;
private RequestContext requestContext;
private Map<String, String> sessionAccounts;
+ private boolean allowUnknownRegions;
private AccountContextFilter filter;
@BeforeEach
@@ -31,8 +40,18 @@ class AccountContextFilterTest {
regionResolver = new RegionResolver(DEFAULT_REGION, DEFAULT_ACCOUNT);
requestContext = new RequestContext();
sessionAccounts = new java.util.HashMap<>();
+ allowUnknownRegions = false;
SessionAccountLookup sessionLookup = akid -> Optional.ofNullable(sessionAccounts.get(akid));
- filter = new AccountContextFilter(accountResolver, regionResolver, requestContext, sessionLookup);
+ filter = new AccountContextFilter(accountResolver, regionResolver, requestContext, sessionLookup,
+ this::config);
+ }
+
+ private EmulatorConfig config() {
+ EmulatorConfig.PartitionsConfig partitions = mock(EmulatorConfig.PartitionsConfig.class);
+ when(partitions.allowUnknownRegions()).thenReturn(allowUnknownRegions);
+ EmulatorConfig config = mock(EmulatorConfig.class);
+ when(config.partitions()).thenReturn(partitions);
+ return config;
}
@Test
@@ -108,12 +127,79 @@ class AccountContextFilterTest {
void anUnsignedRequestGetsTheDeploymentPartition() {
RegionResolver china = new RegionResolver("cn-north-1", DEFAULT_ACCOUNT);
AccountContextFilter chinaFilter = new AccountContextFilter(accountResolver, china, requestContext,
- akid -> Optional.empty());
+ akid -> Optional.empty(), this::config);
chinaFilter.filter(mockContext(null, null));
assertEquals("cn-north-1", requestContext.getRegion());
assertEquals("aws-cn", requestContext.getPartition());
}
+ /** A label no partition publishes or admits is refused; the context still names it for the mappers. */
+ @Test
+ void anUnknownScopeRegionIsRefusedWithTheJsonSignatureError() {
+ ContainerRequestContext ctx = mockContext(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/polygondwanaland-west-1/sqs/aws4_request, "
+ + "SignedHeaders=host, Signature=abc",
+ null);
+ filter.filter(ctx);
+ ArgumentCaptor<Response> aborted = ArgumentCaptor.forClass(Response.class);
+ verify(ctx).abortWith(aborted.capture());
+ assertEquals(400, aborted.getValue().getStatus());
+ assertEquals("InvalidSignatureException", aborted.getValue().getHeaderString("X-Amzn-Errortype"));
+ assertTrue(aborted.getValue().getEntity().toString().contains("polygondwanaland-west-1"),
+ aborted.getValue().getEntity().toString());
+ assertEquals("polygondwanaland-west-1", requestContext.getRegion());
+ assertEquals("aws", requestContext.getPartition());
+ }
+
+ @Test
+ void anUnknownScopeRegionOnAnS3RequestGetsS3sXmlError() {
+ ContainerRequestContext ctx = mockContext(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/polygondwanaland-west-1/s3/aws4_request, "
+ + "SignedHeaders=host, Signature=abc",
+ null);
+ filter.filter(ctx);
+ ArgumentCaptor<Response> aborted = ArgumentCaptor.forClass(Response.class);
+ verify(ctx).abortWith(aborted.capture());
+ assertEquals(400, aborted.getValue().getStatus());
+ String body = aborted.getValue().getEntity().toString();
+ assertTrue(body.contains("<Code>AuthorizationHeaderMalformed</Code>"), body);
+ // XmlBuilder escapes the quotes around the label, as S3's own error document does.
+ assertTrue(body.contains("the region &apos;polygondwanaland-west-1&apos; is wrong"), body);
+ }
+
+ @Test
+ void anUnknownRegionInAPresignedCredentialIsRefusedToo() {
+ ContainerRequestContext ctx = mockContext(null, "AKID/20260617/polygondwanaland-west-1/s3/aws4_request");
+ filter.filter(ctx);
+ ArgumentCaptor<Response> aborted = ArgumentCaptor.forClass(Response.class);
+ verify(ctx).abortWith(aborted.capture());
+ assertEquals(400, aborted.getValue().getStatus());
+ }
+
+ /** The pattern rule is the SDKs' own: an unpublished label of a known shape is served. */
+ @Test
+ void aPatternAdmittedUnpublishedRegionIsServed() {
+ ContainerRequestContext ctx = mockContext(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/eu-south-9/sqs/aws4_request, SignedHeaders=host, Signature=abc",
+ null);
+ filter.filter(ctx);
+ verify(ctx, never()).abortWith(any());
+ assertEquals("eu-south-9", requestContext.getRegion());
+ assertEquals("aws", requestContext.getPartition());
+ }
+
+ @Test
+ void allowUnknownRegionsServesTheLabelWithItsOwnNamespace() {
+ allowUnknownRegions = true;
+ ContainerRequestContext ctx = mockContext(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/polygondwanaland-west-1/sqs/aws4_request, "
+ + "SignedHeaders=host, Signature=abc",
+ null);
+ filter.filter(ctx);
+ verify(ctx, never()).abortWith(any());
+ assertEquals("polygondwanaland-west-1", requestContext.getRegion());
+ }
+
@Test
void emptyAuthHeaderFallsBackToPresignedCredential() {
ContainerRequestContext ctx = mockContext("",
@@ -220,6 +306,24 @@ class AccountContextFilterTest {
assertEquals("111122223333", requestContext.getAccountId());
}
+ /** A Query SDK parses only an XML error body, and this filter runs before the protocol claim. */
+ @Test
+ void anUnknownScopeRegionOnAFormEncodedRequestGetsTheQueryXmlError() {
+ ContainerRequestContext ctx = mockContext(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/polygondwanaland-west-1/sts/aws4_request, "
+ + "SignedHeaders=host, Signature=abc",
+ null);
+ when(ctx.getMediaType()).thenReturn(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
+ filter.filter(ctx);
+ ArgumentCaptor<Response> aborted = ArgumentCaptor.forClass(Response.class);
+ verify(ctx).abortWith(aborted.capture());
+ String body = aborted.getValue().getEntity().toString();
+ assertEquals(400, aborted.getValue().getStatus());
+ assertEquals(MediaType.APPLICATION_XML_TYPE, aborted.getValue().getMediaType());
+ assertTrue(body.contains("<Code>InvalidSignatureException</Code>"), body);
+ assertTrue(body.contains("polygondwanaland-west-1"), body);
+ }
+
private ContainerRequestContext mockContext(String authHeader, String xAmzCredential) {
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
when(ctx.getHeaderString("Authorization")).thenReturn(authHeader);
@@ -230,7 +334,9 @@ class AccountContextFilterTest {
queryParams.add("X-Amz-Credential", xAmzCredential);
}
when(uriInfo.getQueryParameters()).thenReturn(queryParams);
+ when(uriInfo.getPath()).thenReturn("/");
when(ctx.getUriInfo()).thenReturn(uriInfo);
+ when(ctx.getMethod()).thenReturn("POST");
return ctx;
}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionAllowedIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionAllowedIntegrationTest.java
new file mode 100644
index 0000000000..e3eff91ecd
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionAllowedIntegrationTest.java
@@ -0,0 +1,58 @@
+package io.github.hectorvent.floci.core.common;
+
+import io.github.hectorvent.floci.testing.PartitionCleanup;
+import io.github.hectorvent.floci.testing.PartitionMatrix;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.util.Map;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+
+/** {@code floci.partitions.allow-unknown-regions=true}: the old behaviour, any label is a region. */
+@QuarkusTest
+@TestProfile(PartitionUnknownRegionAllowedIntegrationTest.AllowUnknownRegionsProfile.class)
+class PartitionUnknownRegionAllowedIntegrationTest {
+
+ private static final String UNKNOWN = PartitionUnknownRegionIntegrationTest.UNKNOWN;
+
+ @RegisterExtension
+ final PartitionCleanup cleanup = new PartitionCleanup();
+
+ @Test
+ void theUnknownLabelIsServedWithItsOwnNamespace() {
+ String name = "unknown-" + Long.toString(System.nanoTime(), 36);
+ cleanup.register(() -> PartitionUnknownRegionIntegrationTest.deleteQueue(UNKNOWN, name));
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(UNKNOWN, "sqs"))
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateQueue")
+ .formParam("QueueName", name)
+ .when().post("/").then().statusCode(200);
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(UNKNOWN, "sqs"))
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "ListQueues")
+ .when().post("/").then().statusCode(200)
+ .body(containsString(name));
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth("us-east-1", "sqs"))
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "ListQueues")
+ .formParam("QueueNamePrefix", name)
+ .when().post("/").then().statusCode(200)
+ .body(not(containsString(name)));
+ }
+
+ public static final class AllowUnknownRegionsProfile implements QuarkusTestProfile {
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ return Map.of("floci.partitions.allow-unknown-regions", "true");
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionIntegrationTest.java
new file mode 100644
index 0000000000..4b93fc8c19
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionUnknownRegionIntegrationTest.java
@@ -0,0 +1,125 @@
+package io.github.hectorvent.floci.core.common;
+
+import io.github.hectorvent.floci.testing.PartitionCleanup;
+import io.github.hectorvent.floci.testing.PartitionMatrix;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.nio.charset.StandardCharsets;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * A SigV4 scope region that no partition publishes or admits by its region pattern is refused,
+ * as moto and LocalStack refuse it by default; a label of a published shape that the vendored
+ * data does not list yet is served, which is the AWS SDKs' own rule. The escape hatch serves any
+ * label with its own namespace.
+ */
+@QuarkusTest
+class PartitionUnknownRegionIntegrationTest {
+
+ static final String UNKNOWN = "polygondwanaland-west-1";
+ static final String UNPUBLISHED_BUT_SHAPED = "eu-south-9";
+
+ @RegisterExtension
+ final PartitionCleanup cleanup = new PartitionCleanup();
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @Test
+ void aQueryRequestSignedForAnUnknownRegionIsRefusedWithTheSignatureError() {
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(UNKNOWN, "sqs"))
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "ListQueues")
+ .when().post("/").then().statusCode(400)
+ .contentType(containsString("xml"))
+ .body("ErrorResponse.Error.Code", equalTo("InvalidSignatureException"))
+ .body("ErrorResponse.Error.Type", equalTo("Sender"))
+ .body("ErrorResponse.Error.Message", containsString(UNKNOWN));
+ }
+
+ @Test
+ void aJsonRequestSignedForAnUnknownRegionGetsTheJsonSignatureError() {
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(UNKNOWN, "sqs"))
+ .header("X-Amz-Target", "AmazonSQS.ListQueues")
+ .contentType("application/x-amz-json-1.0")
+ .body("{}")
+ .when().post("/").then().statusCode(400)
+ .header("X-Amzn-Errortype", "InvalidSignatureException")
+ .body("__type", equalTo("InvalidSignatureException"))
+ .body("message", containsString(UNKNOWN));
+ }
+
+ /** A presigned POST's credential sits in the form body, out of the ingress filter's sight. */
+ @Test
+ void aPresignedPostSignedForAnUnknownRegionGetsS3sMalformedHeaderError() {
+ given()
+ .multiPart("key", "unknown-region.txt")
+ .multiPart("x-amz-credential", "AKID/20260215/" + UNKNOWN + "/s3/aws4_request")
+ .multiPart("file", "unknown-region.txt", "never stored".getBytes(StandardCharsets.UTF_8), "text/plain")
+ .when().post("/unknown-region-post-bucket").then().statusCode(400)
+ .body("Error.Code", equalTo("AuthorizationHeaderMalformed"))
+ .body("Error.Message", containsString(UNKNOWN));
+ }
+
+ @Test
+ void anS3RequestSignedForAnUnknownRegionGetsS3sMalformedHeaderError() {
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(UNKNOWN, "s3"))
+ .when().get("/").then().statusCode(400)
+ .body(containsString("<Code>AuthorizationHeaderMalformed</Code>"))
+ .body(containsString("the region &apos;" + UNKNOWN + "&apos; is wrong"));
+ }
+
+ @Test
+ void aPatternAdmittedRegionTheDataDoesNotListIsServed() {
+ String name = "shaped-" + Long.toString(System.nanoTime(), 36);
+ cleanup.register(() -> deleteQueue(UNPUBLISHED_BUT_SHAPED, name));
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(UNPUBLISHED_BUT_SHAPED, "sqs"))
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateQueue")
+ .formParam("QueueName", name)
+ .when().post("/").then().statusCode(200)
+ .body(containsString(name));
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(UNPUBLISHED_BUT_SHAPED, "s3"))
+ .when().get("/").then().statusCode(200)
+ .body(not(containsString("AuthorizationHeaderMalformed")));
+ }
+
+ static void deleteQueue(String region, String name) {
+ // Spell the envelope out: AwsQueryResponse.envelope wraps the result as
+ // <GetQueueUrlResponse><GetQueueUrlResult><QueueUrl>. XmlPath reads "**" as a parameter
+ // reference rather than a deep scan, so it threw before the delete ever ran and
+ // PartitionCleanup swallowed it, leaving the queue behind in the shared emulator.
+ String url = given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(region, "sqs"))
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "GetQueueUrl")
+ .formParam("QueueName", name)
+ .when().post("/").xmlPath()
+ .getString("GetQueueUrlResponse.GetQueueUrlResult.QueueUrl");
+ if (url == null || url.isBlank()) {
+ throw new IllegalStateException("GetQueueUrl returned no QueueUrl for " + name
+ + " in " + region + "; the queue would leak into the shared emulator");
+ }
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth(region, "sqs"))
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DeleteQueue")
+ .formParam("QueueUrl", url)
+ .when().post("/");
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java
index 85d538b6ab..dab52f4118 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java
@@ -80,6 +80,19 @@ class RegionResolverTest {
assertEquals("us-east-1", resolver.resolveRegion(headers));
}
+ /** Published, pseudo, and pattern-admitted labels are regions; garbage, null and blank are not. */
+ @Test
+ void isKnownRegionFollowsThePartitionCatalogAndItsRegionPatterns() {
+ assertTrue(RegionResolver.isKnownRegion("us-east-1"));
+ assertTrue(RegionResolver.isKnownRegion("cn-north-1"));
+ assertTrue(RegionResolver.isKnownRegion("aws-cn-global"));
+ assertTrue(RegionResolver.isKnownRegion("eu-south-9"), "unpublished but matches the commercial pattern");
+ assertFalse(RegionResolver.isKnownRegion("polygondwanaland-west-1"));
+ assertFalse(RegionResolver.isKnownRegion("s3"));
+ assertFalse(RegionResolver.isKnownRegion(null));
+ assertFalse(RegionResolver.isKnownRegion(" "));
+ }
+
@Test
void getAccountId() {
assertEquals("000000000000", resolver.getAccountId());
diff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeSchedulerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeSchedulerIntegrationTest.java
index 1b47271b33..884ec5d405 100644
--- a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeSchedulerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeSchedulerIntegrationTest.java
@@ -296,7 +296,14 @@ class EventBridgeSchedulerIntegrationTest {
@Override
public String defaultAccountId() { return ACCOUNT; }
@Override
- public PartitionsConfig partitions() { return Optional::empty; }
+ public PartitionsConfig partitions() {
+ return new PartitionsConfig() {
+ @Override
+ public Optional<String> id() { return Optional.empty(); }
+ @Override
+ public boolean allowUnknownRegions() { return false; }
+ };
+ }
@Override
public Optional<String> aiMockConfigFile() { return Optional.empty(); }
@Override
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
index 9cf1630bf7..a0ad145bfc 100644
--- a/src/test/resources/application.yml
+++ b/src/test/resources/application.yml
@@ -21,6 +21,8 @@ floci:
helper-image: floci/network-helper:local
default-region: us-east-1
default-account-id: "000000000000"
+ partitions:
+ allow-unknown-regions: false
ai-mock-config-file: ${AI_MOCK_CONFIG:src/test/resources/fixtures/ai-mock-config.json}
storage:
mode: memory