Merge commit from fork
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 3892a6c · Browse files · 4 files·+86 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+86 added−2 removed
- tests:+73 added−0 removed
- other:+13 added−2 removed
Provenance
- Commit
fa5f9bf- Tree
3e4a25b- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
3892a6c
4 files changed+86 added−2 removed
modified:src/adapter/aws-lambda/handler.test.ts+33 added
1 import { setCookie } from '../../helper/cookie'2 import { Hono } from '../../hono'3+import { bodyLimit } from '../../middleware/body-limit'4 import type { LambdaEvent, LatticeProxyEventV2 } from './handler'5 import {6 getProcessor,7 handle,289 unmodified lines297 expect(request.url).toEqual(298 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value'299 )300 expect(Object.fromEntries(request.headers)).toEqual({301+ 'content-length': '17',302 'content-type': 'application/json',303 cookie: 'cookie1; cookie2',304 header1: 'value1',305 header2: 'value1,value2',37 unmodified lines343 expect(request.url).toEqual(344 'https://my-service-a1b2c3.x1y2z3.vpc-lattice-svcs.us-east-1.on.aws/my/path?parameter1=value1¶meter1=value2¶meter2=value'345 )346 expect(Object.fromEntries(request.headers)).toEqual({347+ 'content-length': '17',348 'content-type': 'application/x-www-form-urlencoded',349 cookie: 'cookie1=value1; cookie2=value2',350 header1: 'value1',351 header2: 'value1, value2',123 unmodified lines475 const result = await handler(event)476 expect(result.statusCode).toBe(400)477 expect(result.body).toBe('Invalid request')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+ })509 })modified:src/adapter/aws-lambda/handler.ts+5 added-1 removed
329 unmodified lines330 method,331 }332 333 if (event.body) {334- requestInit.body = event.isBase64Encoded ? decodeBase64(event.body) : event.body334+ const body = event.isBase64Encoded335+ ? decodeBase64(event.body)336+ : new TextEncoder().encode(event.body)337+ requestInit.body = body338+ headers.set('content-length', body.length.toString())339 }340 341 return new Request(url, requestInit)342 }346 unmodified linesmodified:src/adapter/lambda-edge/handler.test.ts+40 added
1 import { describe } from 'vitest'2 import { setCookie } from '../../helper/cookie'3 import { Hono } from '../../hono'4+import { bodyLimit } from '../../middleware/body-limit'5 import { encodeBase64 } from '../../utils/encode'6 import type { Callback, CloudFrontEdgeEvent } from './handler'7 import { createBody, handle, isContentTypeBinary } from './handler'8 173 unmodified lines182 },183 ],184 })185 })186+187+ it('Should enforce bodyLimit when the client understates Content-Length', async () => {188+ const app = new Hono()189+ app.post(190+ '/upload',191+ bodyLimit({ maxSize: 1024, onError: (c) => c.text('too large', 413) }),192+ async (c) => c.json({ received: (await c.req.text()).length })193+ )194+ const handler = handle(app)195+196+ const event: CloudFrontEdgeEvent = {197+ Records: [198+ {199+ cf: {200+ ...cloudFrontEdgeEvent.Records[0].cf,201+ request: {202+ ...cloudFrontEdgeEvent.Records[0].cf.request,203+ method: 'POST',204+ uri: '/upload',205+ headers: {206+ host: [{ key: 'Host', value: 'hono.dev' }],207+ 'content-type': [{ key: 'Content-Type', value: 'text/plain' }],208+ 'content-length': [{ key: 'Content-Length', value: '1' }],209+ },210+ body: {211+ inputTruncated: false,212+ action: 'read-only',213+ encoding: 'text',214+ data: 'A'.repeat(10000),215+ },216+ },217+ },218+ },219+ ],220+ }221+222+ const res = await handler(event)223+ expect(res.status).toBe('413')224+ })225 })modified:src/adapter/lambda-edge/handler.ts+8 added-1 removed
162 unmodified lines163 })164 165 const requestBody = event.Records[0].cf.request.body166 const method = event.Records[0].cf.request.method167- const body = createBody(method, requestBody)167+ const rawBody = createBody(method, requestBody)168 169+ let body: string | Uint8Array<ArrayBuffer> | undefined = rawBody170+ if (rawBody !== undefined) {171+ const bytes = typeof rawBody === 'string' ? new TextEncoder().encode(rawBody) : rawBody172+ body = bytes173+ headers.set('content-length', bytes.length.toString())174+ }175+176 return new Request(url, {177 headers,178 method,179 body,24 unmodified lines