fix(apigateway): refuse a long-term access key sent with a session token (#5875)

* fix(apigateway): refuse a long-term access key sent with a session token

AWS answers 403 "The security token included in the request is invalid"
when a long-term key comes with X-Amz-Security-Token, signed or not. The
same message without the period answers a temporary key with a wrong token.

The session token check moves to core.common.auth, and Lambda Web IamAuth
uses it too.

Closes #5858

* fix(apigateway): read the session token where the signature is and refuse an empty one

AWS ignores a token in the query of a header-signed request and a token
header on a presigned one. It refuses an empty token with any key.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-17 09:30:35 -04:00
1 parent 9705b2584d
commit 4f4db89d8f
9 files changed
+196 -58

No files matched your search

@@ -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;
}
}
@@ -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",
@@ -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.
*
* <p>A temporary credential must additionally present the session token it was issued with, which
* is checked by {@link #checkSessionToken}.
* <p>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.
*
* <h2>Deliberate deviations from real AWS</h2>
* <ul>
@@ -77,6 +76,8 @@ public class ExecuteApiSigV4Authorizer {
MALFORMED,
/** The credential names an access key this emulator has never issued. */
UNKNOWN_KEY,
/** A session token came that was not issued with the access key. */
INVALID_TOKEN,
/** The signing timestamp is outside the accepted window, or a presigned URL has expired. */
EXPIRED,
/** The signature does not match the one derived from the caller's secret key. */
@@ -164,7 +165,9 @@ public class ExecuteApiSigV4Authorizer {
return Result.rejected(Failure.UNKNOWN_KEY, "access key is not registered");
}
Result sessionToken = checkSessionToken(scope.accessKeyId(), headers, queryParameters);
Result sessionToken = checkSessionToken(scope.accessKeyId(), presigned
? queryParameters.getFirst(SECURITY_TOKEN)
: headers == null ? null : headers.getHeaderString(SECURITY_TOKEN));
if (sessionToken != null) {
return sessionToken;
}
@@ -242,50 +245,19 @@ public class ExecuteApiSigV4Authorizer {
}
/**
* Rejects a temporary credential that does not present the session token Floci issued with it,
* returning {@code null} when there is nothing to object to.
*
* <p>The secret alone is not the whole credential. AWS requires the session token on every
* request made with temporary credentials precisely because it is what confirms the credential
* is live and genuinely STS-issued, so accepting an {@code ASIA...} key on its secret alone
* lets an incomplete credential through a route that is supposed to demand a complete one.
*
* <p>The token is compared against the value recorded at mint time rather than merely required
* to be present, so a fabricated token fails as well as a missing one. Presence is all that can
* be demanded of a session stored before Floci recorded tokens: {@code findSessionToken} is
* empty there, and rejecting it would lock out a credential that is otherwise perfectly valid.
* {@code findSecretKey(accessKeyId, sessionToken)} is the stricter one-step alternative, which
* this deliberately does not use because it cannot tell that case apart from a real mismatch.
*
* <p>Folding the token into the canonical request is deliberately not required. Whether a
* service covers it by {@code SignedHeaders} or appends it after signing is service-specific in
* SigV4, and comparing against the issued value binds the token regardless of where it rode.
* Rejects a session token that does not fit the access key, returning {@code null} when there
* is nothing to object to. AWS reads the token from where the signature is, the header or the
* presigned query, and ignores it in the other place.
*/
private Result checkSessionToken(String accessKeyId, HttpHeaders headers,
MultivaluedMap<String, String> queryParameters) {
if (!IamService.isTemporaryAccessKey(accessKeyId)) {
private Result checkSessionToken(String accessKeyId, String presented) {
if (LEGACY_ACCESS_KEY_ID.equals(accessKeyId)) {
return null;
}
String presented = queryParameters.getFirst(SECURITY_TOKEN);
if (isBlank(presented) && headers != null) {
presented = headers.getHeaderString(SECURITY_TOKEN);
}
if (isBlank(presented)) {
LOG.debugv("execute-api request uses temporary credential accessKey={0} with no {1}",
SigV4RequestValidator.sanitizeForLog(accessKeyId), SECURITY_TOKEN);
return Result.rejected(Failure.UNKNOWN_KEY, "temporary credential presents no session token");
}
String issued = iamService.findSessionToken(accessKeyId).orElse(null);
if (issued == null) {
return null;
}
if (!MessageDigest.isEqual(issued.getBytes(StandardCharsets.UTF_8),
presented.getBytes(StandardCharsets.UTF_8))) {
LOG.debugv("execute-api request presents a session token that does not match the one"
+ " issued for accessKey={0}", SigV4RequestValidator.sanitizeForLog(accessKeyId));
return Result.rejected(Failure.UNKNOWN_KEY, "session token does not match the issued credential");
}
return null;
return switch (SessionTokenCheck.of(iamService, accessKeyId, presented)) {
case VALID -> null;
case MISSING -> Result.rejected(Failure.UNKNOWN_KEY, "session token is missing or empty");
case INVALID -> Result.rejected(Failure.INVALID_TOKEN, "session token was not issued with the access key");
};
}
/**
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
import io.github.hectorvent.floci.core.common.RequestScopes;
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;
@@ -171,16 +172,14 @@ public class LambdaWebIamAuth {
}
/**
* The secret of a key Floci issued. A temporary key needs its session token, and a long-term
* key with a session token is refused, as on AWS. The local {@code test} key signs with
* {@code test}.
* The secret of a key Floci issued, when the session token fits it. The local {@code test} key
* signs with {@code test}.
*/
private String secretKey(String accessKeyId, String sessionToken) {
if (TEST_ACCESS_KEY.equals(accessKeyId)) {
return TEST_ACCESS_KEY;
}
boolean temporary = IamService.isTemporaryAccessKey(accessKeyId);
if (temporary && isBlank(sessionToken) || !temporary && !isBlank(sessionToken)) {
if (SessionTokenCheck.of(iamService, accessKeyId, sessionToken) != SessionTokenCheck.VALID) {
return null;
}
return iamService.findSecretKeyInAnyAccount(accessKeyId, sessionToken)