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
Provenance
- Commit
301e142- Tree
c3a064b- Author
watany <76135106+watany-dev@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0efc695
4 files changed+59 added−6 removed
modified:runtime_tests/lambda/index.test.ts+2 added-2 removed
115 unmodified lines116 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 lines137 '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 linesmodified:runtime_tests/lambda-edge/index.test.ts+55 added-1 removed
30 unmodified lines31 await next()32 c.env.callback(null, c.env.request)33 })34 35+ app.post('/post/binary', async (c) => {36+ const body = await c.req.blob()37+ return c.text(`${body.size} bytes`)38+ })39+40 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 lines546 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 lines560 expect(response.status).toBe('200')561 expect(response.body).toBe('Good Morning Lambda!')562 })563 564+ 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+ }607+608+ const response = await handler(event)609+ expect(response.status).toBe('200')610+ expect(response.body).toBe('3 bytes')611+ })612+613 it('Should handle a request and return a 401 response with Basic auth', async () => {614 const event = {615 Records: [616 {115 unmodified linesmodified:src/adapter/aws-lambda/handler.ts+1 added-2 removed
1233456108 unmodified linesExpand all11511611711812011912012112212331 unmodified lines// @denoify-ignoreimport crypto from 'crypto'import { Buffer } from 'node:buffer'import type { Hono } from '../../hono'
import { encodeBase64 } from '../../utils/encode'
108 unmodified linesExpand all method, }
if (event.body) { requestInit.body = event.isBase64Encoded ? new Buffer(event.body, 'base64') : event.body requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.body }
return new Request(url, requestInit)}31 unmodified linesmodified:src/adapter/lambda-edge/handler.ts+1 added-1 removed
140 unmodified lines141 142 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