mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 09:22:49 -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
+48
-14
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4AuthorizationHeader;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.ApiGatewayResource;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.ApiKey;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.GatewayResponse;
|
||||
@@ -458,7 +459,7 @@ public class ApiGatewayExecuteController {
|
||||
case "AWS" -> invokeAwsIntegration(scope, region, httpMethod, path, stageName,
|
||||
matched, integration, headers, uriInfo, body, authorizerResult);
|
||||
case "HTTP_PROXY" -> invokeHttpProxy(scope, apiId, httpMethod, path, proxy, stageName,
|
||||
matched, integration, headers, uriInfo, body);
|
||||
matched, integration, headers, uriInfo, body, iamIdentity);
|
||||
case "HTTP" -> invokeHttpIntegration(scope, region, apiId, httpMethod, path, proxy, stageName,
|
||||
matched, integration, headers, uriInfo, body, authorizerResult);
|
||||
case "MOCK" -> invokeMock(scope, region, httpMethod, path, stageName,
|
||||
@@ -534,7 +535,8 @@ public class ApiGatewayExecuteController {
|
||||
private Response invokeHttpProxy(GatewayResponseScope scope, String apiId, String httpMethod, String path,
|
||||
String proxy, String stageName, ApiGatewayResource resource,
|
||||
Integration integration, HttpHeaders headers,
|
||||
UriInfo uriInfo, byte[] body) {
|
||||
UriInfo uriInfo, byte[] body,
|
||||
ExecuteApiSigV4Authorizer.CallerIdentity iamIdentity) {
|
||||
String uri = integration.getUri();
|
||||
if (uri == null || uri.isBlank()) {
|
||||
return gatewayResponse(scope, GatewayResponseType.API_CONFIGURATION_ERROR, 500,
|
||||
@@ -545,11 +547,10 @@ public class ApiGatewayExecuteController {
|
||||
// proxy integration passes the request through, so "?tag=a&tag=b" has to arrive as two
|
||||
// tag parameters and not as "tag=a,b". The joined single-value maps are only the lookup
|
||||
// surface for method.request.* parameter mapping, which resolves to one value in AWS too.
|
||||
Map<String, List<String>> multiValueHeaders = new LinkedHashMap<>();
|
||||
Map<String, List<String>> multiValueHeaders = integrationRequestHeaders(headers, iamIdentity);
|
||||
Map<String, String> headerMap = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, List<String>> e : headers.getRequestHeaders().entrySet()) {
|
||||
if (e.getValue().isEmpty()) continue;
|
||||
multiValueHeaders.put(e.getKey(), List.copyOf(e.getValue()));
|
||||
headerMap.put(e.getKey(), String.join(",", e.getValue()));
|
||||
}
|
||||
Map<String, List<String>> multiValueQuery = new LinkedHashMap<>();
|
||||
@@ -1137,8 +1138,8 @@ public class ApiGatewayExecuteController {
|
||||
node.put("resource", resourcePath);
|
||||
node.put("path", preservedPath);
|
||||
node.put("httpMethod", httpMethod);
|
||||
putSingleValueHeaders(node, headers);
|
||||
putMultiValueHeaders(node, headers);
|
||||
putSingleValueHeaders(node, headers.getRequestHeaders());
|
||||
putMultiValueHeaders(node, headers.getRequestHeaders());
|
||||
putQueryStringParameters(node, uriInfo);
|
||||
putMultiValueQueryStringParameters(node, uriInfo);
|
||||
|
||||
@@ -1262,8 +1263,9 @@ public class ApiGatewayExecuteController {
|
||||
event.put("path", requestPath);
|
||||
event.put("httpMethod", httpMethod);
|
||||
|
||||
putSingleValueHeaders(event, headers);
|
||||
putMultiValueHeaders(event, headers);
|
||||
Map<String, List<String>> integrationHeaders = integrationRequestHeaders(headers, iamIdentity);
|
||||
putSingleValueHeaders(event, integrationHeaders);
|
||||
putMultiValueHeaders(event, integrationHeaders);
|
||||
putQueryStringParameters(event, uriInfo);
|
||||
putMultiValueQueryStringParameters(event, uriInfo);
|
||||
|
||||
@@ -1376,10 +1378,42 @@ public class ApiGatewayExecuteController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The inbound headers a REST integration receives. AWS drops {@code Authorization} on the way
|
||||
* to a Lambda or HTTP integration when the method is AWS_IAM ({@code iamIdentity} is non-null
|
||||
* only then) or the header carries a SigV4 signature, so a function behind a signed call never
|
||||
* sees the caller's signature. Every other header, {@code X-Amz-Date} and
|
||||
* {@code X-Amz-Security-Token} included, passes through.
|
||||
*/
|
||||
static Map<String, List<String>> integrationRequestHeaders(HttpHeaders headers,
|
||||
ExecuteApiSigV4Authorizer.CallerIdentity iamIdentity) {
|
||||
Map<String, List<String>> forwarded = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, List<String>> e : headers.getRequestHeaders().entrySet()) {
|
||||
if (e.getValue().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (HttpHeaders.AUTHORIZATION.equalsIgnoreCase(e.getKey())
|
||||
&& (iamIdentity != null || isSigV4Authorization(e.getValue()))) {
|
||||
continue;
|
||||
}
|
||||
forwarded.put(e.getKey(), e.getValue());
|
||||
}
|
||||
return forwarded;
|
||||
}
|
||||
|
||||
private static boolean isSigV4Authorization(List<String> values) {
|
||||
for (String value : values) {
|
||||
if (SigV4AuthorizationHeader.parse(value) != null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Package-private for unit testing (see ApiGatewayExecuteControllerTest).
|
||||
void putSingleValueHeaders(ObjectNode event, HttpHeaders headers) {
|
||||
void putSingleValueHeaders(ObjectNode event, Map<String, List<String>> headers) {
|
||||
ObjectNode headersNode = event.putObject("headers");
|
||||
headers.getRequestHeaders().forEach((name, values) -> {
|
||||
headers.forEach((name, values) -> {
|
||||
// AWS collapses duplicate request headers to the LAST value in the single-value `headers`
|
||||
// map (multiValueHeaders keeps every value). Taking the first value diverged from AWS.
|
||||
if (!values.isEmpty()) {
|
||||
@@ -1388,9 +1422,9 @@ public class ApiGatewayExecuteController {
|
||||
});
|
||||
}
|
||||
|
||||
void putMultiValueHeaders(ObjectNode event, HttpHeaders headers) {
|
||||
void putMultiValueHeaders(ObjectNode event, Map<String, List<String>> headers) {
|
||||
ObjectNode mvHeaders = event.putObject("multiValueHeaders");
|
||||
headers.getRequestHeaders().forEach((name, values) -> {
|
||||
headers.forEach((name, values) -> {
|
||||
ArrayNode arr = mvHeaders.putArray(name);
|
||||
values.forEach(arr::add);
|
||||
});
|
||||
@@ -2906,8 +2940,8 @@ public class ApiGatewayExecuteController {
|
||||
event.put("path", preservedPath);
|
||||
event.put("httpMethod", httpMethod);
|
||||
|
||||
putSingleValueHeaders(event, headers);
|
||||
putMultiValueHeaders(event, headers);
|
||||
putSingleValueHeaders(event, headers.getRequestHeaders());
|
||||
putMultiValueHeaders(event, headers.getRequestHeaders());
|
||||
putQueryStringParameters(event, uriInfo);
|
||||
putMultiValueQueryStringParameters(event, uriInfo);
|
||||
|
||||
|
||||
Reference in new issue
Block a user