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

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

P2b of the multi-partition plan. RegionResolver.resolveRegionFromAuth took the
credential-scope label with no membership test, so polygondwanaland-west-1
became a region with its own storage namespace and its own ARNs. A label that
no partition publishes or admits by its regionRegex is now refused with a 400:
S3-signed requests get S3's XML AuthorizationHeaderMalformed ("the region '...'
is wrong", the S3 and minio wording), everything else InvalidSignatureException.
The pattern rule is the AWS SDKs' own, so a region launched after the vendored
data was refreshed (eu-south-9) is still served. moto
(MOTO_ALLOW_NONEXISTENT_REGION) and LocalStack (ALLOW_NONSTANDARD_REGIONS)
refuse the same way by default; floci.partitions.allow-unknown-regions
(FLOCI_PARTITIONS_ALLOW_UNKNOWN_REGIONS) restores the old any-label behaviour.

* fix(core): answer an unknown scope region in the caller's protocol, including presigned POST

A form-encoded request now gets a Query ErrorResponse instead of JSON, which Query SDKs cannot parse, and an S3 presigned POST's form credential is checked for an unknown region with S3's AuthorizationHeaderMalformed, as minio does.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-25 10:12:46 -04:00
1 parent 5913812b53
commit 6fcfc0450a
18 files changed
+450 -13

No files matched your search

+1 -1
View File
@@ -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>
@@ -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
@@ -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 |
+11
View File
@@ -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:...`);
@@ -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();
}
/**
@@ -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});
@@ -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();
}
}
@@ -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.
@@ -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")
@@ -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
@@ -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
+2
View File
@@ -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:
@@ -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;
}
@@ -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");
}
}
}
@@ -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("/");
}
}
@@ -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());
@@ -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
+2
View File
@@ -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