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:
David authored and GitHub committed 2026-10-09 16:31:22 -04:00
1 parent 91c15be74a
commit 9f224d9de6
16 files changed
+1393 -178

No files matched your search

@@ -104,7 +104,7 @@ floci:
spoof-aws-endpoints: false
auth:
validate-signatures: false # Set to true to verify S3 SigV4 signatures (header, presigned URL, presigned POST)
validate-signatures: false # Set to true to verify SigV4 signatures for every service (S3: header, presigned URL, presigned POST; others: header)
presign-secret: local-emulator-secret # HMAC secret for S3 pre-signed URL verification
tls:
+1 -1
View File
@@ -173,7 +173,7 @@ The most frequently set variables when running Floci as a Docker image:
| `FLOCI_STORAGE_MODE` | `memory` | `memory`, `persistent`, `hybrid`, or `wal` |
| `FLOCI_STORAGE_PERSISTENT_PATH` | `./data` | Directory for persistent storage |
| `FLOCI_SERVICES_DOCKER_NETWORK` | _(none)_ | Docker network for spawned containers (Lambda, ElastiCache, RDS, OpenSearch, MSK) |
| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify S3 SigV4 signatures without evaluating bucket policies |
| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify the SigV4 signature of every signed request, for every service, without evaluating policies |
| `FLOCI_SERVICES_LAMBDA_EPHEMERAL` | `false` | Remove Lambda containers after each invocation |
For the complete list of every `FLOCI_*` variable, see [Environment Variables Reference](./environment-variables.md).
+1 -1
View File
@@ -23,7 +23,7 @@ Floci is configured exclusively through environment variables. Every option belo
| Variable | Default | Description |
|---|---|---|
| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | When `true`, verifies S3 SigV4 signatures (`Authorization` header, presigned URL and presigned POST) without evaluating bucket policies |
| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | When `true`, verifies the SigV4 signature of every signed request without evaluating policies: every S3 placement (`Authorization` header, presigned URL and presigned POST), and the `Authorization` header for every other service (see [Signature Validation](multi-account.md#signature-validation)) |
| `FLOCI_AUTH_PRESIGN_SECRET` | `local-emulator-secret` | Secret used to sign and verify pre-signed URLs |
## Network Exposure
+10 -5
View File
@@ -167,18 +167,23 @@ bucket in one account and reads it from another (see [S3](../services/s3.md#glob
## Signature Validation
Outside S3, Floci does not perform general SigV4 validation for `Authorization` headers. Only the access key ID matters for account resolution, so the secret access key can be any non-empty string.
By default Floci does not verify SigV4 signatures. Only the access key ID matters for account resolution, so the secret access key can be any non-empty string.
`FLOCI_AUTH_VALIDATE_SIGNATURES` applies only to S3. It verifies the SigV4 signature of every signed S3 request (`Authorization` header, presigned URL and presigned POST) without evaluating bucket policies, and leaves unsigned requests alone (see [S3 Signature Verification](../services/s3.md#signature-verification)). It does not authenticate other services or protect IAM and STS account routing. To verify S3 signatures:
`FLOCI_AUTH_VALIDATE_SIGNATURES` verifies the SigV4 signature of every signed request, for every service, without evaluating any policy, and leaves unsigned requests alone:
- **S3** verifies every placement (`Authorization` header, presigned URL and presigned POST); see [S3 Signature Verification](../services/s3.md#signature-verification).
- **Every other service** verifies the `Authorization` header. The canonical request is rebuilt from the request as it arrived, with the path encoded the way AWS signers encode it outside S3 and the body's own SHA-256 as the payload hash. A refusal comes back in the protocol the request used, with the Query and REST-XML name first (Route 53, CloudFront and S3 Control answer in XML) and the JSON, CBOR and REST-JSON name second: `SignatureDoesNotMatch` or `InvalidSignatureException` for a signature that does not verify or is more than 15 minutes from server time, `InvalidClientTokenId` or `UnrecognizedClientException` for an access key Floci does not know, and `IncompleteSignature` or `IncompleteSignatureException` for an `Authorization` header missing a part. Presigned query-string signatures and SigV4a are not verified outside S3, and API Gateway `execute-api` requests keep their own [IAM authorization](../services/api-gateway.md#iam-authorization).
To verify signatures:
```bash
FLOCI_AUTH_VALIDATE_SIGNATURES=true
FLOCI_AUTH_PRESIGN_SECRET=your-secret # for pre-signed URL verification
```
With `FLOCI_AUTH_VALIDATE_SIGNATURES` or `FLOCI_SERVICES_S3_ENFORCE_AUTH` enabled, header-signed S3 requests using a synthetic 12-digit account key fail with `403 InvalidAccessKeyId`, while presigned POSTs using that key fail with `403 SignatureDoesNotMatch`. Sign S3 requests with `test`/`test` or credentials created through Floci IAM or STS (including the session token for STS credentials). For multi-account S3 access, use IAM or STS credentials for the target account; `test`/`test` routes to `FLOCI_DEFAULT_ACCOUNT_ID` (see [S3 Signature Verification](../services/s3.md#signature-verification)).
Once it is on, every signed request must use credentials Floci can verify: `test`/`test`, or credentials created through Floci IAM or STS (including the session token for STS credentials). A synthetic 12-digit account key fails on every service, with `403 InvalidAccessKeyId` from S3 header-signed requests, `403 SignatureDoesNotMatch` from S3 presigned POSTs, and `403 InvalidClientTokenId` or `403 UnrecognizedClientException` elsewhere. For multi-account access, use IAM or STS credentials for the target account; `test`/`test` routes to `FLOCI_DEFAULT_ACCOUNT_ID`. The same applies to workloads Floci launches: a container started without a role, such as a Lambda function with no execution role, is given placeholder credentials that do not verify, so give it a role.
When `validate-signatures` is `false` (the default), S3 signatures are verified only under `FLOCI_SERVICES_S3_ENFORCE_AUTH`. Account routing remains AKID-based regardless of this setting.
When `validate-signatures` is `false` (the default), S3 signatures are verified only under `FLOCI_SERVICES_S3_ENFORCE_AUTH`, and no other service verifies them. Account routing remains AKID-based regardless of this setting.
## Persistence and Account Isolation
@@ -190,4 +195,4 @@ Storage keys are namespaced per account at the persistence layer. When using `pe
|---|---|---|
| `FLOCI_DEFAULT_ACCOUNT_ID` | `000000000000` | Account ID used when the AKID does not resolve directly or through a stored credential |
| `FLOCI_DEFAULT_REGION` | `us-east-1` | Region used when not derivable from the `Authorization` header |
| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify S3 SigV4 signatures without evaluating bucket policies |
| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify the SigV4 signature of every signed request, for every service, without evaluating policies |
+1 -1
View File
@@ -272,7 +272,7 @@ For a signed request, verification rebuilds the canonical request from the wire
- an `Authorization` header whose algorithm is not `AWS4-HMAC-SHA256`, with `400 AuthorizationHeaderMalformed`. This includes SigV4A (`AWS4-ECDSA-P256-SHA256`), which AWS accepts, for example on multi-region access points: Floci does not verify ECDSA signatures yet, so it refuses the request instead of serving it unverified
- a presigned URL that carries any of `X-Amz-Algorithm`, `X-Amz-Credential` or `X-Amz-Signature` but lacks one of `X-Amz-Algorithm`, `X-Amz-Credential`, `X-Amz-Signature`, `X-Amz-Date`, `X-Amz-SignedHeaders` or `X-Amz-Expires`, with `400 AuthorizationQueryParametersError`
`FLOCI_AUTH_VALIDATE_SIGNATURES` is authentication alone: use it when forged or stale credentials must fail but buckets should stay reachable without policies. `FLOCI_SERVICES_S3_ENFORCE_AUTH` adds authorization, so it also decides anonymous access the way AWS does: a bucket that anonymous clients read needs a public-read bucket policy or ACL.
`FLOCI_AUTH_VALIDATE_SIGNATURES` is authentication alone: use it when forged or stale credentials must fail but buckets should stay reachable without policies. It is not specific to S3: the same flag verifies the `Authorization` header signature of every other service (see [Signature Validation](../configuration/multi-account.md#signature-validation)). `FLOCI_SERVICES_S3_ENFORCE_AUTH` adds authorization, so it also decides anonymous access the way AWS does: a bucket that anonymous clients read needs a public-read bucket policy or ACL.
A workload must sign with credentials Floci can verify once either flag is on. A container Floci launches without a role, such as a Lambda function with no execution role, is given placeholder credentials that no flag accepts, so give it a role, or sign with `test`/`test`.
@@ -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();
}
}
@@ -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();
}
@@ -0,0 +1,121 @@
package io.github.hectorvent.floci.core.common;
import io.github.hectorvent.floci.core.common.auth.CredentialScope;
import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Checks the canonical request against signatures an independent signer produced, so the verifier
* is not only ever compared with a signer written alongside it. Every expected signature below was
* computed by botocore 1.43.108 ({@code SigV4Auth.add_auth}) for the same method, URL, headers and
* body, with the clock pinned to 2026-01-01T00:00:00Z and the AWS documentation's example key pair.
*/
class SigV4HeaderSignatureFilterTest {
private static final String SECRET = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY";
private static final String AMZ_DATE = "20260101T000000Z";
private static final String SCOPE_DATE = "20260101";
private static final String REGION = "us-east-1";
private static final String HOST = "localhost:4566";
@Test
void queryRequestVerifies() throws Exception {
assertTrue(matches("POST", "/", null,
"content-type;host;x-amz-date",
Map.of("content-type", "application/x-www-form-urlencoded; charset=utf-8"),
"Action=GetCallerIdentity&Version=2011-06-15", SECRET, "sts",
"5df4b2dd1edf5b1846612118237772601820753b9d87d8214afbad7e2d70d522"));
}
@Test
void jsonRequestVerifies() throws Exception {
assertTrue(matches("POST", "/", null,
"content-type;host;x-amz-date;x-amz-target",
Map.of("content-type", "application/x-amz-json-1.0", "x-amz-target", "AmazonSQS.ListQueues"),
"{}", SECRET, "sqs",
"969501491602b1e686863668c36b1049fb733134484fe49487e1f6a8e0b2dc27"));
}
@Test
void restPathWithEscapesIsDoubleEncoded() throws Exception {
assertTrue(matches("GET",
"/2015-03-31/functions/arn%3Aaws%3Alambda%3Aus-east-1%3A000000000000%3Afunction%3Amy-fn/configuration",
"Qualifier=v1", "host;x-amz-date", Map.of(), "", SECRET, "lambda",
"52dde5d043128b9bef236aeea1158ab5ee3712d75e436b01fff35b3ed0dbc962"));
}
@Test
void restQueryIsCanonicalized() throws Exception {
assertTrue(matches("GET", "/schedule-groups", "NamePrefix=a%20b%2Bc&MaxResults=5",
"host;x-amz-date", Map.of(), "", SECRET, "scheduler",
"6882cb650943c3c5770a1b6fe60091fa72f5a628948df4c7aae4fa4da5772da8"));
}
@Test
void sessionTokenHeaderIsPartOfTheSignature() throws Exception {
assertTrue(matches("POST", "/", null,
"content-type;host;x-amz-date;x-amz-security-token;x-amz-target",
Map.of("content-type", "application/x-amz-json-1.1",
"x-amz-target", "TrentService.ListKeys",
"x-amz-security-token", "session-token-value"),
"{\"Limit\":10}", SECRET, "kms",
"5feaffc90be139e5f4288e442918fb838d1ef12f334d84ab292491f27535e800"));
}
@Test
void wrongSecretDoesNotVerify() throws Exception {
assertFalse(matches("POST", "/", null,
"content-type;host;x-amz-date;x-amz-target",
Map.of("content-type", "application/x-amz-json-1.0", "x-amz-target", "AmazonSQS.ListQueues"),
"{}", "not-the-secret", "sqs",
"969501491602b1e686863668c36b1049fb733134484fe49487e1f6a8e0b2dc27"));
}
@Test
void replacedBodyDoesNotVerify() throws Exception {
assertFalse(matches("POST", "/", null,
"content-type;host;x-amz-date",
Map.of("content-type", "application/x-www-form-urlencoded; charset=utf-8"),
"Action=DeleteUser&UserName=admin", SECRET, "sts",
"5df4b2dd1edf5b1846612118237772601820753b9d87d8214afbad7e2d70d522"));
}
@Test
void signatureForAnotherServiceDoesNotVerify() throws Exception {
assertFalse(matches("POST", "/", null,
"content-type;host;x-amz-date;x-amz-target",
Map.of("content-type", "application/x-amz-json-1.0", "x-amz-target", "AmazonSQS.ListQueues"),
"{}", SECRET, "kms",
"969501491602b1e686863668c36b1049fb733134484fe49487e1f6a8e0b2dc27"));
}
@Test
void canonicalUriKeepsThePlainPathWhenNothingIsEscaped() {
assertEquals(List.of("/"), SigV4Canonicalization.canonicalUriCandidates(""));
assertEquals(List.of("/schedule-groups/default"),
SigV4Canonicalization.canonicalUriCandidates("/schedule-groups/default"));
assertEquals(List.of("/functions/a%3Ab", "/functions/a%253Ab"),
SigV4Canonicalization.canonicalUriCandidates("/functions/a%3Ab"));
}
private static boolean matches(String method, String rawPath, String rawQuery, String signedHeaders,
Map<String, String> headers, String body, String secret, String service,
String signature) throws Exception {
return SigV4HeaderSignatureFilter.signatureMatches(method, rawPath, rawQuery, signedHeaders,
name -> switch (name) {
case "host" -> HOST;
case "x-amz-date" -> AMZ_DATE;
default -> headers.get(name);
},
body.getBytes(StandardCharsets.UTF_8), secret, AMZ_DATE,
new CredentialScope("AKIDEXAMPLE", SCOPE_DATE, REGION, service), signature);
}
}
@@ -0,0 +1,452 @@
package io.github.hectorvent.floci.core.common;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.github.hectorvent.floci.testing.ValidateSignaturesProfile;
import io.github.hectorvent.floci.testutil.AppSyncRequestSigner;
import io.github.hectorvent.floci.testutil.AwsRequestSigner;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.RestAssured;
import io.restassured.path.xml.XmlPath;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* {@code floci.auth.validate-signatures} authenticates every header-signed request, not only S3's:
* a request signed with the wrong secret is refused in the error vocabulary of the protocol it
* used, a request signed with the right one is served, and an unsigned request is left alone.
* Shares {@link ValidateSignaturesProfile} with the S3 signature tests.
*/
@QuarkusTest
@TestProfile(ValidateSignaturesProfile.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SigV4HeaderSignatureIntegrationTest {
private static final String QUEUE = "validate-signatures-queue";
private static final String JSON_1_0 = "application/x-amz-json-1.0";
private static final String JSON_1_1 = "application/x-amz-json-1.1";
private static final String FORM = "application/x-www-form-urlencoded";
private static final String CBOR_1_1 = "application/x-amz-cbor-1.1";
private static String userAccessKeyId;
private static String userSecretKey;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
@Order(1)
void jsonRequestWithTheRightSecretIsServed() {
given()
.filter(AwsRequestSigner.signedAs("test", "test", "sqs"))
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.CreateQueue")
.body("{\"QueueName\":\"" + QUEUE + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("QueueUrl", containsString(QUEUE));
}
@Test
@Order(2)
void jsonRequestWithTheWrongSecretIsRefused() {
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "sqs"))
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.ListQueues")
.body("{}")
.when()
.post("/")
.then()
.statusCode(403)
.body("__type", equalTo("InvalidSignatureException"));
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "kms"))
.contentType(JSON_1_1)
.header("X-Amz-Target", "TrentService.ListKeys")
.body("{}")
.when()
.post("/")
.then()
.statusCode(403)
.body("__type", equalTo("InvalidSignatureException"));
}
@Test
@Order(3)
void queryRequestWithTheWrongSecretIsRefusedInXml() {
given()
.filter(AwsRequestSigner.signedAs("test", "test", "iam"))
.contentType(FORM)
.body("Action=ListUsers&Version=2010-05-08")
.when()
.post("/")
.then()
.statusCode(200);
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "iam"))
.contentType(FORM)
.body("Action=ListUsers&Version=2010-05-08")
.when()
.post("/")
.then()
.statusCode(403)
.body("ErrorResponse.Error.Code", equalTo("SignatureDoesNotMatch"));
}
@Test
@Order(4)
void restJsonRequestWithTheWrongSecretIsRefusedThroughTheErrorTypeHeader() {
given()
.filter(AwsRequestSigner.signedAs("test", "test", "scheduler"))
.when()
.get("/schedule-groups")
.then()
.statusCode(200);
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "scheduler"))
.when()
.get("/schedule-groups")
.then()
.statusCode(403)
.header("X-Amzn-Errortype", equalTo("InvalidSignatureException"));
}
@Test
@Order(5)
void pathWithEscapesVerifiesDoubleEncoded() {
// A signature over the double-encoded ARN gets past authentication to Lambda's own 404.
given()
.filter(AwsRequestSigner.signedAs("test", "test", "lambda"))
.urlEncodingEnabled(false)
.when()
.get("/2015-03-31/functions/arn%3Aaws%3Alambda%3Aus-east-1%3A000000000000%3Afunction%3Amissing")
.then()
.statusCode(404);
}
@Test
@Order(6)
void queueUrlPathIsVerifiedBeforeItIsRewritten() {
// An SDK v1 Query call posts to the queue URL; the router rewrites the path to / and
// appends QueueUrl to the body, neither of which the client signed.
given()
.filter(AwsRequestSigner.signedAs("test", "test", "sqs"))
.contentType(FORM)
.body("Action=GetQueueAttributes&AttributeName.1=All&Version=2012-11-05")
.when()
.post("/000000000000/" + QUEUE)
.then()
.statusCode(200);
}
@Test
@Order(7)
void cborContentTypeIsVerifiedAsTheClientSentIt() {
// AwsCborContentTypeFilter rewrites application/x-amz-cbor-1.1 before matching; the client
// signed the original. An empty CBOR map is the single byte 0xA0.
byte[] emptyMap = {(byte) 0xA0};
given()
.filter(AwsRequestSigner.signedAs("test", "test", "kinesis").signingContentType())
.contentType(CBOR_1_1)
.header("X-Amz-Target", "Kinesis_20131202.ListStreams")
.body(emptyMap)
.when()
.post("/")
.then()
.statusCode(200);
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "kinesis").signingContentType())
.contentType(CBOR_1_1)
.header("X-Amz-Target", "Kinesis_20131202.ListStreams")
.body(emptyMap)
.when()
.post("/")
.then()
.statusCode(403)
.contentType(containsString("cbor"))
.header("x-amzn-query-error", startsWith("InvalidSignatureException"));
}
@Test
@Order(8)
void unknownAccessKeyIsRefused() {
given()
.filter(AwsRequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret", "sqs"))
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.ListQueues")
.body("{}")
.when()
.post("/")
.then()
.statusCode(403)
.body("__type", equalTo("UnrecognizedClientException"));
given()
.filter(AwsRequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret", "sts"))
.contentType(FORM)
.body("Action=GetCallerIdentity&Version=2011-06-15")
.when()
.post("/")
.then()
.statusCode(403)
.body("ErrorResponse.Error.Code", equalTo("InvalidClientTokenId"));
}
@Test
@Order(9)
void iamCreatedKeyVerifiesWithItsOwnSecretOnly() {
AwsRequestSigner iam = AwsRequestSigner.signedAs("test", "test", "iam");
given().filter(iam).contentType(FORM)
.body("Action=CreateUser&UserName=validate-signatures-sqs-user&Version=2010-05-08")
.when().post("/").then().statusCode(200);
XmlPath key = given().filter(iam).contentType(FORM)
.body("Action=CreateAccessKey&UserName=validate-signatures-sqs-user&Version=2010-05-08")
.when().post("/").then().statusCode(200).extract().xmlPath();
userAccessKeyId = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
userSecretKey = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey");
AwsRequestSigner user = AwsRequestSigner.signedAs(userAccessKeyId, userSecretKey, "sqs");
given()
.filter(user)
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.ListQueues")
.body("{}")
.when()
.post("/")
.then()
.statusCode(200);
given()
.filter(user.withSecret("not-the-secret"))
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.ListQueues")
.body("{}")
.when()
.post("/")
.then()
.statusCode(403)
.body("__type", equalTo("InvalidSignatureException"));
}
@Test
@Order(10)
void signatureOutsideTheClockSkewWindowIsRefused() {
given()
.filter(AwsRequestSigner.signedAs("test", "test", "sqs")
.signedAt(Instant.now().minus(Duration.ofMinutes(20))))
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.ListQueues")
.body("{}")
.when()
.post("/")
.then()
.statusCode(403)
.body("__type", equalTo("InvalidSignatureException"))
.body("message", containsString("Signature expired"));
}
@Test
@Order(11)
void malformedAuthorizationHeaderIsIncomplete() {
given()
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260101/us-east-1/sqs/aws4_request")
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.ListQueues")
.body("{}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("IncompleteSignatureException"));
}
@Test
@Order(12)
void credentialScopeNamingAnotherVerifiersServiceIsStillVerified() {
// Which verifier owns a request is decided by the resource it matched, not by the service
// the caller wrote into its own credential scope.
for (String scopeService : new String[]{"s3", "s3express", "execute-api"}) {
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", scopeService))
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.ListQueues")
.body("{}")
.when()
.post("/")
.then()
.statusCode(403)
.body("__type", equalTo("InvalidSignatureException"));
}
}
@Test
@Order(13)
void s3ControlIsVerifiedAndAnswersInXml() {
given()
.filter(AwsRequestSigner.signedAs("test", "test", "s3"))
.header("x-amz-account-id", "000000000000")
.when()
.get("/v20180820/accesspoint")
.then()
.statusCode(200);
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "s3"))
.header("x-amz-account-id", "000000000000")
.when()
.get("/v20180820/accesspoint")
.then()
.statusCode(403)
.contentType(containsString("xml"))
.body("ErrorResponse.Error.Code", equalTo("SignatureDoesNotMatch"));
}
@Test
@Order(14)
void restXmlServiceAnswersInXml() {
given()
.filter(AwsRequestSigner.signedAs("test", "test", "route53"))
.when()
.get("/2013-04-01/hostedzone")
.then()
.statusCode(200);
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "route53"))
.when()
.get("/2013-04-01/hostedzone")
.then()
.statusCode(403)
.contentType(containsString("xml"))
.body("ErrorResponse.Error.Code", equalTo("SignatureDoesNotMatch"));
given()
.filter(AwsRequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret", "route53"))
.when()
.get("/2013-04-01/hostedzone")
.then()
.statusCode(403)
.body("ErrorResponse.Error.Code", equalTo("InvalidClientTokenId"));
}
@Test
@Order(15)
void clientSuppliedOriginalContentTypeCannotStandInForTheSignedOne() throws Exception {
// Signed over one Content-Type, sent with another plus Floci's internal header claiming the
// signed value. A raw client keeps both headers exactly as written. The internal header is
// stripped on the way in, so the swap is a mismatch rather than a verified request.
URI uri = URI.create("http://localhost:" + RestAssured.port + "/");
byte[] body = "{}".getBytes(StandardCharsets.UTF_8);
Map<String, String> auth = AwsRequestSigner.signedAs("test", "test", "sqs").headersFor("POST", uri,
Map.of("content-type", JSON_1_0, "x-amz-target", "AmazonSQS.ListQueues"), body);
assertEquals(200, sendSqsListQueues(uri, auth, JSON_1_0, null, body).statusCode());
HttpResponse<String> swapped = sendSqsListQueues(uri, auth, JSON_1_0 + "; charset=utf-8", JSON_1_0, body);
assertEquals(403, swapped.statusCode());
assertTrue(swapped.body().contains("InvalidSignatureException"), swapped.body());
}
@Test
@Order(16)
void executeApiRoutesAreLeftToTheirOwnAuthorizer() {
// A wrong secret on an execute-api path is ExecuteApiSigV4Authorizer's call, per method:
// here the API does not exist, so the answer is the controller's 404, not this filter's 403.
for (String path : new String[]{"/execute-api/nope/stage/x", "/_aws/execute-api/nope/stage/x",
"/restapis/nope/stage/_user_request_/x"}) {
given()
.filter(AwsRequestSigner.signedAs("test", "not-the-secret", "execute-api"))
.when()
.get(path)
.then()
.statusCode(404);
}
}
@Test
@Order(17)
void appSyncGraphqlIsLeftToItsOwnIamCheck() throws Exception {
// The management plane is verified here like any other service; the GraphQL endpoint is
// AppSync's own, and a wrong secret there gets AppSync's UnauthorizedException envelope.
String apiId = given()
.filter(AwsRequestSigner.signedAs("test", "test", "appsync"))
.contentType("application/json")
.body("{\"name\": \"validate-signatures-graphql\", \"authenticationType\": \"AWS_IAM\"}")
.when()
.post("/v1/apis")
.then()
.statusCode(200)
.extract().path("graphqlApi.apiId");
String query = "{\"query\": \"{ __typename }\"}";
Map<String, String> forged = AppSyncRequestSigner.signedHeaders(apiId, "localhost:" + RestAssured.port,
query, "test", "not-the-secret", "us-east-1", Instant.now());
given()
.headers(forged)
.contentType("application/json")
.body(query)
.when()
.post("/v1/apis/" + apiId + "/graphql")
.then()
.statusCode(401)
.header("x-amzn-errortype", containsString("UnauthorizedException"))
.body("errors[0].errorType", equalTo("UnauthorizedException"));
}
private static HttpResponse<String> sendSqsListQueues(URI uri, Map<String, String> auth, String contentType,
String claimedOriginal, byte[] body) throws Exception {
HttpRequest.Builder request = HttpRequest.newBuilder(uri)
.version(HttpClient.Version.HTTP_1_1)
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
.header("Content-Type", contentType)
.header("X-Amz-Target", "AmazonSQS.ListQueues");
auth.forEach(request::header);
if (claimedOriginal != null) {
request.header(AwsCborContentTypeFilter.ORIGINAL_CONTENT_TYPE_HEADER, claimedOriginal);
}
try (HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build()) {
return client.send(request.build(), HttpResponse.BodyHandlers.ofString());
}
}
@Test
@Order(18)
void unsignedRequestIsLeftToIamEnforcement() {
given()
.contentType(JSON_1_0)
.header("X-Amz-Target", "AmazonSQS.ListQueues")
.body("{}")
.when()
.post("/")
.then()
.statusCode(200);
}
}
@@ -1,9 +1,9 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.testing.ValidateSignaturesProfile;
import io.github.hectorvent.floci.testutil.S3RequestSigner;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import io.restassured.RestAssured;
import jakarta.inject.Inject;
@@ -15,24 +15,16 @@ import org.junit.jupiter.api.TestMethodOrder;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
@QuarkusTest
@TestProfile(PreSignedUrlIntegrationTest.PresignValidationProfile.class)
@TestProfile(ValidateSignaturesProfile.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class PreSignedUrlIntegrationTest {
public static final class PresignValidationProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.auth.validate-signatures", "true");
}
}
private static final String BUCKET = "presign-test-bucket";
private static final S3RequestSigner LOCAL_SIGNER = S3RequestSigner.signedAs("test", "test");
@@ -1,6 +1,8 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.testing.ValidateSignaturesProfile;
import io.github.hectorvent.floci.testutil.AwsRequestSigner;
import io.github.hectorvent.floci.testutil.S3RequestSigner;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
@@ -27,11 +29,11 @@ import static org.hamcrest.Matchers.equalTo;
* {@code floci.auth.validate-signatures} authenticates every signed S3 request, whichever
* placement carries the signature, without authorizing it: a forged or unknown credential is
* refused, while bucket policies are not evaluated and unsigned requests pass, as they do with
* {@code enforce-auth} off. Shares {@link PreSignedUrlIntegrationTest}'s profile, which covers the
* presigned query-string placement.
* {@code enforce-auth} off. Shares {@link ValidateSignaturesProfile} with
* {@link PreSignedUrlIntegrationTest}, which covers the presigned query-string placement.
*/
@QuarkusTest
@TestProfile(PreSignedUrlIntegrationTest.PresignValidationProfile.class)
@TestProfile(ValidateSignaturesProfile.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class S3ValidateSignaturesIntegrationTest {
@@ -295,21 +297,21 @@ class S3ValidateSignaturesIntegrationTest {
}
private static void createIamUser(String userName) {
String authorization = "AWS4-HMAC-SHA256 Credential=test/" + CREDENTIAL_DATE
+ "/us-east-1/iam/aws4_request, SignedHeaders=host, Signature=unused";
// validate-signatures verifies IAM's signatures too, so these calls carry real ones.
AwsRequestSigner iam = AwsRequestSigner.signedAs("test", "test", "iam");
given()
.formParam("Action", "CreateUser")
.formParam("UserName", userName)
.header("Authorization", authorization)
.filter(iam)
.contentType("application/x-www-form-urlencoded")
.body("Action=CreateUser&UserName=" + userName + "&Version=2010-05-08")
.when()
.post("/")
.then()
.statusCode(200);
XmlPath key = given()
.formParam("Action", "CreateAccessKey")
.formParam("UserName", userName)
.header("Authorization", authorization)
.filter(iam)
.contentType("application/x-www-form-urlencoded")
.body("Action=CreateAccessKey&UserName=" + userName + "&Version=2010-05-08")
.when()
.post("/")
.then()
@@ -0,0 +1,18 @@
package io.github.hectorvent.floci.testing;
import io.quarkus.test.junit.QuarkusTestProfile;
import java.util.Map;
/**
* Turns {@code floci.auth.validate-signatures} on, and nothing else. Quarkus builds the application
* once per distinct profile class, so the classes that need only this override share this one
* rather than each declaring an identical nested profile.
*/
public class ValidateSignaturesProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.auth.validate-signatures", "true");
}
}
@@ -0,0 +1,169 @@
package io.github.hectorvent.floci.testutil;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.restassured.filter.Filter;
import io.restassured.filter.FilterContext;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
/**
* A RestAssured filter that header-signs a request for a service other than S3 the way an AWS SDK
* does: each segment of the wire path URI-encoded a second time, no {@code x-amz-content-sha256}
* header, and the body's SHA-256 as the payload hash. Signs {@code host}, {@code x-amz-date} and,
* when present, {@code x-amz-security-token} and {@code x-amz-target}. {@code Content-Type} is
* signed only on request ({@link #signingContentType()}), because RestAssured appends a charset to
* text types after the filter runs.
*
* <p>Attach with {@code given().filter(AwsRequestSigner.signedAs("test", "test", "sqs"))}. The
* {@code with*} methods return a copy that deliberately deviates for the rejection cases.
*/
public final class AwsRequestSigner implements Filter {
private static final String ALGORITHM = "AWS4-HMAC-SHA256";
private static final DateTimeFormatter AMZ_DATE =
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
private final String accessKeyId;
private final String secretKey;
private final String sessionToken;
private final String service;
private final Instant signedAt;
private final boolean signContentType;
private AwsRequestSigner(String accessKeyId, String secretKey, String sessionToken, String service,
Instant signedAt, boolean signContentType) {
this.accessKeyId = accessKeyId;
this.secretKey = secretKey;
this.sessionToken = sessionToken;
this.service = service;
this.signedAt = signedAt;
this.signContentType = signContentType;
}
public static AwsRequestSigner signedAs(String accessKeyId, String secretKey, String service) {
return new AwsRequestSigner(accessKeyId, secretKey, null, service, null, false);
}
/** Signs as of a fixed instant instead of now; used to trip the clock-skew window. */
public AwsRequestSigner signedAt(Instant instant) {
return new AwsRequestSigner(accessKeyId, secretKey, sessionToken, service, instant, signContentType);
}
/** Signs with the same key but a different secret, as a client with a stale secret would. */
public AwsRequestSigner withSecret(String otherSecret) {
return new AwsRequestSigner(accessKeyId, otherSecret, sessionToken, service, signedAt, signContentType);
}
/** Also signs {@code content-type}, as the Java SDK and botocore do; for binary bodies only. */
public AwsRequestSigner signingContentType() {
return new AwsRequestSigner(accessKeyId, secretKey, sessionToken, service, signedAt, true);
}
@Override
public Response filter(FilterableRequestSpecification request,
FilterableResponseSpecification response, FilterContext ctx) {
try {
sign(request);
} catch (Exception e) {
throw new IllegalStateException("could not sign the request", e);
}
return ctx.next(request, response);
}
private void sign(FilterableRequestSpecification request) throws Exception {
Map<String, String> signed = new LinkedHashMap<>();
if (signContentType) {
signed.put("content-type", request.getContentType());
}
String target = request.getHeaders().getValue("X-Amz-Target");
if (target != null) {
signed.put("x-amz-target", target);
}
headersFor(request.getMethod(), URI.create(request.getURI()), signed, bodyBytes(request.getBody()))
.forEach(request::header);
}
/**
* The headers a signed request must carry ({@code X-Amz-Date}, {@code Authorization} and, with a
* session token, {@code X-Amz-Security-Token}), for tests that send with a client other than
* RestAssured. {@code host}, {@code x-amz-date} and the token are always signed; {@code extra}
* names any further headers to sign, by lowercase name, with the values the request will carry.
*/
public Map<String, String> headersFor(String method, URI uri, Map<String, String> extra, byte[] body)
throws Exception {
String amzDate = AMZ_DATE.format(signedAt != null ? signedAt : Instant.now());
String scopeDate = amzDate.substring(0, 8);
String host = uri.getPort() > 0 && uri.getPort() != 80 && uri.getPort() != 443
? uri.getHost() + ":" + uri.getPort()
: uri.getHost();
TreeMap<String, String> headers = new TreeMap<>(extra);
headers.put("host", host);
headers.put("x-amz-date", amzDate);
if (sessionToken != null) {
headers.put("x-amz-security-token", sessionToken);
}
String signedHeaders = String.join(";", headers.keySet());
String canonicalHeaders = headers.entrySet().stream()
.map(h -> h.getKey() + ":" + h.getValue() + "\n")
.collect(Collectors.joining());
String canonicalRequest = method + "\n"
+ doubleEncodedPath(uri.getRawPath()) + "\n"
+ S3RequestSigner.canonicalQueryString(uri.getRawQuery()) + "\n"
+ canonicalHeaders + "\n"
+ signedHeaders + "\n"
+ SigV4RequestValidator.sha256Hex(body);
String credentialScope = scopeDate + "/us-east-1/" + service + "/aws4_request";
String stringToSign = ALGORITHM + "\n" + amzDate + "\n" + credentialScope + "\n"
+ SigV4RequestValidator.sha256Hex(canonicalRequest.getBytes(StandardCharsets.UTF_8));
byte[] signingKey = SigV4RequestValidator.deriveSigningKey(secretKey, scopeDate, "us-east-1", service);
String signature = SigV4RequestValidator.hexEncode(SigV4RequestValidator.hmacSha256(signingKey, stringToSign));
Map<String, String> result = new LinkedHashMap<>();
result.put("X-Amz-Date", amzDate);
if (sessionToken != null) {
result.put("X-Amz-Security-Token", sessionToken);
}
result.put("Authorization", ALGORITHM + " Credential=" + accessKeyId + "/" + credentialScope
+ ", SignedHeaders=" + signedHeaders + ", Signature=" + signature);
return result;
}
private static String doubleEncodedPath(String rawPath) {
String path = rawPath == null || rawPath.isEmpty() ? "/" : rawPath;
String[] segments = path.split("/", -1);
StringBuilder encoded = new StringBuilder();
for (int i = 0; i < segments.length; i++) {
if (i > 0) {
encoded.append('/');
}
encoded.append(S3RequestSigner.uriEncode(segments[i]));
}
return encoded.toString();
}
private static byte[] bodyBytes(Object body) {
if (body == null) {
return new byte[0];
}
if (body instanceof byte[] bytes) {
return bytes;
}
if (body instanceof String text) {
return text.getBytes(StandardCharsets.UTF_8);
}
throw new IllegalArgumentException("unsupported body type " + body.getClass().getName());
}
}