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

* fix(apigatewayv2): resolve HTTP API region across regions like REST APIs do

Unsigned requests (and requests carrying a non-SigV4 Authorization header,
e.g. a Cognito bearer JWT) resolve to RegionResolver's configured default
region rather than a region the caller specified. For v1 REST APIs,
resolveRestApiRegion already falls back to scanning all regions for the
apiId when that happens. HTTP API (v2) dispatch had no equivalent, so any
HTTP API deployed outside the default region returned 404 "Invalid API id
specified" for every unsigned or bearer-token-authenticated request - which
is effectively every request a browser, curl, or non-SigV4 client sends.

Adds ApiGatewayV2Service#resolveHttpApiRegion, mirroring
ApiGatewayService#resolveRestApiRegion exactly (same region::apiId key
shape), and RegionResolver#isRegionUnresolved to give dispatch() a single
correct signal for "region resolution silently fell back to the default"
that covers both the blank-header and non-SigV4-header cases the previous
blank-check missed.

* test(apigateway): cover HTTP API cross-region resolution and non-SigV4 fallback

Adds unit coverage for the dispatch() region-resolution fix:

- An unsigned request for a v2 HTTP API deployed outside the default region
  must fall back to resolveHttpApiRegion and find it there.
- A correctly-resolved (signed) request must not pay for the fallback scan.
- A non-SigV4 Authorization header (e.g. a Cognito bearer JWT) on a v1 REST
  API must also trigger the region scan, not just a blank/missing header —
  using a real RegionResolver rather than a mock so the test exercises the
  actual header-parsing logic in isRegionUnresolved.

Verified each test fails when the corresponding fix is reverted: the v2 test
and the bearer-JWT test both fail (2 of 12) when dispatch()'s region-fallback
logic is rolled back to the pre-fix version, confirming they aren't
vacuously passing.

* chore: trigger CI

* refactor(apigateway): drop duplicate RegionResolver.isRegionUnresolved

#2169 (merged) added resolveRegionFromAuthOrNull, the same predicate as
isRegionUnresolved added here. Collapse onto the former per hectorvent's
review note on #2169 rather than let both live in core/common.

* test(apigateway): update mocks for merged v1-first dispatch flow

Finish resolving the PR #2149 merge conflict — the test file's edits for
the merged dispatch() ordering weren't included in the merge commit.

* test(apigateway): note that v2 region tests pin #2054, not this PR

Per review feedback on #2149: unsignedRequestFindsV2ApiDeployedOutsideDefaultRegion
and signedV2RequestDoesNotConsultRegionFallback exercise v2 cross-region dispatch
behavior that landed independently in #2054, not the v1 Authorization-header fix
this PR makes. Kept as the only controller-level coverage of that behavior, with
a comment so they aren't read as regression guards for this change.
This commit is contained in:
Jay authored and GitHub committed 2026-08-14 13:42:57 -04:00
1 parent 12154297b5
commit 5156095f01
3 files changed
+124 -2

No files matched your search

@@ -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);
}
@@ -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;