mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 17:33:03 -05:00
fix(apigateway): drop the SigV4 Authorization header before REST integrations (#4567)
* fix(apigateway): drop the SigV4 Authorization header before REST integrations API Gateway drops the inbound Authorization header on its way to a REST Lambda or HTTP integration when the method is AWS_IAM, or on any method when the header carries a SigV4 signature. Floci forwarded it into the AWS_PROXY event and the HTTP_PROXY request, so a function serving both Bearer-token users and IAM-signed services saw "AWS4-HMAC-SHA256 ..." where AWS delivers no header at all. X-Amz-Date, X-Amz-Security-Token and every other header still pass through, a Bearer token on a non-IAM method is unchanged, and REQUEST authorizer events keep the header. HTTP API (v2) is unchanged: AWS documents no such stripping there. * test(apigateway): cover method.request.header.Authorization mappings on AWS_IAM routes * test(apigateway): fold the Authorization header tests into ProxyEventIamIdentityTest
This commit is contained in:
1 parent
047d0d848c
commit
2ec5300029
5 files changed
+207
-20
No files matched your search
+2
-4
@@ -123,14 +123,12 @@ class ApiGatewayExecuteControllerTest {
|
||||
requestHeaders.add("X-Dup", "first");
|
||||
requestHeaders.add("X-Dup", "second");
|
||||
requestHeaders.add("X-Dup", "third");
|
||||
HttpHeaders headers = mock(HttpHeaders.class);
|
||||
when(headers.getRequestHeaders()).thenReturn(requestHeaders);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
ObjectNode event = objectMapper.createObjectNode();
|
||||
ApiGatewayExecuteController controller = controller(objectMapper);
|
||||
controller.putSingleValueHeaders(event, headers);
|
||||
controller.putMultiValueHeaders(event, headers);
|
||||
controller.putSingleValueHeaders(event, requestHeaders);
|
||||
controller.putMultiValueHeaders(event, requestHeaders);
|
||||
|
||||
assertEquals("third", event.path("headers").path("X-Dup").asText());
|
||||
assertEquals(
|
||||
|
||||
+75
-1
@@ -2,7 +2,9 @@ package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.testutil.ExecuteApiRequestSigner;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
@@ -13,6 +15,7 @@ import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -23,6 +26,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -49,6 +53,8 @@ class ApiGatewayHttpProxyIntegrationTest {
|
||||
private static final AtomicReference<String> lastBody = new AtomicReference<>();
|
||||
private static final AtomicReference<String> lastForwardedHeader = new AtomicReference<>();
|
||||
private static final AtomicReference<String> lastAuthorization = new AtomicReference<>();
|
||||
private static final AtomicReference<String> lastAmzDate = new AtomicReference<>();
|
||||
private static final AtomicReference<String> lastCallerAuthorization = new AtomicReference<>();
|
||||
private static final AtomicReference<String> lastHost = new AtomicReference<>();
|
||||
private static final AtomicReference<List<String>> lastTraceHeaders = new AtomicReference<>();
|
||||
|
||||
@@ -78,6 +84,8 @@ class ApiGatewayHttpProxyIntegrationTest {
|
||||
lastBody.set(new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8));
|
||||
lastForwardedHeader.set(exchange.getRequestHeaders().getFirst("X-Forwarded-Tenant"));
|
||||
lastAuthorization.set(exchange.getRequestHeaders().getFirst("Authorization"));
|
||||
lastAmzDate.set(exchange.getRequestHeaders().getFirst("X-Amz-Date"));
|
||||
lastCallerAuthorization.set(exchange.getRequestHeaders().getFirst("X-Caller-Authorization"));
|
||||
lastHost.set(exchange.getRequestHeaders().getFirst("Host"));
|
||||
List<String> trace = exchange.getRequestHeaders().get("X-Trace");
|
||||
lastTraceHeaders.set(trace == null ? null : List.copyOf(trace));
|
||||
@@ -105,6 +113,8 @@ class ApiGatewayHttpProxyIntegrationTest {
|
||||
lastBody.set(null);
|
||||
lastForwardedHeader.set(null);
|
||||
lastAuthorization.set(null);
|
||||
lastAmzDate.set(null);
|
||||
lastCallerAuthorization.set(null);
|
||||
lastHost.set(null);
|
||||
lastTraceHeaders.set(null);
|
||||
}
|
||||
@@ -114,6 +124,11 @@ class ApiGatewayHttpProxyIntegrationTest {
|
||||
* HTTP_PROXY integration pointing at {@code targetUri}, deployed to stage {@code test}.
|
||||
*/
|
||||
private String createProxyApi(String name, String targetUri, String requestParametersJson) {
|
||||
return createProxyApi(name, targetUri, requestParametersJson, "NONE");
|
||||
}
|
||||
|
||||
private String createProxyApi(String name, String targetUri, String requestParametersJson,
|
||||
String authorizationType) {
|
||||
String apiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"name\":\"" + name + "\"}")
|
||||
@@ -132,7 +147,7 @@ class ApiGatewayHttpProxyIntegrationTest {
|
||||
.then().statusCode(201).extract().path("id");
|
||||
|
||||
given().contentType(ContentType.JSON)
|
||||
.body("{\"authorizationType\":\"NONE\"}")
|
||||
.body("{\"authorizationType\":\"" + authorizationType + "\"}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/ANY")
|
||||
.then().statusCode(201);
|
||||
|
||||
@@ -317,6 +332,65 @@ class ApiGatewayHttpProxyIntegrationTest {
|
||||
assertEquals("Bearer caller-supplied-token", lastAuthorization.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dropsAuthorizationHeaderOnAwsIamRoute() throws Exception {
|
||||
resetRecordings();
|
||||
String apiId = createProxyApi("http-proxy-iam-auth-api", backendUri(), null, "AWS_IAM");
|
||||
String path = "/execute-api/" + apiId + "/test/orders";
|
||||
Map<String, String> signed = ExecuteApiRequestSigner.signedHeaders(
|
||||
"GET", path, Map.of(), "localhost:" + RestAssured.port, null,
|
||||
"test", "test", "us-east-1", Instant.now());
|
||||
|
||||
// AWS drops Authorization on its way to the integration when the method is AWS_IAM; the
|
||||
// rest of the signed request, X-Amz-Date included, still reaches the backend.
|
||||
given().headers(signed)
|
||||
.when().get(path)
|
||||
.then().statusCode(200);
|
||||
|
||||
assertEquals("/orders", lastPath.get());
|
||||
assertNull(lastAuthorization.get());
|
||||
assertEquals(signed.get("X-Amz-Date"), lastAmzDate.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void methodRequestAuthorizationStillResolvesInParameterMappingOnAwsIamRoute() throws Exception {
|
||||
resetRecordings();
|
||||
String apiId = createProxyApi("http-proxy-iam-auth-mapping-api", backendUri(),
|
||||
"{\"integration.request.header.X-Caller-Authorization\":\"method.request.header.Authorization\"}",
|
||||
"AWS_IAM");
|
||||
String path = "/execute-api/" + apiId + "/test/orders";
|
||||
Map<String, String> signed = ExecuteApiRequestSigner.signedHeaders(
|
||||
"GET", path, Map.of(), "localhost:" + RestAssured.port, null,
|
||||
"test", "test", "us-east-1", Instant.now());
|
||||
|
||||
// Only the header the integration receives is dropped: the method request still carries
|
||||
// Authorization, so an explicit mapping from it resolves as it did before.
|
||||
given().headers(signed)
|
||||
.when().get(path)
|
||||
.then().statusCode(200);
|
||||
|
||||
assertEquals(signed.get("Authorization"), lastCallerAuthorization.get());
|
||||
assertNull(lastAuthorization.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dropsSigV4AuthorizationHeaderOnNoneAuthRoute() {
|
||||
resetRecordings();
|
||||
String apiId = createProxyApi("http-proxy-sigv4-none-api", backendUri(), null);
|
||||
|
||||
// No signature is verified on a NONE route, but AWS still drops an Authorization header
|
||||
// that carries one.
|
||||
given()
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 "
|
||||
+ "Credential=test/20260928/us-east-1/execute-api/aws4_request, "
|
||||
+ "SignedHeaders=host;x-amz-date, Signature=abc123")
|
||||
.when().get("/execute-api/" + apiId + "/test/orders")
|
||||
.then().statusCode(200);
|
||||
|
||||
assertEquals("/orders", lastPath.get());
|
||||
assertNull(lastAuthorization.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotForwardTheInboundHostHeaderToTheBackend() {
|
||||
resetRecordings();
|
||||
|
||||
+76
-1
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -21,6 +22,10 @@ import static org.mockito.Mockito.when;
|
||||
* REST renders it under {@code requestContext.identity}, HTTP APIs under
|
||||
* {@code requestContext.authorizer.iam}. Both were unreachable before AWS_IAM was enforced at
|
||||
* all: REST's identity fields were hardcoded to null with a comment saying so.
|
||||
*
|
||||
* <p>The caller's signature itself must not: AWS drops the {@code Authorization} header before a
|
||||
* REST Lambda proxy integration when the method is AWS_IAM or the header carries a SigV4 signature
|
||||
* ("Amazon API Gateway important notes for REST APIs", header table footnote).
|
||||
*/
|
||||
class ProxyEventIamIdentityTest {
|
||||
|
||||
@@ -29,8 +34,12 @@ class ProxyEventIamIdentityTest {
|
||||
new ExecuteApiSigV4Authorizer.CallerIdentity(
|
||||
"AKIAIOSFODNN7EXAMPLE", "000000000000",
|
||||
"arn:aws:iam::000000000000:user/alice", "AIDAEXAMPLEUSERID");
|
||||
private static final String SIGV4 = "AWS4-HMAC-SHA256 "
|
||||
+ "Credential=AKIAIOSFODNN7EXAMPLE/20260928/us-east-1/execute-api/aws4_request, "
|
||||
+ "SignedHeaders=host;x-amz-date, Signature=abc123";
|
||||
|
||||
private ApiGatewayExecuteController controller;
|
||||
private MultivaluedMap<String, String> requestHeaders;
|
||||
private HttpHeaders headers;
|
||||
private UriInfo uriInfo;
|
||||
|
||||
@@ -40,8 +49,9 @@ class ProxyEventIamIdentityTest {
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
|
||||
|
||||
requestHeaders = new MultivaluedHashMap<>();
|
||||
headers = mock(HttpHeaders.class);
|
||||
when(headers.getRequestHeaders()).thenReturn(new MultivaluedHashMap<>());
|
||||
when(headers.getRequestHeaders()).thenReturn(requestHeaders);
|
||||
|
||||
uriInfo = mock(UriInfo.class);
|
||||
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
|
||||
@@ -107,4 +117,69 @@ class ProxyEventIamIdentityTest {
|
||||
assertTrue(requestContext.path("authorizer").isMissingNode(),
|
||||
"a route with no authorizer must not grow an authorizer node");
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsIamMethodDropsTheSignatureButKeepsTheOtherSigningHeaders() throws Exception {
|
||||
requestHeaders.add("Authorization", SIGV4);
|
||||
requestHeaders.add("X-Amz-Date", "20260928T120000Z");
|
||||
requestHeaders.add("X-Amz-Security-Token", "session-token");
|
||||
|
||||
JsonNode event = restEvent(CALLER);
|
||||
|
||||
assertAuthorizationAbsent(event);
|
||||
assertEquals("20260928T120000Z", event.path("headers").path("X-Amz-Date").asText());
|
||||
assertEquals("session-token", event.path("headers").path("X-Amz-Security-Token").asText());
|
||||
assertEquals("session-token",
|
||||
event.path("multiValueHeaders").path("X-Amz-Security-Token").path(0).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsIamMethodDropsAnAuthorizationHeaderThatIsNotSigV4() throws Exception {
|
||||
// A presigned-URL caller is authenticated by the query string, so its Authorization header
|
||||
// can be anything. AWS still drops it: the method is AWS_IAM.
|
||||
requestHeaders.add("Authorization", "Bearer user-token");
|
||||
|
||||
assertAuthorizationAbsent(restEvent(CALLER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noneMethodDropsASigV4AuthorizationHeader() throws Exception {
|
||||
requestHeaders.add("Authorization", SIGV4);
|
||||
|
||||
assertAuthorizationAbsent(restEvent(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noneMethodPassesABearerAuthorizationHeaderThrough() throws Exception {
|
||||
requestHeaders.add("Authorization", "Bearer user-token");
|
||||
|
||||
JsonNode event = restEvent(null);
|
||||
|
||||
assertEquals("Bearer user-token", event.path("headers").path("Authorization").asText());
|
||||
assertEquals("Bearer user-token",
|
||||
event.path("multiValueHeaders").path("Authorization").path(0).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizationHeaderNameIsMatchedCaseInsensitively() throws Exception {
|
||||
requestHeaders.add("authorization", SIGV4);
|
||||
|
||||
JsonNode event = restEvent(null);
|
||||
|
||||
assertTrue(event.path("headers").path("authorization").isMissingNode());
|
||||
assertTrue(event.path("multiValueHeaders").path("authorization").isMissingNode());
|
||||
}
|
||||
|
||||
private JsonNode restEvent(ExecuteApiSigV4Authorizer.CallerIdentity iamIdentity) throws Exception {
|
||||
return MAPPER.readTree(controller.buildProxyEvent(
|
||||
"us-east-1", "api1", "GET", "/iam", "/iam", "res1", "test", null,
|
||||
headers, uriInfo, null, "req-1", null, null, null, iamIdentity));
|
||||
}
|
||||
|
||||
private static void assertAuthorizationAbsent(JsonNode event) {
|
||||
assertTrue(event.path("headers").path("Authorization").isMissingNode(),
|
||||
"headers must not carry Authorization");
|
||||
assertTrue(event.path("multiValueHeaders").path("Authorization").isMissingNode(),
|
||||
"multiValueHeaders must not carry Authorization");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user