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

AWS answers 403 UnrecognizedClientException "The security token included
in the request is invalid" to a long-term key with a session token, and to
a temporary key with a wrong one. An unknown key, a temporary key with no
token and an empty token get the same message with a period.

AppSync now uses the session token check API Gateway and Lambda Web share.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-17 13:55:03 -04:00
1 parent 115a919a83
commit ffb3d84bad
5 files changed
+106 -43

No files matched your search

@@ -2,18 +2,25 @@ package io.github.hectorvent.floci.services.appsync;
import io.github.hectorvent.floci.core.common.ResolvedServiceCatalog;
import io.github.hectorvent.floci.services.appsync.graphql.AppSyncExecutionController;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.github.hectorvent.floci.testing.SidecarContainersProfile;
import io.github.hectorvent.floci.testutil.AppSyncRequestSigner;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.RestAssured;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
@@ -36,6 +43,12 @@ class AppSyncAuthIntegrationTest {
@Inject
ResolvedServiceCatalog catalog;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@TestHTTPResource("/")
URI baseUri;
@BeforeAll
static void configureRestAssured() {
SidecarContainersProfile.requireDockerAndImage("floci.services.appsync.graphql-image");
@@ -210,6 +223,42 @@ class AppSyncAuthIntegrationTest {
.body("errors[0].path", equalTo(java.util.List.of("hello")));
}
@Test
void longTermKeyWithASessionTokenIsRefused() throws Exception {
String apiId = given()
.header("Authorization", MGMT_AUTH)
.contentType("application/json")
.body("""
{"name": "token-%s", "authenticationType": "AWS_IAM"}
""".formatted(UUID.randomUUID().toString().substring(0, 8)))
.when()
.post("/v1/apis")
.then()
.statusCode(200)
.extract().path("graphqlApi.apiId");
startSchema(apiId, "type Query { hello: String }");
awaitSchemaSuccess(apiId);
UserCredentials user = EnforcementFixtures.createUser(cleanup, baseUri.getPort(),
EnforcementFixtures.auth("test", "iam"), "appsync-token-user");
String body = "{\"query\":\"{ hello }\"}";
Map<String, String> headers = new LinkedHashMap<>(AppSyncRequestSigner.signedHeaders(apiId,
"localhost:" + RestAssured.port, body, user.accessKeyId(), user.secretAccessKey(), "us-east-1",
Instant.now()));
headers.put("X-Amz-Security-Token", "bogus");
given()
.contentType("application/json")
.headers(headers)
.body(body)
.when()
.post("/v1/apis/" + apiId + "/graphql")
.then()
.statusCode(403)
.header("x-amzn-errortype", equalTo("UnrecognizedClientException"))
.body("errors[0].errorType", equalTo("UnrecognizedClientException"))
.body("errors[0].message", equalTo("The security token included in the request is invalid"));
}
@Test
void additionalModeIamCanIntrospect() throws Exception {
String apiId = given()
@@ -130,9 +130,7 @@ class IamAuthValidatorTest {
Map<String, String> signed = AppSyncRequestSigner.signedHeaders(
"api-1", HOST, BODY, "AKIAUNKNOWN", "whatever-secret", REGION, Instant.now());
AppSyncTransportException ex = assertThrows(AppSyncTransportException.class,
() -> validator.validateRequest(authorization(signed), "api-1", infoWith(signed)));
assertEquals(401, ex.getHttpStatus());
assertRefused(signed, "The security token included in the request is invalid.");
}
@Test
@@ -191,25 +189,44 @@ class IamAuthValidatorTest {
}
@Test
void temporaryCredentialSignedButNoSessionTokenIs401() throws Exception {
void temporaryCredentialSignedButNoSessionTokenIsRefused() throws Exception {
when(iamService.findSecretKey("ASIANOTOKEN")).thenReturn(Optional.of("session-secret"));
Map<String, String> signed = AppSyncRequestSigner.signedHeaders(
"api-1", HOST, BODY, "ASIANOTOKEN", "session-secret", REGION, Instant.now());
AppSyncTransportException ex = assertThrows(AppSyncTransportException.class,
() -> validator.validateRequest(authorization(signed), "api-1", infoWith(signed)));
assertEquals(401, ex.getHttpStatus());
assertRefused(signed, "The security token included in the request is invalid.");
}
@Test
void temporaryCredentialWithMismatchedSessionTokenIs401() throws Exception {
void temporaryCredentialWithMismatchedSessionTokenIsRefused() throws Exception {
when(iamService.findSecretKey("ASIAFORGED")).thenReturn(Optional.of("session-secret"));
when(iamService.findSessionToken("ASIAFORGED")).thenReturn(Optional.of("issued-token"));
Map<String, String> signed = signedWithSessionToken("ASIAFORGED", "session-secret", "forged-token");
assertRefused(signed, "The security token included in the request is invalid");
}
@Test
void longTermKeyWithASessionTokenIsRefused() throws Exception {
assertRefused(signedWithSessionToken("AKIAGOOD", "good-secret", "bogus"),
"The security token included in the request is invalid");
assertRefused(signedWithSessionToken("AKIAGOOD", "good-secret", ""),
"The security token included in the request is invalid.");
}
@Test
void legacyTestKeyTakesAnySessionToken() throws Exception {
Map<String, String> signed = signedWithSessionToken("test", "test", "bogus");
assertEquals("test", validator.validateRequest(authorization(signed), "api-1", infoWith(signed)).get("user"));
}
private void assertRefused(Map<String, String> signed, String message) {
AppSyncTransportException ex = assertThrows(AppSyncTransportException.class,
() -> validator.validateRequest(authorization(signed), "api-1", infoWith(signed)));
assertEquals(401, ex.getHttpStatus());
assertEquals(403, ex.getHttpStatus());
assertEquals("UnrecognizedClientException", ex.getErrorType());
assertEquals(message, ex.getMessage());
}
@Test