mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(core): reject colons in region names (#5705)
This commit is contained in:
1 parent
a522c5a9da
commit
ae3ff3405a
11 files changed
+445
-29
No files matched your search
@@ -45,9 +45,13 @@ public class PartitionStartupValidator {
|
||||
* The partition the deployment serves.
|
||||
*
|
||||
* @throws IllegalStateException when {@code configuredPartition} names no published partition,
|
||||
* or disagrees with a default region the catalog recognises
|
||||
* or disagrees with a default region the catalog recognises,
|
||||
* or the default region contains an ARN field delimiter
|
||||
*/
|
||||
static AwsPartition validate(String defaultRegion, Optional<String> configuredPartition) {
|
||||
if (RegionResolver.containsArnDelimiter(defaultRegion)) {
|
||||
throw new IllegalStateException("floci.default-region must not contain ':': " + defaultRegion);
|
||||
}
|
||||
String effective = RegionResolver.effectivePartition(defaultRegion, configuredPartition);
|
||||
AwsPartition partition = AwsPartitions.find(effective).orElseThrow(() -> new IllegalStateException(
|
||||
"floci.partitions.id names no AWS partition: '" + effective + "'. Published partitions: "
|
||||
|
||||
@@ -77,6 +77,9 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
}
|
||||
String sessionToken = extractSessionToken(ctx);
|
||||
String auth = ctx.getHeaderString("Authorization");
|
||||
if (rejectAmbiguousRegion(ctx, auth)) {
|
||||
return;
|
||||
}
|
||||
String akid = auth != null && !auth.isEmpty() ? accountResolver.extractAccessKeyId(auth) : null;
|
||||
if (akid != null) {
|
||||
requestContext.setAccountId(resolveAccount(akid, accountResolver.resolve(auth)));
|
||||
@@ -84,7 +87,9 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
requestContext.setSessionToken(sessionToken);
|
||||
String region = regionResolver.resolveRegionFromAuth(auth);
|
||||
applyRegion(region);
|
||||
rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceName(auth));
|
||||
if (rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceName(auth))) {
|
||||
return;
|
||||
}
|
||||
rejectPartitionAbsentService(ctx, SigV4CredentialScope.serviceName(auth).orElse(null));
|
||||
} else {
|
||||
// A present Authorization header wins, as in IamEnforcementFilter: an unrecognised one
|
||||
@@ -101,7 +106,9 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
requestContext.setSessionToken(sessionToken);
|
||||
String region = regionResolver.resolveRegionFromPresignedCredential(credential);
|
||||
applyRegion(region);
|
||||
rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceNameFromCredential(credential));
|
||||
if (rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceNameFromCredential(credential))) {
|
||||
return;
|
||||
}
|
||||
rejectPartitionAbsentService(ctx,
|
||||
SigV4CredentialScope.serviceNameFromCredential(credential).orElse(null));
|
||||
} else {
|
||||
@@ -135,6 +142,25 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
requestContext.setPartition(regionResolver.partitionForRegion(region));
|
||||
}
|
||||
|
||||
/** Refuses delimiters in region interpretations used by request context or downstream dispatch. */
|
||||
private boolean rejectAmbiguousRegion(ContainerRequestContext ctx, String authorization) {
|
||||
String queryCredential = authorization == null || authorization.isBlank()
|
||||
? ctx.getUriInfo().getQueryParameters().getFirst("X-Amz-Credential") : null;
|
||||
String parsedAuthorization = queryCredential == null ? authorization
|
||||
: IamEnforcementFilter.requestAuthorization(null, ctx.getUriInfo().getQueryParameters());
|
||||
// Some Query routes synthesize this header even without X-Amz-Algorithm. Inspect the
|
||||
// same region they use, in addition to the split credential used by request context.
|
||||
String region = regionResolver.resolveRegionFromAuthOrNull(parsedAuthorization);
|
||||
if (!RegionResolver.containsArnDelimiter(region) && queryCredential != null) {
|
||||
region = regionResolver.resolveRegionFromPresignedCredential(queryCredential);
|
||||
}
|
||||
if (!RegionResolver.containsArnDelimiter(region)) {
|
||||
return false;
|
||||
}
|
||||
applyRegion(region);
|
||||
return rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceName(parsedAuthorization));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -142,28 +168,45 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
* 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.
|
||||
* {@code <ErrorResponse>} for a claimed Query or form-encoded request, CBOR for a claimed
|
||||
* CBOR request, and JSON otherwise. The pre-matching protocol claim is already available.
|
||||
*/
|
||||
private void rejectUnknownRegion(ContainerRequestContext ctx, String region, Optional<String> signingName) {
|
||||
if (RegionResolver.isKnownRegion(region) || configProvider.get().partitions().allowUnknownRegions()) {
|
||||
return;
|
||||
private boolean rejectUnknownRegion(ContainerRequestContext ctx, String region, Optional<String> signingName) {
|
||||
boolean ambiguous = RegionResolver.containsArnDelimiter(region);
|
||||
if (!ambiguous && (RegionResolver.isKnownRegion(region)
|
||||
|| configProvider.get().partitions().allowUnknownRegions())) {
|
||||
return false;
|
||||
}
|
||||
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
|
||||
ambiguous ? "The authorization header is malformed; the region '" + region
|
||||
+ "' must not contain ':'."
|
||||
: "The authorization header is malformed; the region '" + region
|
||||
+ "' is wrong; expecting a region AWS publishes."));
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
String message = "Region '" + region + "' is not a region in any AWS partition. Sign the request "
|
||||
String message = ambiguous ? "Region '" + region + "' must not contain ':' because it separates ARN fields."
|
||||
: "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())) {
|
||||
WireProtocol protocol = ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim claim
|
||||
? claim.protocol() : WireProtocol.REST;
|
||||
if (protocol == WireProtocol.RPCV2_CBOR || protocol == WireProtocol.AWS_CBOR_TARGET) {
|
||||
String contentType = ctx.getHeaderString(AwsCborContentTypeFilter.ORIGINAL_CONTENT_TYPE_HEADER);
|
||||
if (contentType == null) {
|
||||
contentType = ctx.getHeaderString("Content-Type");
|
||||
}
|
||||
ctx.abortWith(CborErrorResponses.of(new AwsException("InvalidSignatureException", message, 400),
|
||||
CborErrorResponses.mediaTypeFor(contentType)));
|
||||
return true;
|
||||
}
|
||||
if (protocol == WireProtocol.AWS_QUERY || isFormEncoded(ctx.getMediaType())) {
|
||||
ctx.abortWith(AwsQueryResponse.error("InvalidSignatureException", message, null, 400));
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
ctx.abortWith(AwsProtocolClaimFilter.errorResponse(400, "InvalidSignatureException", message));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isFormEncoded(MediaType mediaType) {
|
||||
|
||||
@@ -773,10 +773,6 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
for (Map<String, List<String>> targetContext : targetContexts) {
|
||||
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
|
||||
targetContext, resource, region, accountId, resourceOwnerAccountId);
|
||||
if ("logs".equals(credentialScope)
|
||||
&& ("logs:CreateLogStream".equals(action) || "logs:DeleteLogStream".equals(action))) {
|
||||
effectiveContext.put("aws:ResourceAccount", List.of(accountId));
|
||||
}
|
||||
if ("dynamodb".equals(credentialScope)) {
|
||||
Map<String, List<String>> batchContext =
|
||||
conditionContextResolver.dynamoDbBatchConditionContext(action, ctx, resource);
|
||||
|
||||
@@ -127,6 +127,11 @@ public class RegionResolver {
|
||||
return AwsPartitions.forRegion(region).isPresent();
|
||||
}
|
||||
|
||||
/** A colon separates ARN fields and cannot be part of a request's region label. */
|
||||
public static boolean containsArnDelimiter(String region) {
|
||||
return region != null && region.indexOf(':') >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the region from an X-Amz-Credential value found in
|
||||
* presigned URL query parameters.
|
||||
|
||||
@@ -3864,13 +3864,16 @@ public class S3Controller {
|
||||
* {@code AuthorizationHeaderMalformed}.
|
||||
*/
|
||||
private void rejectUnknownPostRegion(String credential) {
|
||||
if (credential == null || credential.isEmpty() || config.partitions().allowUnknownRegions()) {
|
||||
if (credential == null || credential.isEmpty()) {
|
||||
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);
|
||||
boolean ambiguous = RegionResolver.containsArnDelimiter(region);
|
||||
if (ambiguous || (!RegionResolver.isKnownRegion(region) && !config.partitions().allowUnknownRegions())) {
|
||||
String message = ambiguous ? "The authorization header is malformed; the region '" + region
|
||||
+ "' must not contain ':'." : "The authorization header is malformed; "
|
||||
+ "the region '" + region + "' is wrong; expecting a region AWS publishes.";
|
||||
throw new AwsException("AuthorizationHeaderMalformed", message, 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user