Skip to content
Myr

fix(aws-lambda): handle invalid header names in request processing (#4883)

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

* fix(aws-lambda): handle invalid header names in request processing Co-authored-by: Taku Amano <taku@taaas.jp> * ci: apply automated fixes ---------

authored by Yusuke Wada · committed by GitHub · parent 3779927 · Browse files · 2 files+69 added−3 removed
This commit

State

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

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>
  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Diff stats

2 files+69 added−3 removed

  • tests:+57 added−1 removed
  • other:+12 added−2 removed

2 files+69 added−3 removed

modified:src/adapter/aws-lambda/handler.test.ts+57 added-1 removed
1import { Hono } from '../../hono'2import type { LambdaEvent, LatticeProxyEventV2 } from './handler'2import { getProcessor, isContentEncodingBinary, defaultIsContentTypeBinary } from './handler'3import {4 getProcessor,5 handle,6 isContentEncodingBinary,7 defaultIsContentTypeBinary,8} from './handler'910// Base event objects to reduce duplication11const baseV1Event: LambdaEvent = {12 version: '1.0',
352 unmodified lines
365 expect(decodeURIComponent(xCity)).toBe('炎')366 })367 })368})369370describe('handle', () => {371 it('Should return 400 when request contains invalid header names (v2)', async () => {372 const app = new Hono()373 app.get('/my/path', (c) => c.text('Hello'))374 const handler = handle(app)375376 const event: LambdaEvent = {377 ...baseV2Event,378 headers: {379 'valid-header': 'value',380 'a"a': 'invalid header name',381 },382 requestContext: {383 ...baseV2Event.requestContext,384 http: {385 method: 'GET',386 path: '/my/path',387 protocol: 'HTTP/1.1',388 sourceIp: '192.0.2.1',389 userAgent: 'agent',390 },391 },392 }393394 const result = await handler(event)395 expect(result.statusCode).toBe(400)396 expect(result.body).toBe('Invalid request')397 })398399 it('Should return 400 when request contains invalid header names (v1)', async () => {400 const app = new Hono()401 app.get('/my/path', (c) => c.text('Hello'))402 const handler = handle(app)403404 const event: LambdaEvent = {405 ...baseV1Event,406 headers: {407 'a"a': 'invalid header name',408 },409 multiValueHeaders: {410 'a"a': ['invalid header name'],411 },412 }413414 const result = await handler(event)415 expect(result.statusCode).toBe(400)416 expect(result.body).toBe('Invalid request')417 })418})
modified:src/adapter/aws-lambda/handler.ts+12 added-2 removed
250 unmodified lines
251 // @ts-expect-error FIXME: Fix return typing252 return async (event, lambdaContext?) => {253 const processor = getProcessor(event)254255 let req, requestContext256 try {255 const req = processor.createRequest(event)256 const requestContext = getRequestContext(event)257 req = processor.createRequest(event)258 requestContext = getRequestContext(event)259 } catch (error) {260 console.error('Error processing request:', error)261 const errorResponse =262 error instanceof TypeError263 ? new Response('Invalid request', { status: 400 })264 : new Response('Internal Server Error', { status: 500 })265 return processor.createResult(event, errorResponse, { isContentTypeBinary })266 }267268 const res = await app.fetch(req, {269 event,270 requestContext,
415 unmodified lines