Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(apigateway): fall back to REST API region scan for non-SigV4 Authorization headers (#2149)

Jay5156095f018d
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 b29e3ec052..9b65d74394 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
@@ -256,8 +256,12 @@ public class ApiGatewayExecuteController {
}
String preferredRegion = region;
- String auth = headers.getHeaderString("Authorization");
- if (auth == null || auth.isBlank()) {
+ // True for SigV4-unsigned requests, and also for requests whose Authorization header
+ // isn't a SigV4 credential at all (e.g. a Cognito bearer JWT) - resolveRegion silently
+ // fell back to defaultRegion in both cases, so the resolved region is a guess.
+ boolean regionUnresolved = regionResolver.resolveRegionFromAuthOrNull(
+ headers == null ? null : headers.getHeaderString("Authorization")) == null;
+ if (regionUnresolved) {
region = apiGatewayService.resolveRestApiRegion(region, apiId);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java
index 5df8e5cf69..a69d7a5668 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java
@@ -127,6 +127,11 @@ public class ApiGatewayV2Service {
.orElseThrow(() -> new AwsException("NotFoundException", "Invalid API id specified", 404));
}
+ /**
+ * Mirrors ApiGatewayService#resolveRestApiRegion: unsigned data-plane requests carry no
+ * region, so preferredRegion is whatever RegionResolver defaults to, which need not match
+ * where the API was actually created. Falls back to scanning stored keys for the apiId.
+ */
public String resolveApiRegion(String preferredRegion, String apiId) {
if (apiStore.get(apiKey(preferredRegion, apiId)).isPresent()) {
return preferredRegion;
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java
index 641446053f..89069a6f7f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java
@@ -2,7 +2,10 @@ package io.github.hectorvent.floci.services.apigateway;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
+import io.github.hectorvent.floci.services.apigatewayv2.model.Api;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
@@ -16,7 +19,11 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** Unit coverage for API Gateway request-event construction and HTTP API v2 route matching. */
@@ -193,4 +200,110 @@ class ApiGatewayExecuteControllerTest {
"""));
assertEquals("application/xml", response.getMediaType().toString());
}
+
+ // ── HTTP API (v2) region resolution for unsigned / non-SigV4 requests ──────
+ //
+ // The two tests below (unsignedRequestFindsV2ApiDeployedOutsideDefaultRegion and
+ // signedV2RequestDoesNotConsultRegionFallback) exercise the v2 cross-region dispatch
+ // behavior that #2054 introduced on `resolveApiRegion` — not this PR's change, which is
+ // scoped to the v1 REST path's Authorization-header check below. They're kept here because
+ // they're the only controller-level coverage of that v2 behavior; treat them as pinning
+ // #2054, not as regression guards for this PR.
+
+ @Test
+ void unsignedRequestFindsV2ApiDeployedOutsideDefaultRegion() {
+ RegionResolver regionResolver = mock(RegionResolver.class);
+ ApiGatewayV2Service apiGatewayV2Service = mock(ApiGatewayV2Service.class);
+ ApiGatewayService apiGatewayService = mock(ApiGatewayService.class);
+ HttpHeaders headers = mock(HttpHeaders.class);
+
+ when(regionResolver.resolveRegion(headers)).thenReturn("us-east-1");
+ // resolveRegionFromAuthOrNull is unstubbed and returns null by default, i.e. unresolved.
+ // Not a v1 API at the default region...
+ when(apiGatewayService.resolveRestApiRegion("us-east-1", "abc123")).thenReturn("us-east-1");
+ when(apiGatewayService.getRestApi("us-east-1", "abc123")).thenThrow(
+ new AwsException("NotFoundException", "Invalid REST API id specified", 404));
+ // The API was actually created in eu-west-1; the default-region lookup must miss...
+ // ...so resolveApiRegion is consulted and finds the real region.
+ when(apiGatewayV2Service.resolveApiRegion("us-east-1", "abc123")).thenReturn("eu-west-1");
+ when(apiGatewayV2Service.getApi("eu-west-1", "abc123")).thenReturn(new Api());
+ // No route configured — dispatchV2 returns 404, but that's downstream of the region fix;
+ // what this test asserts is which region the API/route lookups actually ran against.
+ when(apiGatewayV2Service.findMatchingRoute("eu-west-1", "abc123", "GET", "/hello"))
+ .thenReturn(null);
+
+ ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
+ apiGatewayService, apiGatewayV2Service, null,
+ regionResolver, new ObjectMapper(), null,
+ null, null, null, null, new ApiGatewayExecuteRouteContext());
+
+ Response response = controller.dispatch("GET", "abc123", "prod", "hello", headers, null, null);
+
+ assertEquals(404, response.getStatus());
+ verify(apiGatewayV2Service).findMatchingRoute("eu-west-1", "abc123", "GET", "/hello");
+ }
+
+ @Test
+ void signedV2RequestDoesNotConsultRegionFallback() {
+ RegionResolver regionResolver = mock(RegionResolver.class);
+ ApiGatewayV2Service apiGatewayV2Service = mock(ApiGatewayV2Service.class);
+ ApiGatewayService apiGatewayService = mock(ApiGatewayService.class);
+ HttpHeaders headers = mock(HttpHeaders.class);
+
+ when(regionResolver.resolveRegion(headers)).thenReturn("us-east-1");
+ when(headers.getHeaderString("Authorization")).thenReturn(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260215/us-east-1/execute-api/aws4_request");
+ when(regionResolver.resolveRegionFromAuthOrNull(anyString())).thenReturn("us-east-1");
+ // Not a v1 API — falls through to v2 at the already-resolved (signed) region.
+ when(apiGatewayService.getRestApi("us-east-1", "abc123")).thenThrow(
+ new AwsException("NotFoundException", "Invalid REST API id specified", 404));
+ when(apiGatewayV2Service.resolveApiRegion("us-east-1", "abc123")).thenReturn("us-east-1");
+ when(apiGatewayV2Service.getApi("us-east-1", "abc123")).thenReturn(new Api());
+ when(apiGatewayV2Service.findMatchingRoute("us-east-1", "abc123", "GET", "/hello"))
+ .thenReturn(null);
+
+ ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
+ apiGatewayService, apiGatewayV2Service, null,
+ regionResolver, new ObjectMapper(), null,
+ null, null, null, null, new ApiGatewayExecuteRouteContext());
+
+ controller.dispatch("GET", "abc123", "prod", "hello", headers, null, null);
+
+ // A correctly-signed (or otherwise resolved) request must not pay for the v1 region
+ // scan — resolveRestApiRegion is only for the "region is a guess" case.
+ verify(apiGatewayService, never()).resolveRestApiRegion(anyString(), anyString());
+ verify(apiGatewayV2Service, atLeastOnce()).getApi("us-east-1", "abc123");
+ }
+
+ @Test
+ void nonSigV4AuthorizationHeaderFallsBackToRestApiRegionScan() {
+ // A Cognito bearer JWT (or any Authorization header without a SigV4 Credential=...)
+ // must be treated the same as a missing header: resolveRegion silently defaulted,
+ // so the v1 REST path also needs to fall back to scanning for the real region. Uses
+ // the real RegionResolver (not a mock) so the test exercises the actual header-parsing
+ // logic in resolveRegionFromAuthOrNull, not just a stubbed answer.
+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
+ ApiGatewayV2Service apiGatewayV2Service = mock(ApiGatewayV2Service.class);
+ ApiGatewayService apiGatewayService = mock(ApiGatewayService.class);
+ HttpHeaders headers = mock(HttpHeaders.class);
+ when(headers.getHeaderString("Authorization")).thenReturn("Bearer eyJhbGciOiJIUzI1NiJ9.fake.jwt");
+
+ when(apiGatewayService.resolveRestApiRegion("us-east-1", "restapi1")).thenReturn("ap-southeast-2");
+ when(apiGatewayService.getRestApi("ap-southeast-2", "restapi1")).thenThrow(
+ new AwsException("NotFoundException", "Invalid REST API id specified", 404));
+ // Not a v1 API either — falls through to v2, which must also scan.
+ when(apiGatewayV2Service.resolveApiRegion("us-east-1", "restapi1")).thenReturn("us-east-1");
+ when(apiGatewayV2Service.getApi("us-east-1", "restapi1")).thenThrow(
+ new AwsException("NotFoundException", "Invalid API id specified", 404));
+
+ ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
+ apiGatewayService, apiGatewayV2Service, null,
+ regionResolver, new ObjectMapper(), null,
+ null, null, null, null, new ApiGatewayExecuteRouteContext());
+
+ controller.dispatch("GET", "restapi1", "prod", "hello", headers, null, null);
+
+ verify(apiGatewayService).resolveRestApiRegion("us-east-1", "restapi1");
+ verify(apiGatewayService).getRestApi("ap-southeast-2", "restapi1");
+ }
}