diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index b3400b6a06..e98c152702 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -226,10 +226,12 @@ placements AWS accepts are honoured: an `Authorization` header and a presigned q (`X-Amz-Algorithm=AWS4-HMAC-SHA256`). The credential must be scoped to the `execute-api` service. Temporary credentials must also present the session token they were issued with, as -`X-Amz-Security-Token` (a header, or a query parameter on a presigned request). The token is +`X-Amz-Security-Token` (a header, or a query parameter on a presigned request). As on AWS, the +token is read only from where the signature is, and an empty one counts as sent. The token is compared against the value recorded when STS minted the credential, so a missing or fabricated one -is rejected: the secret alone is not the whole credential. Whether the token is covered by -`SignedHeaders` is not required either way, since SigV4 leaves that service-specific. +is rejected: the secret alone is not the whole credential. A long-term key must come with no +token, as on AWS. Whether the token is covered by `SignedHeaders` is not required either way, since +SigV4 leaves that service-specific. Rejections are `403`, and never reach the integration: @@ -238,7 +240,8 @@ Rejections are `403`, and never reach the integration: | No SigV4 credentials at all | `Missing Authentication Token` / `MissingAuthenticationTokenException` | `Forbidden` | | Credentials present but incomplete, or scoped to another service | `Incomplete Signature` / `IncompleteSignatureException` | `Forbidden` | | Access key the emulator never issued | `The security token included in the request is invalid.` / `UnrecognizedClientException` | `Forbidden` | -| Temporary credential presenting no `X-Amz-Security-Token`, or one that is not the token it was issued | `The security token included in the request is invalid.` / `UnrecognizedClientException` | `Forbidden` | +| Temporary credential presenting no `X-Amz-Security-Token`, or an empty one with any key | `The security token included in the request is invalid.` / `UnrecognizedClientException` | `Forbidden` | +| `X-Amz-Security-Token` that is not the token the key was issued with, or any token with a long-term key | `The security token included in the request is invalid` / `UnrecognizedClientException` | `Forbidden` | | `X-Amz-Date` outside the 5-minute window, or a presigned URL past `X-Amz-Expires` | `Signature expired` / `InvalidSignatureException` | `Forbidden` | | Signature mismatch | `The request signature we calculated does not match…` / `InvalidSignatureException` | `Forbidden` | diff --git a/src/main/java/io/github/hectorvent/floci/core/common/auth/SessionTokenCheck.java b/src/main/java/io/github/hectorvent/floci/core/common/auth/SessionTokenCheck.java new file mode 100644 index 0000000000..f79088bdc5 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/auth/SessionTokenCheck.java @@ -0,0 +1,40 @@ +package io.github.hectorvent.floci.core.common.auth; + +import io.github.hectorvent.floci.services.iam.IamService; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; + +/** + * Whether the session token on a SigV4 request fits the access key that signed it. A temporary key + * needs the token it was issued with, and a long-term key must come without one, as on AWS. The + * token counts whether it is inside the signature or not, and an empty one counts too. + */ +public enum SessionTokenCheck { + VALID, + /** A temporary key with no token, or an empty token with any key. */ + MISSING, + /** A token that was not issued with the key, or any token with a long-term key. */ + INVALID; + + /** {@code sessionToken} is null when the request carries none. */ + public static SessionTokenCheck of(IamService iamService, String accessKeyId, String sessionToken) { + boolean temporary = IamService.isTemporaryAccessKey(accessKeyId); + if (sessionToken == null) { + return temporary ? MISSING : VALID; + } + if (sessionToken.isBlank()) { + return MISSING; + } + if (!temporary) { + return INVALID; + } + String issued = iamService.findSessionToken(accessKeyId).orElse(null); + // A session stored before Floci recorded tokens has none to compare. + if (issued == null || MessageDigest.isEqual(issued.getBytes(StandardCharsets.UTF_8), + sessionToken.getBytes(StandardCharsets.UTF_8))) { + return VALID; + } + return INVALID; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java index b6041af99e..78e9170127 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java @@ -2996,6 +2996,9 @@ public class ApiGatewayExecuteController { "Incomplete Signature", GatewayResponseType.INVALID_SIGNATURE); case UNKNOWN_KEY -> new RestIamError("UnrecognizedClientException", "The security token included in the request is invalid.", GatewayResponseType.ACCESS_DENIED); + // AWS drops the period when a session token came and does not fit the key. + case INVALID_TOKEN -> new RestIamError("UnrecognizedClientException", + "The security token included in the request is invalid", GatewayResponseType.ACCESS_DENIED); case EXPIRED -> new RestIamError("InvalidSignatureException", "Signature expired", GatewayResponseType.EXPIRED_TOKEN); case MISMATCH -> new RestIamError("InvalidSignatureException", diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ExecuteApiSigV4Authorizer.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ExecuteApiSigV4Authorizer.java index 7b3c110225..d661370261 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ExecuteApiSigV4Authorizer.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ExecuteApiSigV4Authorizer.java @@ -2,6 +2,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.SessionTokenCheck; import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization; import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator; import io.github.hectorvent.floci.core.common.auth.SigV4SignedRequest; @@ -16,8 +17,6 @@ import jakarta.ws.rs.core.UriInfo; import org.jboss.logging.Logger; import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; import java.time.Instant; /** @@ -31,8 +30,8 @@ import java.time.Instant; * string, the headers named in {@code SignedHeaders}, and the SHA-256 of the body), and the derived * signature is compared in constant time. * - *

A temporary credential must additionally present the session token it was issued with, which - * is checked by {@link #checkSessionToken}. + *

A temporary credential must also present the session token it was issued with, and a long-term + * one must present none. {@link #checkSessionToken} checks this. * *

Deliberate deviations from real AWS

*