Skip to content

Commit fa2c74f

Browse files
yusukebeusualomaautofix-ci[bot]
authored
fix(aws-lambda): handle invalid header names in request processing (#4883)
* fix(aws-lambda): handle invalid header names in request processing Co-authored-by: Taku Amano <taku@taaas.jp> * ci: apply automated fixes --------- Co-authored-by: Taku Amano <taku@taaas.jp> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 3779927 commit fa2c74f

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/adapter/aws-lambda/handler.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import { Hono } from '../../hono'
12
import type { LambdaEvent, LatticeProxyEventV2 } from './handler'
2-
import { getProcessor, isContentEncodingBinary, defaultIsContentTypeBinary } from './handler'
3+
import {
4+
getProcessor,
5+
handle,
6+
isContentEncodingBinary,
7+
defaultIsContentTypeBinary,
8+
} from './handler'
39

410
// Base event objects to reduce duplication
511
const baseV1Event: LambdaEvent = {
@@ -360,3 +366,53 @@ describe('EventProcessor.createRequest', () => {
360366
})
361367
})
362368
})
369+
370+
describe('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)
375+
376+
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+
}
393+
394+
const result = await handler(event)
395+
expect(result.statusCode).toBe(400)
396+
expect(result.body).toBe('Invalid request')
397+
})
398+
399+
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)
403+
404+
const event: LambdaEvent = {
405+
...baseV1Event,
406+
headers: {
407+
'a"a': 'invalid header name',
408+
},
409+
multiValueHeaders: {
410+
'a"a': ['invalid header name'],
411+
},
412+
}
413+
414+
const result = await handler(event)
415+
expect(result.statusCode).toBe(400)
416+
expect(result.body).toBe('Invalid request')
417+
})
418+
})

src/adapter/aws-lambda/handler.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,18 @@ export const handle = <E extends Env = Env, S extends Schema = {}, BasePath exte
252252
return async (event, lambdaContext?) => {
253253
const processor = getProcessor(event)
254254

255-
const req = processor.createRequest(event)
256-
const requestContext = getRequestContext(event)
255+
let req, requestContext
256+
try {
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 TypeError
263+
? new Response('Invalid request', { status: 400 })
264+
: new Response('Internal Server Error', { status: 500 })
265+
return processor.createResult(event, errorResponse, { isContentTypeBinary })
266+
}
257267

258268
const res = await app.fetch(req, {
259269
event,

0 commit comments

Comments
 (0)