mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(core): verify SigV4 header signatures for every service under validate-signatures (#5268)
* fix(core): verify SigV4 header signatures for every service under validate-signatures FLOCI_AUTH_VALIDATE_SIGNATURES verified signatures for S3 only, so SQS, KMS, IAM, Scheduler and every other service accepted a request signed with the wrong secret. SigV4HeaderSignatureFilter verifies the Authorization header of every request outside S3 and execute-api when the flag is on, rebuilding the canonical request from the request as it arrived and answering in the protocol the request used. Unsigned requests are left alone, and the default configuration is unchanged. The filter runs after matching so it can hash the body. The SQS queue-URL router now records the bytes it appends to a Query body, and the CBOR content type is read from the header that preserves it, so neither rewrite breaks a valid signature. With the flag on, an unknown or synthetic access key is now refused by every service, as S3 already refused it. Closes #5195 * fix(core): skip signature checks by matched resource, strip the spoofable content-type header The filter skipped any request whose credential scope named s3 or execute-api, so a caller could sign an SQS or KMS call under either and pass with the wrong secret, and S3 Control was never checked. It now steps aside only for the resources another verifier owns: S3Controller and the API Gateway execute controllers. AwsCborContentTypeFilter removes any client-sent X-Floci-Original-Content-Type, so a signed Content-Type cannot be swapped behind a forged copy. REST-XML services (Route 53, CloudFront, S3 Control) get XML errors. Header and scope parsing reuse SigV4AuthorizationHeader and CredentialScope, and the canonical request moves into SigV4Canonicalization, shared with ExecuteApiSigV4Authorizer. * fix(core): leave AppSync GraphQL signatures to AppSync's own check AppSync's GraphQL endpoint verifies IAM signatures itself and answers a failure with the UnauthorizedException envelope AppSync clients parse. With validate-signatures on, the header filter reached it first and answered with a JSON InvalidSignatureException. AppSyncExecutionController joins the resources the filter steps aside for.
This commit is contained in:
1 parent
91c15be74a
commit
9f224d9de6
16 files changed
+1393
-178
No files matched your search
@@ -18,6 +18,10 @@ public class AwsCborContentTypeFilter implements ContainerRequestFilter {
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext ctx) {
|
||||
// The header is this filter's own record of what it replaced. A client could send it too,
|
||||
// and every reader (the protocol claim, the CBOR error encoding, SigV4 verification of a
|
||||
// signed Content-Type) would then trust a value the client chose, so it never gets through.
|
||||
ctx.getHeaders().keySet().removeIf(name -> name.equalsIgnoreCase(ORIGINAL_CONTENT_TYPE_HEADER));
|
||||
String contentType = ctx.getHeaderString("Content-Type");
|
||||
if (contentType != null && contentType.startsWith(AWS_CBOR_1_1_MEDIA_TYPE)) {
|
||||
normalize(ctx, contentType);
|
||||
|
||||
@@ -0,0 +1,400 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.auth.CredentialScope;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4AuthorizationHeader;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
|
||||
import io.github.hectorvent.floci.services.apigateway.ApiGatewayAwsExecuteController;
|
||||
import io.github.hectorvent.floci.services.apigateway.ApiGatewayExecuteController;
|
||||
import io.github.hectorvent.floci.services.apigateway.ApiGatewayUserRequestController;
|
||||
import io.github.hectorvent.floci.services.appsync.graphql.AppSyncExecutionController;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.s3.S3ControlController;
|
||||
import io.github.hectorvent.floci.services.s3.S3Controller;
|
||||
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
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.container.ResourceInfo;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
/**
|
||||
* Verifies the SigV4 signature in the {@code Authorization} header of every request outside S3
|
||||
* when {@code floci.auth.validate-signatures} is enabled, so a request signed with the wrong
|
||||
* secret is refused by SQS, KMS, IAM, Scheduler and the rest as it already is by S3.
|
||||
*
|
||||
* <p>The canonical request is rebuilt from the request as it arrived, by the rules
|
||||
* {@link SigV4Canonicalization} shares with {@code ExecuteApiSigV4Authorizer}: the wire path with
|
||||
* each segment URI-encoded a second time (and the path as sent, for a signer that encodes once),
|
||||
* the canonical query string, the headers named in {@code SignedHeaders}, and the SHA-256 of the
|
||||
* body. The signature is derived with the key's secret and the credential scope, and the two are
|
||||
* compared in constant time.
|
||||
*
|
||||
* <p>Runs after matching, on the worker thread a blocking resource method runs on, because hashing
|
||||
* the body is a blocking read that the I/O thread a pre-matching filter runs on does not allow. By
|
||||
* then pre-matching filters may have rewritten the request, so the path and query come from the
|
||||
* Vert.x request as it arrived rather than from {@code UriInfo}. The one body rewrite, the
|
||||
* {@code QueueUrl} that {@link SqsQueueUrlRouterFilter} appends to an SDK v1 Query call sent to a
|
||||
* queue URL, is reported by that filter and left out of the hash and the {@code content-length}.
|
||||
* The one header rewrite, the CBOR {@code Content-Type} that {@link AwsCborContentTypeFilter}
|
||||
* normalizes, is read back from the header that filter preserves it in, which it strips from
|
||||
* every inbound request first so a client cannot supply it.
|
||||
*
|
||||
* <p>Which requests another verifier owns is decided by the resource the request matched, never by
|
||||
* the service the caller's own credential scope names: a request for {@link S3Controller} goes
|
||||
* through {@code S3HeaderSignatureFilter}, one for the API Gateway execute controllers through
|
||||
* {@code ExecuteApiSigV4Authorizer}, which decides per method whether IAM auth applies at all, and
|
||||
* one for {@code AppSyncExecutionController} (GraphQL) through AppSync's {@code IamAuthValidator},
|
||||
* which answers in the {@code UnauthorizedException} envelope AppSync clients parse.
|
||||
* Everything else is verified here, S3 Control included, whatever service its scope names.
|
||||
*
|
||||
* <p>As with S3, signature verification is authentication only, and an unsigned request is let
|
||||
* through: whether a caller may act is IAM enforcement's decision. Presigned query-string
|
||||
* signatures and SigV4a ({@code AWS4-ECDSA-P256-SHA256}) are not verified here.
|
||||
*
|
||||
* <p>Errors follow the protocol the request used: a Query or REST-XML request (Route 53,
|
||||
* CloudFront, S3 Control) receives an {@code <ErrorResponse>}, a CBOR request a CBOR body, and
|
||||
* everything else JSON, which REST-JSON clients read through {@code X-Amzn-Errortype}. The codes
|
||||
* are AWS's: {@code IncompleteSignature} for a header that cannot be checked,
|
||||
* {@code InvalidClientTokenId} / {@code UnrecognizedClientException} for an access key Floci does
|
||||
* not know, and {@code SignatureDoesNotMatch} / {@code InvalidSignatureException} for a signature
|
||||
* that does not verify or falls outside the fifteen-minute clock-skew window.
|
||||
*
|
||||
* <p>Nothing here runs with the flag off: the filter returns before reading a single header.
|
||||
*/
|
||||
@Provider
|
||||
@Priority(Priorities.AUTHENTICATION)
|
||||
@ApplicationScoped
|
||||
public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SigV4HeaderSignatureFilter.class);
|
||||
|
||||
private static final Duration MAX_CLOCK_SKEW = Duration.ofMinutes(15);
|
||||
private static final DateTimeFormatter AMZ_DATE =
|
||||
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
|
||||
|
||||
/** Resources whose signatures another verifier owns; see the class comment. */
|
||||
private static final Set<Class<?>> VERIFIED_ELSEWHERE = Set.of(S3Controller.class,
|
||||
ApiGatewayExecuteController.class, ApiGatewayAwsExecuteController.class,
|
||||
ApiGatewayUserRequestController.class, AppSyncExecutionController.class);
|
||||
|
||||
/** REST-XML resources the catalog does not list: S3 Control answers in S3's XML. */
|
||||
private static final Set<Class<?>> UNCATALOGUED_REST_XML = Set.of(S3ControlController.class);
|
||||
|
||||
/** The well-known local-dev pair, honoured as every other SigV4 check in Floci honours it. */
|
||||
private static final String LEGACY_ACCESS_KEY_ID = "test";
|
||||
private static final String LEGACY_SECRET_KEY = "test";
|
||||
|
||||
private static final String MISMATCH_MESSAGE = "The request signature we calculated does not match "
|
||||
+ "the signature you provided. Check your AWS Secret Access Key and signing method. "
|
||||
+ "Consult the service documentation for details.";
|
||||
private static final String INVALID_TOKEN_MESSAGE = "The security token included in the request is invalid.";
|
||||
|
||||
// Lazily resolved: JAX-RS providers are instantiated before runtime config mappings exist
|
||||
// (the AwsProtocolClaimFilter pattern).
|
||||
private final jakarta.inject.Provider<EmulatorConfig> configProvider;
|
||||
private final IamService iamService;
|
||||
private final ResolvedServiceCatalog catalog;
|
||||
private final CurrentVertxRequest currentVertxRequest;
|
||||
|
||||
@Context
|
||||
ResourceInfo resourceInfo;
|
||||
|
||||
@Inject
|
||||
public SigV4HeaderSignatureFilter(jakarta.inject.Provider<EmulatorConfig> configProvider,
|
||||
IamService iamService, ResolvedServiceCatalog catalog,
|
||||
CurrentVertxRequest currentVertxRequest) {
|
||||
this.configProvider = configProvider;
|
||||
this.iamService = iamService;
|
||||
this.catalog = catalog;
|
||||
this.currentVertxRequest = currentVertxRequest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext ctx) throws IOException {
|
||||
if (!configProvider.get().auth().validateSignatures()) {
|
||||
return;
|
||||
}
|
||||
Class<?> resource = resourceInfo != null ? resourceInfo.getResourceClass() : null;
|
||||
if (resource != null && VERIFIED_ELSEWHERE.contains(resource)) {
|
||||
return;
|
||||
}
|
||||
SigV4AuthorizationHeader signed = SigV4AuthorizationHeader.parse(ctx.getHeaderString("Authorization"));
|
||||
if (signed == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
CredentialScope scope = CredentialScope.parse(signed.credential());
|
||||
if (scope == null || isBlank(signed.signedHeaders()) || isBlank(signed.signature())) {
|
||||
incompleteSignature(ctx, resource, "Authorization header requires 'Credential' (in the form "
|
||||
+ "<access key>/<date>/<region>/<service>/aws4_request), 'SignedHeaders' and "
|
||||
+ "'Signature' parameters.");
|
||||
return;
|
||||
}
|
||||
if (!SigV4RequestValidator.containsHeader(signed.signedHeaders(), "host")) {
|
||||
incompleteSignature(ctx, resource,
|
||||
"'Host' or ':authority' must be a 'SignedHeader' in the AWS Authorization.");
|
||||
return;
|
||||
}
|
||||
Instant requestTime = requestTime(ctx);
|
||||
if (requestTime == null) {
|
||||
incompleteSignature(ctx, resource, "Authorization header requires existence of either a "
|
||||
+ "'X-Amz-Date' or a 'Date' header.");
|
||||
return;
|
||||
}
|
||||
|
||||
Optional<String> secretKey = resolveSecretKey(scope.accessKeyId(), ctx.getHeaderString("X-Amz-Security-Token"));
|
||||
if (secretKey.isEmpty()) {
|
||||
LOG.debugv("Refusing {0} request signed with unknown access key {1}",
|
||||
scope.service(), SigV4RequestValidator.sanitizeForLog(scope.accessKeyId()));
|
||||
abort(ctx, resource, 403, "InvalidClientTokenId", "UnrecognizedClientException", INVALID_TOKEN_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
String amzDate = AMZ_DATE.format(requestTime);
|
||||
Instant now = Instant.now();
|
||||
if (Duration.between(requestTime, now).abs().compareTo(MAX_CLOCK_SKEW) > 0) {
|
||||
signatureDoesNotMatch(ctx, resource, "Signature expired: " + amzDate + " is outside the "
|
||||
+ MAX_CLOCK_SKEW.toMinutes() + " minute window around the server time "
|
||||
+ AMZ_DATE.format(now) + ".");
|
||||
return;
|
||||
}
|
||||
if (!amzDate.startsWith(scope.date())) {
|
||||
signatureDoesNotMatch(ctx, resource, "Date in Credential scope does not match YYYYMMDD from "
|
||||
+ "ISO-8601 version of date from HTTP: '" + scope.date() + "' != '"
|
||||
+ amzDate.substring(0, 8) + "'.");
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] body = signedBody(ctx);
|
||||
UnaryOperator<String> headers = name -> "content-length".equals(name)
|
||||
&& ctx.getProperty(SqsQueueUrlRouterFilter.APPENDED_BODY_BYTES_PROPERTY) != null
|
||||
? String.valueOf(body.length)
|
||||
: headerValue(ctx, name);
|
||||
boolean matches;
|
||||
try {
|
||||
matches = signatureMatches(ctx.getMethod(), rawPath(ctx), rawQuery(ctx), signed.signedHeaders(),
|
||||
headers, body, secretKey.get(), amzDate, scope, signed.signature());
|
||||
} catch (Exception e) {
|
||||
LOG.debugv(e, "SigV4 verification failed to complete for accessKey={0}",
|
||||
SigV4RequestValidator.sanitizeForLog(scope.accessKeyId()));
|
||||
matches = false;
|
||||
}
|
||||
if (!matches) {
|
||||
LOG.debugv("Refusing {0} request: signature mismatch for accessKey={1}",
|
||||
scope.service(), SigV4RequestValidator.sanitizeForLog(scope.accessKeyId()));
|
||||
signatureDoesNotMatch(ctx, resource, MISMATCH_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether {@code signature} verifies for this request; package-private for the unit test. */
|
||||
static boolean signatureMatches(String method, String rawPath, String rawQuery, String signedHeaders,
|
||||
UnaryOperator<String> headers, byte[] body, String secretKey,
|
||||
String amzDate, CredentialScope scope, String signature) throws Exception {
|
||||
return SigV4Canonicalization.signatureMatches(method, rawPath,
|
||||
SigV4Canonicalization.canonicalQueryString(rawQuery, false),
|
||||
SigV4Canonicalization.canonicalHeaders(signedHeaders, headers),
|
||||
signedHeaders,
|
||||
SigV4Canonicalization.payloadHash(headers.apply("x-amz-content-sha256"), signedHeaders, body, false),
|
||||
amzDate, scope, secretKey, signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* The path as the client sent it, before any filter rewrote it or JAX-RS normalized it. Falls
|
||||
* back to the request URI when no Vert.x request is current.
|
||||
*/
|
||||
private String rawPath(ContainerRequestContext ctx) {
|
||||
HttpServerRequest request = vertxRequest();
|
||||
if (request != null && request.path() != null && !request.path().isEmpty()) {
|
||||
return request.path();
|
||||
}
|
||||
return ctx.getUriInfo().getRequestUri().getRawPath();
|
||||
}
|
||||
|
||||
/** The query string as the client sent it, with the same fallback as {@link #rawPath}. */
|
||||
private String rawQuery(ContainerRequestContext ctx) {
|
||||
HttpServerRequest request = vertxRequest();
|
||||
if (request != null) {
|
||||
return request.query();
|
||||
}
|
||||
return ctx.getUriInfo().getRequestUri().getRawQuery();
|
||||
}
|
||||
|
||||
private HttpServerRequest vertxRequest() {
|
||||
RoutingContext routingContext = currentVertxRequest != null ? currentVertxRequest.getCurrent() : null;
|
||||
return routingContext != null ? routingContext.request() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A signed header's value as the client sent it, repeated values joined with commas as SigV4
|
||||
* specifies. {@code content-type} is the one {@link AwsCborContentTypeFilter} replaced, when it
|
||||
* replaced it. {@code host} falls back to the request URI's authority for a request that carried
|
||||
* no {@code Host} header (HTTP/2, or HTTP/1.0 handled by {@link MissingHostHeaderFilter}).
|
||||
*/
|
||||
private static String headerValue(ContainerRequestContext ctx, String name) {
|
||||
if ("content-type".equals(name)) {
|
||||
return SigV4RequestValidator.normalizeHeaderValue(requestContentType(ctx));
|
||||
}
|
||||
List<String> values = null;
|
||||
for (Map.Entry<String, List<String>> entry : ctx.getHeaders().entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(name)) {
|
||||
values = entry.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (values != null && !values.isEmpty()) {
|
||||
List<String> normalized = new ArrayList<>(values.size());
|
||||
for (String value : values) {
|
||||
normalized.add(SigV4RequestValidator.normalizeHeaderValue(value));
|
||||
}
|
||||
return String.join(",", normalized);
|
||||
}
|
||||
if ("host".equals(name)) {
|
||||
URI requestUri = ctx.getUriInfo().getRequestUri();
|
||||
return RequestHost.of(null, requestUri);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the body and hands an identical stream back to the request, returning the part the
|
||||
* client signed: all of it, less any bytes {@link SqsQueueUrlRouterFilter} appended. The stream
|
||||
* is read directly rather than gated on {@code hasEntity()}, which is false for a request
|
||||
* without a {@code Content-Type}.
|
||||
*/
|
||||
private static byte[] signedBody(ContainerRequestContext ctx) throws IOException {
|
||||
InputStream entity = ctx.getEntityStream();
|
||||
byte[] body = entity != null ? entity.readAllBytes() : new byte[0];
|
||||
ctx.setEntityStream(new ByteArrayInputStream(body));
|
||||
if (ctx.getProperty(SqsQueueUrlRouterFilter.APPENDED_BODY_BYTES_PROPERTY) instanceof Integer appended
|
||||
&& appended <= body.length) {
|
||||
return Arrays.copyOf(body, body.length - appended);
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
private Optional<String> resolveSecretKey(String accessKeyId, String sessionToken) {
|
||||
if (LEGACY_ACCESS_KEY_ID.equals(accessKeyId)) {
|
||||
return Optional.of(LEGACY_SECRET_KEY);
|
||||
}
|
||||
return iamService.findSecretKey(accessKeyId, sessionToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* The request timestamp the client signed: {@code x-amz-date} in ISO 8601 basic form, or the
|
||||
* HTTP {@code Date} header when the client signed that instead.
|
||||
*/
|
||||
private static Instant requestTime(ContainerRequestContext ctx) {
|
||||
String amzDate = ctx.getHeaderString("x-amz-date");
|
||||
try {
|
||||
if (amzDate != null && !amzDate.isBlank()) {
|
||||
return Instant.from(AMZ_DATE.parse(amzDate.trim()));
|
||||
}
|
||||
String httpDate = ctx.getHeaderString("Date");
|
||||
if (httpDate != null && !httpDate.isBlank()) {
|
||||
return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(httpDate.trim()));
|
||||
}
|
||||
} catch (DateTimeParseException e) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void incompleteSignature(ContainerRequestContext ctx, Class<?> resource, String message) {
|
||||
abort(ctx, resource, 400, "IncompleteSignature", "IncompleteSignatureException", message);
|
||||
}
|
||||
|
||||
private void signatureDoesNotMatch(ContainerRequestContext ctx, Class<?> resource, String message) {
|
||||
abort(ctx, resource, 403, "SignatureDoesNotMatch", "InvalidSignatureException", message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses the request in the encoding it used. Query and REST-XML services name these failures
|
||||
* without the {@code Exception} suffix and JSON services with it, the split
|
||||
* {@code IamEnforcementFilter} already makes for an unknown key. A REST request carries no claim
|
||||
* that names its protocol, so REST-XML is read from the matched resource's catalog entry; the
|
||||
* other protocols from the request's content type, as {@code AccountContextFilter} reads it.
|
||||
*/
|
||||
private void abort(ContainerRequestContext ctx, Class<?> resource, int status, String queryCode,
|
||||
String jsonCode, String message) {
|
||||
String contentType = requestContentType(ctx);
|
||||
if (isCbor(ctx, contentType)) {
|
||||
ctx.abortWith(CborErrorResponses.of(new AwsException(jsonCode, message, status),
|
||||
CborErrorResponses.mediaTypeFor(contentType)));
|
||||
return;
|
||||
}
|
||||
if (isFormEncoded(ctx.getMediaType())) {
|
||||
ctx.abortWith(AwsQueryResponse.error(queryCode, message, null, status));
|
||||
return;
|
||||
}
|
||||
if (isRestXml(resource)) {
|
||||
ctx.abortWith(AwsQueryResponse.error(queryCode, message, xmlNamespace(resource), status));
|
||||
return;
|
||||
}
|
||||
ctx.abortWith(AwsProtocolClaimFilter.errorResponse(status, jsonCode, message));
|
||||
}
|
||||
|
||||
/** Whether the matched resource serves a REST-XML API. */
|
||||
private boolean isRestXml(Class<?> resource) {
|
||||
if (resource == null) {
|
||||
return false;
|
||||
}
|
||||
return UNCATALOGUED_REST_XML.contains(resource) || catalog.byResourceClass(resource)
|
||||
.map(service -> service.defaultProtocol() == ServiceProtocol.REST_XML)
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
private String xmlNamespace(Class<?> resource) {
|
||||
return catalog.byResourceClass(resource).map(ServiceDescriptor::xmlNamespace).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code Content-Type} the client sent, before {@link AwsCborContentTypeFilter} normalized it.
|
||||
* That filter strips a client-sent copy of the header, so a value here is always its own.
|
||||
*/
|
||||
private static String requestContentType(ContainerRequestContext ctx) {
|
||||
String original = ctx.getHeaderString(AwsCborContentTypeFilter.ORIGINAL_CONTENT_TYPE_HEADER);
|
||||
return original != null ? original : ctx.getHeaderString("Content-Type");
|
||||
}
|
||||
|
||||
private static boolean isCbor(ContainerRequestContext ctx, String contentType) {
|
||||
return (contentType != null && contentType.toLowerCase().contains("cbor"))
|
||||
|| "rpc-v2-cbor".equalsIgnoreCase(ctx.getHeaderString("smithy-protocol"));
|
||||
}
|
||||
|
||||
private static boolean isFormEncoded(MediaType mediaType) {
|
||||
return mediaType != null
|
||||
&& "application".equalsIgnoreCase(mediaType.getType())
|
||||
&& "x-www-form-urlencoded".equalsIgnoreCase(mediaType.getSubtype());
|
||||
}
|
||||
|
||||
private static boolean isBlank(String value) {
|
||||
return value == null || value.isBlank();
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,13 @@ import java.util.regex.Pattern;
|
||||
@PreMatching
|
||||
public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
|
||||
|
||||
/**
|
||||
* The number of bytes this filter appended to a Query body, set only when it appended any.
|
||||
* {@link SigV4HeaderSignatureFilter} verifies the body the client signed, which ends before them.
|
||||
*/
|
||||
public static final String APPENDED_BODY_BYTES_PROPERTY =
|
||||
SqsQueueUrlRouterFilter.class.getName() + ".appendedBodyBytes";
|
||||
|
||||
private static final Pattern QUEUE_PATH = Pattern.compile("^/(\\d+)/([^/]+)$");
|
||||
|
||||
@Override
|
||||
@@ -74,6 +81,7 @@ public class SqsQueueUrlRouterFilter implements ContainerRequestFilter {
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
ctx.setEntityStream(new SequenceInputStream(ctx.getEntityStream(),
|
||||
new ByteArrayInputStream(injection)));
|
||||
ctx.setProperty(APPENDED_BODY_BYTES_PROPERTY, injection.length);
|
||||
String cl = ctx.getHeaderString("Content-Length");
|
||||
if (cl != null) {
|
||||
ctx.getHeaders().putSingle("Content-Length",
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
package io.github.hectorvent.floci.core.common.auth;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
/**
|
||||
* The canonical request of a header- or query-signed SigV4 request outside S3, and the signature
|
||||
* check over it. Shared by {@code ExecuteApiSigV4Authorizer} (API Gateway) and
|
||||
* {@code SigV4HeaderSignatureFilter} (every other service under {@code validate-signatures}); the
|
||||
* first carried these as private helpers before the second needed the same rules.
|
||||
*/
|
||||
public final class SigV4Canonicalization {
|
||||
|
||||
private static final String ALGORITHM = "AWS4-HMAC-SHA256";
|
||||
|
||||
private SigV4Canonicalization() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code signature} is the one {@code secretKey} produces for this request under any of
|
||||
* {@link #canonicalUriCandidates(String) the canonical URIs} a signer could have produced for
|
||||
* {@code rawPath}, compared in constant time.
|
||||
*/
|
||||
public static boolean signatureMatches(String method, String rawPath, String canonicalQueryString,
|
||||
String canonicalHeaders, String signedHeaders, String payloadHash,
|
||||
String amzDate, CredentialScope scope, String secretKey,
|
||||
String signature) throws Exception {
|
||||
byte[] signingKey = SigV4RequestValidator.deriveSigningKey(
|
||||
secretKey, scope.date(), scope.region(), scope.service());
|
||||
for (String canonicalUri : canonicalUriCandidates(rawPath)) {
|
||||
String canonicalRequest = method + "\n"
|
||||
+ canonicalUri + "\n"
|
||||
+ canonicalQueryString + "\n"
|
||||
+ canonicalHeaders + "\n"
|
||||
+ signedHeaders + "\n"
|
||||
+ payloadHash;
|
||||
String stringToSign = ALGORITHM + "\n"
|
||||
+ amzDate + "\n"
|
||||
+ scope.credentialScope() + "\n"
|
||||
+ SigV4RequestValidator.sha256Hex(canonicalRequest.getBytes(StandardCharsets.UTF_8));
|
||||
String expected = SigV4RequestValidator.hexEncode(
|
||||
SigV4RequestValidator.hmacSha256(signingKey, stringToSign));
|
||||
if (MessageDigest.isEqual(expected.getBytes(StandardCharsets.UTF_8),
|
||||
signature.getBytes(StandardCharsets.UTF_8))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The canonical URI candidates a signer could have produced for this raw path. Non-S3 SigV4
|
||||
* URI-encodes each path segment a second time on top of the encoding already on the wire; for
|
||||
* an all-ASCII path the two forms are identical, so only paths carrying escapes produce a
|
||||
* second candidate. Trying both keeps a legitimately signed request from being rejected over
|
||||
* which convention its signer follows.
|
||||
*/
|
||||
public static List<String> canonicalUriCandidates(String rawPath) {
|
||||
String raw = isBlank(rawPath) ? "/" : rawPath;
|
||||
List<String> candidates = new ArrayList<>(2);
|
||||
candidates.add(raw);
|
||||
String[] segments = raw.split("/", -1);
|
||||
StringBuilder doubleEncoded = new StringBuilder();
|
||||
for (int i = 0; i < segments.length; i++) {
|
||||
if (i > 0) {
|
||||
doubleEncoded.append('/');
|
||||
}
|
||||
doubleEncoded.append(uriEncode(segments[i]));
|
||||
}
|
||||
if (!candidates.contains(doubleEncoded.toString())) {
|
||||
candidates.add(doubleEncoded.toString());
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
/**
|
||||
* The canonical query string: every pair decoded, re-encoded per SigV4 and sorted. A presigned
|
||||
* request drops its own {@code X-Amz-Signature} ({@code dropSignature}).
|
||||
*/
|
||||
public static String canonicalQueryString(String rawQuery, boolean dropSignature) {
|
||||
if (isBlank(rawQuery)) {
|
||||
return "";
|
||||
}
|
||||
List<String[]> pairs = new ArrayList<>();
|
||||
for (String pair : rawQuery.split("&")) {
|
||||
if (pair.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
int equals = pair.indexOf('=');
|
||||
String name = decodeQueryComponent(equals >= 0 ? pair.substring(0, equals) : pair);
|
||||
String value = decodeQueryComponent(equals >= 0 ? pair.substring(equals + 1) : "");
|
||||
if (dropSignature && "X-Amz-Signature".equals(name)) {
|
||||
continue;
|
||||
}
|
||||
pairs.add(new String[]{uriEncode(name), uriEncode(value)});
|
||||
}
|
||||
pairs.sort(Comparator.<String[], String>comparing(pair -> pair[0]).thenComparing(pair -> pair[1]));
|
||||
StringBuilder canonical = new StringBuilder();
|
||||
for (String[] pair : pairs) {
|
||||
if (!canonical.isEmpty()) {
|
||||
canonical.append('&');
|
||||
}
|
||||
canonical.append(pair[0]).append('=').append(pair[1]);
|
||||
}
|
||||
return canonical.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* The canonical headers block for {@code signedHeaders}, each value read through
|
||||
* {@code valueOf} (given the lowercase name) and normalized.
|
||||
*/
|
||||
public static String canonicalHeaders(String signedHeaders, UnaryOperator<String> valueOf) {
|
||||
StringBuilder canonical = new StringBuilder();
|
||||
for (String name : signedHeaders.split(";")) {
|
||||
canonical.append(name).append(':')
|
||||
.append(SigV4RequestValidator.normalizeHeaderValue(valueOf.apply(name))).append('\n');
|
||||
}
|
||||
return canonical.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload hash the canonical request must carry.
|
||||
*
|
||||
* <p>A literal {@code x-amz-content-sha256} is deliberately <em>not</em> taken on trust: the
|
||||
* body is hashed instead. For an honest request the two are equal, and for a tampered one they
|
||||
* are not, which turns a swapped body into a signature mismatch. Trusting the header would let
|
||||
* a caller keep a valid signature over a body they had replaced, since the header value the
|
||||
* signer hashed would still be the one presented.
|
||||
*
|
||||
* <p>The sentinels ({@code UNSIGNED-PAYLOAD}, the {@code STREAMING-*} forms) mean the caller
|
||||
* chose not to sign the body, and are honoured only when {@code x-amz-content-sha256} is itself
|
||||
* in {@code SignedHeaders}: that is, when the choice is covered by the signature. A presigned
|
||||
* request is unsigned-payload by convention; every AWS presigner emits it that way.
|
||||
*/
|
||||
public static String payloadHash(String declaredContentSha256, String signedHeaders, byte[] body,
|
||||
boolean presigned) throws Exception {
|
||||
if (declaredContentSha256 != null && !declaredContentSha256.isBlank()
|
||||
&& SigV4RequestValidator.containsHeader(signedHeaders, "x-amz-content-sha256")
|
||||
&& !SigV4RequestValidator.isSha256Hex(declaredContentSha256.trim())) {
|
||||
return declaredContentSha256.trim();
|
||||
}
|
||||
if (presigned) {
|
||||
return "UNSIGNED-PAYLOAD";
|
||||
}
|
||||
return SigV4RequestValidator.sha256Hex(body == null ? new byte[0] : body);
|
||||
}
|
||||
|
||||
/** RFC 3986 percent-encoding as SigV4 defines it: {@code /} is escaped, {@code -._~} are not. */
|
||||
public static String uriEncode(String value) {
|
||||
StringBuilder encoded = new StringBuilder(value.length());
|
||||
for (byte raw : value.getBytes(StandardCharsets.UTF_8)) {
|
||||
int b = raw & 0xFF;
|
||||
char ch = (char) b;
|
||||
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
|
||||
|| ch == '-' || ch == '.' || ch == '_' || ch == '~') {
|
||||
encoded.append(ch);
|
||||
} else {
|
||||
encoded.append('%')
|
||||
.append(Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16)))
|
||||
.append(Character.toUpperCase(Character.forDigit(b & 0xF, 16)));
|
||||
}
|
||||
}
|
||||
return encoded.toString();
|
||||
}
|
||||
|
||||
private static String decodeQueryComponent(String value) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
return URLDecoder.decode(value.replace("+", "%2B"), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static boolean isBlank(String value) {
|
||||
return value == null || value.isBlank();
|
||||
}
|
||||
}
|
||||
+11
-148
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.apigateway;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.auth.CredentialScope;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4AuthorizationHeader;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.model.AccessKey;
|
||||
@@ -15,15 +16,11 @@ import jakarta.ws.rs.core.UriInfo;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
@@ -189,32 +186,20 @@ public class ExecuteApiSigV4Authorizer {
|
||||
return sessionToken;
|
||||
}
|
||||
|
||||
String payloadHash = payloadHash(headers, body, presigned, signed.signedHeaders());
|
||||
String canonicalHeaders = canonicalHeaders(signed.signedHeaders(), headers, uriInfo);
|
||||
String canonicalQueryString = canonicalQueryString(uriInfo.getRequestUri().getRawQuery(), presigned);
|
||||
byte[] signingKey = SigV4RequestValidator.deriveSigningKey(
|
||||
secretKey, scope.date(), scope.region(), scope.service());
|
||||
String payloadHash = SigV4Canonicalization.payloadHash(
|
||||
headers == null ? null : headers.getHeaderString("x-amz-content-sha256"),
|
||||
signed.signedHeaders(), body, presigned);
|
||||
String canonicalHeaders = SigV4Canonicalization.canonicalHeaders(signed.signedHeaders(),
|
||||
name -> "host".equals(name) ? hostHeader(headers, uriInfo) : headerValue(headers, name));
|
||||
String canonicalQueryString = SigV4Canonicalization.canonicalQueryString(
|
||||
uriInfo.getRequestUri().getRawQuery(), presigned);
|
||||
|
||||
String rawPath = signedRequestPath != null
|
||||
? signedRequestPath
|
||||
: uriInfo.getRequestUri().getRawPath();
|
||||
for (String canonicalUri : canonicalUriCandidates(rawPath)) {
|
||||
String canonicalRequest = httpMethod + "\n"
|
||||
+ canonicalUri + "\n"
|
||||
+ canonicalQueryString + "\n"
|
||||
+ canonicalHeaders + "\n"
|
||||
+ signed.signedHeaders() + "\n"
|
||||
+ payloadHash;
|
||||
String stringToSign = ALGORITHM + "\n"
|
||||
+ signed.amzDate() + "\n"
|
||||
+ scope.credentialScope() + "\n"
|
||||
+ SigV4RequestValidator.sha256Hex(canonicalRequest.getBytes(StandardCharsets.UTF_8));
|
||||
String expected = SigV4RequestValidator.hexEncode(
|
||||
SigV4RequestValidator.hmacSha256(signingKey, stringToSign));
|
||||
if (MessageDigest.isEqual(expected.getBytes(StandardCharsets.UTF_8),
|
||||
signed.signature().getBytes(StandardCharsets.UTF_8))) {
|
||||
return new Result(null, null, resolveIdentity(scope.accessKeyId()));
|
||||
}
|
||||
if (SigV4Canonicalization.signatureMatches(httpMethod, rawPath, canonicalQueryString, canonicalHeaders,
|
||||
signed.signedHeaders(), payloadHash, signed.amzDate(), scope, secretKey, signed.signature())) {
|
||||
return new Result(null, null, resolveIdentity(scope.accessKeyId()));
|
||||
}
|
||||
|
||||
LOG.debugv("execute-api SigV4 signature mismatch for accessKey={0}",
|
||||
@@ -386,68 +371,6 @@ public class ExecuteApiSigV4Authorizer {
|
||||
|
||||
// ──────────────────────────── Canonical request ────────────────────────────
|
||||
|
||||
/**
|
||||
* The canonical URI candidates a signer could have produced for this raw path. Non-S3 SigV4
|
||||
* URI-encodes each path segment a second time on top of the encoding already on the wire; for
|
||||
* an all-ASCII path the two forms are identical, so only paths carrying escapes produce a
|
||||
* second candidate. Trying both keeps a legitimately signed request from being rejected over
|
||||
* which convention its signer follows.
|
||||
*/
|
||||
private static List<String> canonicalUriCandidates(String rawPath) {
|
||||
String raw = isBlank(rawPath) ? "/" : rawPath;
|
||||
List<String> candidates = new ArrayList<>(2);
|
||||
candidates.add(raw);
|
||||
String[] segments = raw.split("/", -1);
|
||||
StringBuilder doubleEncoded = new StringBuilder();
|
||||
for (int i = 0; i < segments.length; i++) {
|
||||
if (i > 0) {
|
||||
doubleEncoded.append('/');
|
||||
}
|
||||
doubleEncoded.append(uriEncode(segments[i]));
|
||||
}
|
||||
if (!candidates.contains(doubleEncoded.toString())) {
|
||||
candidates.add(doubleEncoded.toString());
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
private static String canonicalQueryString(String rawQuery, boolean dropSignature) {
|
||||
if (isBlank(rawQuery)) {
|
||||
return "";
|
||||
}
|
||||
List<String[]> pairs = new ArrayList<>();
|
||||
for (String pair : rawQuery.split("&")) {
|
||||
if (pair.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
int equals = pair.indexOf('=');
|
||||
String name = decodeQueryComponent(equals >= 0 ? pair.substring(0, equals) : pair);
|
||||
String value = decodeQueryComponent(equals >= 0 ? pair.substring(equals + 1) : "");
|
||||
if (dropSignature && "X-Amz-Signature".equals(name)) {
|
||||
continue;
|
||||
}
|
||||
pairs.add(new String[]{uriEncode(name), uriEncode(value)});
|
||||
}
|
||||
pairs.sort(Comparator.<String[], String>comparing(pair -> pair[0]).thenComparing(pair -> pair[1]));
|
||||
StringBuilder canonical = new StringBuilder();
|
||||
for (String[] pair : pairs) {
|
||||
if (!canonical.isEmpty()) {
|
||||
canonical.append('&');
|
||||
}
|
||||
canonical.append(pair[0]).append('=').append(pair[1]);
|
||||
}
|
||||
return canonical.toString();
|
||||
}
|
||||
|
||||
private static String canonicalHeaders(String signedHeaders, HttpHeaders headers, UriInfo uriInfo) {
|
||||
StringBuilder canonical = new StringBuilder();
|
||||
for (String name : signedHeaders.split(";")) {
|
||||
String value = "host".equals(name) ? hostHeader(headers, uriInfo) : headerValue(headers, name);
|
||||
canonical.append(name).append(':').append(SigV4RequestValidator.normalizeHeaderValue(value)).append('\n');
|
||||
}
|
||||
return canonical.toString();
|
||||
}
|
||||
|
||||
private static String headerValue(HttpHeaders headers, String name) {
|
||||
String value = headers == null ? null : headers.getHeaderString(name);
|
||||
return value == null ? "" : value;
|
||||
@@ -470,66 +393,6 @@ public class ExecuteApiSigV4Authorizer {
|
||||
: String.valueOf(requestUri.getHost());
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload hash the canonical request must carry.
|
||||
*
|
||||
* <p>A literal {@code x-amz-content-sha256} is deliberately <em>not</em> taken on trust: the
|
||||
* body is hashed instead. For an honest request the two are equal, and for a tampered one they
|
||||
* are not, which turns a swapped body into a signature mismatch. Trusting the header would let
|
||||
* a caller keep a valid signature over a body they had replaced, since the header value the
|
||||
* signer hashed would still be the one presented.
|
||||
*
|
||||
* <p>The sentinels ({@code UNSIGNED-PAYLOAD}, the {@code STREAMING-*} forms) mean the caller
|
||||
* chose not to sign the body, and are honoured only when {@code x-amz-content-sha256} is itself
|
||||
* in {@code SignedHeaders}: that is, when the choice is covered by the signature. A presigned
|
||||
* request is unsigned-payload by convention; every AWS presigner emits it that way.
|
||||
*/
|
||||
private String payloadHash(HttpHeaders headers, byte[] body, boolean presigned,
|
||||
String signedHeaders) throws Exception {
|
||||
String declared = headers == null ? null : headers.getHeaderString("x-amz-content-sha256");
|
||||
if (declared != null && !declared.isBlank()
|
||||
&& SigV4RequestValidator.containsHeader(signedHeaders, "x-amz-content-sha256")
|
||||
&& !SigV4RequestValidator.isSha256Hex(declared.trim())) {
|
||||
return declared.trim();
|
||||
}
|
||||
if (presigned) {
|
||||
return "UNSIGNED-PAYLOAD";
|
||||
}
|
||||
return SigV4RequestValidator.sha256Hex(body == null ? new byte[0] : body);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Crypto and encoding helpers ────────────────────────────
|
||||
|
||||
/** RFC 3986 percent-encoding as SigV4 defines it: {@code /} is escaped, {@code -._~} are not. */
|
||||
private static String uriEncode(String value) {
|
||||
StringBuilder encoded = new StringBuilder(value.length());
|
||||
for (byte raw : value.getBytes(StandardCharsets.UTF_8)) {
|
||||
int b = raw & 0xFF;
|
||||
char ch = (char) b;
|
||||
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
|
||||
|| ch == '-' || ch == '.' || ch == '_' || ch == '~') {
|
||||
encoded.append(ch);
|
||||
} else {
|
||||
encoded.append('%')
|
||||
.append(Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16)))
|
||||
.append(Character.toUpperCase(Character.forDigit(b & 0xF, 16)));
|
||||
}
|
||||
}
|
||||
return encoded.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Percent-decodes a query-string component without {@code URLDecoder}'s form-encoding rule that
|
||||
* a literal {@code +} means a space: SigV4 signers escape a space as {@code %20}, so a raw
|
||||
* {@code +} on the wire is a plus sign and re-encoding it as a space would break the signature.
|
||||
*/
|
||||
private static String decodeQueryComponent(String value) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
return URLDecoder.decode(value.replace("+", "%2B"), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static boolean isBlank(String value) {
|
||||
return value == null || value.isBlank();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user