Skip to content
Myr

fix: Safe modification of Buffer (#1238)

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

* refactor lambda-edge * refactor lambda

authored by watany · committed by GitHub · parent 0efc695 · Browse files · 4 files+59 added−6 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+59 added−6 removed

  • tests:+57 added−3 removed
  • other:+2 added−3 removed

4 files+59 added−6 removed

modified:runtime_tests/lambda/index.test.ts+2 added-2 removed
115 unmodified lines
116 headers: {117 'Content-Type': 'application/x-www-form-urlencoded',118 },119 path: '/post',120 body: btoa(searchParam.toString()),120 body: Buffer.from(searchParam.toString()).toString('base64'),121 isBase64Encoded: true,122 requestContext: {123 domainName: 'example.com',124 },
12 unmodified lines
137 'Content-Type': 'application/x-www-form-urlencoded',138 },139 rawPath: '/post',140 rawQueryString: '',141 body: btoa(searchParam.toString()),141 body: Buffer.from(searchParam.toString()).toString('base64'),142 isBase64Encoded: true,143 requestContext: {144 domainName: 'example.com',145 http: {
70 unmodified lines
modified:runtime_tests/lambda-edge/index.test.ts+55 added-1 removed
30 unmodified lines
31 await next()32 c.env.callback(null, c.env.request)33 })3435 app.post('/post/binary', async (c) => {36 const body = await c.req.blob()37 return c.text(`${body.size} bytes`)38 })3940 const username = 'hono-user-a'41 const password = 'hono-password-a'42 app.use('/auth/*', basicAuth({ username, password }))43 app.get('/auth/abc', (c) => c.text('Good Night Lambda!'))
502 unmodified lines
546 body: {547 inputTruncated: false,548 action: 'read-only',549 encoding: 'base64',545 data: btoa(searchParam.toString()),550 data: Buffer.from(searchParam.toString()).toString('base64'),551 },552 },553 },554 },
5 unmodified lines
560 expect(response.status).toBe('200')561 expect(response.body).toBe('Good Morning Lambda!')562 })563564 it('Should handle a POST request with binary and return a 200 response', async () => {565 const array = new Uint8Array([0xc0, 0xff, 0xee])566 const buffer = Buffer.from(array)567 const event = {568 Records: [569 {570 cf: {571 config: {572 distributionDomainName: 'example.com',573 distributionId: 'EXAMPLE123',574 eventType: 'viewer-request',575 requestId: 'exampleRequestId',576 },577 request: {578 clientIp: '123.123.123.123',579 headers: {580 host: [581 {582 key: 'Host',583 value: 'example.com',584 },585 ],586 'content-type': [587 {588 key: 'Content-Type',589 value: 'application/x-www-form-urlencoded',590 },591 ],592 },593 method: 'POST',594 querystring: '',595 uri: '/post/binary',596 body: {597 inputTruncated: false,598 action: 'read-only',599 encoding: 'base64',600 data: buffer.toString('base64'),601 },602 },603 },604 },605 ],606 }607608 const response = await handler(event)609 expect(response.status).toBe('200')610 expect(response.body).toBe('3 bytes')611 })612613 it('Should handle a request and return a 401 response with Basic auth', async () => {614 const event = {615 Records: [616 {
115 unmodified lines
modified:src/adapter/aws-lambda/handler.ts+1 added-2 removed
modified:src/adapter/lambda-edge/handler.ts+1 added-1 removed
140 unmodified lines
141142 const requestBody = event.Records[0].cf.request.body143 requestInit.body =144 requestBody?.encoding === 'base64' && requestBody?.data145 ? atob(requestBody.data)145 ? Buffer.from(requestBody.data, 'base64')146 : requestBody?.data147 return new Request(url, requestInit)148}149
9 unmodified lines