|
1 | 1 | import { setCookie } from '../../helper/cookie' |
2 | 2 | import { Hono } from '../../hono' |
| 3 | +import { bodyLimit } from '../../middleware/body-limit' |
3 | 4 | import type { LambdaEvent, LatticeProxyEventV2 } from './handler' |
4 | 5 | import { |
5 | 6 | getProcessor, |
@@ -297,6 +298,7 @@ describe('EventProcessor.createRequest', () => { |
297 | 298 | 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value' |
298 | 299 | ) |
299 | 300 | expect(Object.fromEntries(request.headers)).toEqual({ |
| 301 | + 'content-length': '17', |
300 | 302 | 'content-type': 'application/json', |
301 | 303 | cookie: 'cookie1; cookie2', |
302 | 304 | header1: 'value1', |
@@ -342,6 +344,7 @@ describe('EventProcessor.createRequest', () => { |
342 | 344 | 'https://my-service-a1b2c3.x1y2z3.vpc-lattice-svcs.us-east-1.on.aws/my/path?parameter1=value1¶meter1=value2¶meter2=value' |
343 | 345 | ) |
344 | 346 | expect(Object.fromEntries(request.headers)).toEqual({ |
| 347 | + 'content-length': '17', |
345 | 348 | 'content-type': 'application/x-www-form-urlencoded', |
346 | 349 | cookie: 'cookie1=value1; cookie2=value2', |
347 | 350 | header1: 'value1', |
@@ -473,4 +476,34 @@ describe('handle', () => { |
473 | 476 | expect(result.statusCode).toBe(400) |
474 | 477 | expect(result.body).toBe('Invalid request') |
475 | 478 | }) |
| 479 | + |
| 480 | + it('Should enforce bodyLimit when the client understates Content-Length', async () => { |
| 481 | + const app = new Hono() |
| 482 | + app.post( |
| 483 | + '/upload', |
| 484 | + bodyLimit({ maxSize: 1024, onError: (c) => c.text('too large', 413) }), |
| 485 | + async (c) => c.json({ received: (await c.req.text()).length }) |
| 486 | + ) |
| 487 | + const handler = handle(app) |
| 488 | + |
| 489 | + const event: LambdaEvent = { |
| 490 | + ...baseV2Event, |
| 491 | + rawPath: '/upload', |
| 492 | + headers: { 'content-type': 'text/plain', 'content-length': '1' }, |
| 493 | + body: 'A'.repeat(10000), |
| 494 | + requestContext: { |
| 495 | + ...baseV2Event.requestContext, |
| 496 | + http: { |
| 497 | + method: 'POST', |
| 498 | + path: '/upload', |
| 499 | + protocol: 'HTTP/1.1', |
| 500 | + sourceIp: '192.0.2.1', |
| 501 | + userAgent: 'agent', |
| 502 | + }, |
| 503 | + }, |
| 504 | + } |
| 505 | + |
| 506 | + const result = await handler(event) |
| 507 | + expect(result.statusCode).toBe(413) |
| 508 | + }) |
476 | 509 | }) |
0 commit comments