Skip to content
Myr

fix(aws-lambda): detect V2 events by request context, not rawPath alone (#5033)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

A V1 (REST API) event behind a custom domain base path mapping also carries a rawPath, so checking for rawPath alone made isProxyEventV2 classify it as a V2 event. The request was then routed on the wrong path and the V2 processor read fields the V1 event does not have. Require the V2-only requestContext.http instead, which every HTTP API and function URL event includes.

authored by VIhaan · committed by GitHub · parent a05813c · Browse files · 2 files+22 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+22 added−1 removed

  • tests:+18 added−0 removed
  • other:+4 added−1 removed

2 files+22 added−1 removed

modified:src/adapter/aws-lambda/handler.test.ts+18 added
415 unmodified lines
416 })417})418419describe('handle', () => {420 it('Should route a V1 REST event by its path even when a base path mapping adds rawPath', async () => {421 const app = new Hono()422 app.get('/my/path', (c) => c.text('Hello'))423 const handler = handle(app)424425 // A custom domain base path mapping makes API Gateway add a `rawPath` to the426 // V1 (REST API) event, holding the path before the mapping was stripped. The427 // event is still V1: it carries `path` and a V1 request context with no `http`.428 const event: LambdaEvent = {429 ...baseV1Event,430 rawPath: '/base/my/path',431 }432433 const result = await handler(event)434 expect(result.statusCode).toBe(200)435 expect(result.body).toBe('Hello')436 })437438 it('Should return 400 when request contains invalid header names (v2)', async () => {439 const app = new Hono()440 app.get('/my/path', (c) => c.text('Hello'))441 const handler = handle(app)
131 unmodified lines
modified:src/adapter/aws-lambda/handler.ts+4 added-1 removed
642 unmodified lines
643 return false644}645646const isProxyEventV2 = (event: LambdaEvent): event is APIGatewayProxyEventV2 => {647 return Object.hasOwn(event, 'rawPath')647 // A V1 (REST API) event behind a custom domain base path mapping also carries a648 // `rawPath`, so `rawPath` alone is not enough to identify a V2 event. Every V2649 // (HTTP API / function URL) event has an `http` object on its request context.650 return Object.hasOwn(event, 'rawPath') && Object.hasOwn(event.requestContext ?? {}, 'http')651}652653const isLatticeEventV2 = (event: LambdaEvent): event is LatticeProxyEventV2 => {654 if (event.requestContext) {
23 unmodified lines