mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
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:
1 parent
5913812b53
commit
6fcfc0450a
18 files changed
+450
-13
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user