Files
floci/src/test/java
Jay 0444753eb8 fix(apigateway): verify and propagate HTTP API v2 JWT authorizer claims (#2353)
* fix(apigateway): propagate JWT authorizer claims to HTTP API v2 Lambda events

enforceJwtAuthorizer (the HttpApi v2 route path for AuthorizationType:
JWT) correctly verifies a token's issuer/audience/expiry, but discarded
the parsed claims on success instead of exposing them - so
buildV2ProxyEvent never populated requestContext.authorizer.jwt.claims
in the event sent to the Lambda, unlike the sibling CUSTOM/Lambda
REQUEST authorizer path, which does build and propagate a context.

Any Lambda relying solely on the native JWT authorizer's verified
identity (rather than re-verifying the token itself) saw no identity at
all, even though the token was accepted and the request let through.
Confirmed against a real downstream consumer (kbdb, migrating off
in-process token verification in favor of the native authorizer) -
every write route gated on the caller's authorizer-verified `sub`
failed after a real sam deploy to floci, despite passing identically
against real AWS API Gateway.

JwtClaims now retains every claim from the token payload (not just the
iss/aud/client_id/exp fields needed for verification), flattened to
strings the same way AWS's real requestContext.authorizer.jwt.claims
does. enforceJwtAuthorizer returns them via a new JwtAuthorizerResult
(mirroring the existing AuthorizerResult record used by the v1/REST
CUSTOM-authorizer path) instead of a bare Response, and dispatchV2
threads them through buildV2ProxyEvent's new claims parameter, which
attaches requestContext.authorizer.jwt.claims when present.

Verified against kbdb's real template.yaml + Ginkgo functional suite:
was 17/49 passing on Builds (every write-path spec 500ing with "no
verified identity from API Gateway authorizer"); now 385/385 across
all 10 suites.

* fix(apigateway): split JWT scope claim into requestContext.authorizer.jwt.scopes

buildV2ProxyEvent's new requestContext.authorizer.jwt.claims population
(previous commit) carries every claim verbatim, including a raw "scope"
string when present. Real AWS HTTP API JWT authorizers split that
standard OAuth2 space-delimited scope claim (RFC 6749 §3.3) into its
own requestContext.authorizer.jwt.scopes array instead - callers read
scopes from there, not by parsing claims.scope themselves (confirmed
against AWS's documented example payload, which shows a scopes key
alongside claims even when null).

Adds the split as an additional field alongside claims when a "scope"
claim is present and non-blank; omitted entirely otherwise, matching
how the sibling claims/authorizer nodes are already conditionally
built.

* fix(apigateway): verify HTTP API v2 JWT authorizer token signatures

enforceJwtAuthorizer checked a token's iss/aud/exp claims but never verified
the token was actually signed by its issuer - parseJwtClaims only base64-decoded
the JWT payload segment, so any well-formed token whose claimed iss/aud matched
the authorizer's configuration was accepted regardless of whether it was ever
signed by that issuer at all. This predates the claims-propagation fix in this
branch, but that fix raises the stakes: those unverified claims are now exposed
to the downstream Lambda as requestContext.authorizer.jwt.claims, the exact
field real AWS-integrated code trusts as a verified identity.

Adds JwtSignatureVerifier: does real OIDC discovery (GET
{issuer}/.well-known/openid-configuration -> jwks_uri) + JWKS fetch, matches
the token's kid, and verifies its RS256 signature against the resolved key -
the same mechanism real HTTP API JWT authorizers use against a real issuer, not
a Floci-specific shortcut (WebIdentityTokenVerifier's Floci-known-key pattern
doesn't apply here, since a JWT authorizer's issuer is an arbitrary external
IdP). Per-issuer JWKS are cached for 10 minutes so repeat requests to the same
route don't refetch on every call.

Wired into enforceJwtAuthorizer ahead of the existing iss/aud/exp checks -
nothing in the payload is trusted, including those claims, until the signature
itself is proven valid. Any verification failure (bad/forged signature, no
matching kid, unsupported alg including "none", unreachable issuer, missing
issuer config) is a 401, matching every other rejection path here; there is no
fallback to unverified acceptance; a local emulator that can't reach the real
issuer must reject; silently accepting would reopen the same gap.

Rewrote HttpApiJwtAuthorizerQuerystringTest, which previously used unsigned
alg:none tokens against a nonexistent issuer.example.com and asserted they
were accepted - that assumption is exactly what this fix removes. It now signs
real RS256 tokens against a local discovery/JWKS fixture server and adds a
forged-signature case, plus new expired-token and wrong-audience cases
covering the iss/aud/exp checks now that they run after signature
verification. JwtSignatureVerifierTest covers the verifier directly: valid
signature, forged signature (same kid, different key), unknown kid, alg:none,
unreachable issuer, and missing issuer.

* fix(apigateway): HTTP_PROXY JWT claim mappings use the verified token

dispatchHttpProxyV2 independently re-extracted a bearer token from the
Authorization header and re-parsed its claims (unverified) for use in
$context.authorizer.claims.* request-parameter mappings, instead of reusing
the claims dispatchV2 already verified via enforceJwtAuthorizer. The
authorizer's gate itself was not bypassed - a forged/invalid token still
401s before dispatchHttpProxyV2 runs - but extractBearerToken only reads the
Authorization header, ignoring the authorizer's configured identitySource
(which can be a querystring parameter, per HttpApiJwtAuthorizerQuerystringTest).
A caller presenting a validly-signed token via the configured source, plus an
unrelated attacker-controlled Authorization header, had claim mappings resolve
from that second unverified token rather than the one actually checked -
flagged in review as an unverified-claims path the signature-verification fix
in this branch didn't close.

dispatchHttpProxyV2 now takes the already-verified jwtClaims map dispatchV2
computed, instead of re-deriving it. extractBearerToken and parseAllJwtClaims
are now unused and removed.

HttpProxyJwtClaimsMappingIntegrationTest covers this end-to-end: an HTTP_PROXY
integration mapping $context.authorizer.claims.sub to a backend header, hit
with a verified querystring token plus a spoofed Authorization header, and
confirms the backend receives the verified subject - not the spoofed one, and
not nothing when no Authorization header is present at all. Reverting the fix
locally reproduced both failure modes before writing this description.

* fix(apigateway): correct scope-claim comment in HTTP API v2 JWT event

The comment claimed callers never see a raw "scope" claim, but
jwtClaims.forEach(claimsNode::put) copies it into .claims unmodified
before .scopes is split out separately, matching real AWS behavior.
2026-08-17 22:33:26 -04:00
..