Skip to content
Myr

fix: use `Object.create(null)` when parsing query, headers, and params (#5161)

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 a88c89d · Browse files · 6 files+24 added−3 removed
This commit

State

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

Diff stats

6 files+24 added−3 removed

  • tests:+21 added−0 removed
  • other:+3 added−3 removed

6 files+24 added−3 removed

modified:src/utils/accept.test.ts+6 added
68 unmodified lines
69 const result = parseAccept(header)70 expect(result[0].params.key).toBe('2')71 expect(result[0].params.KEY).toBe('3')72 })7374 test('treats `__proto__` as a normal parameter without changing the prototype', () => {75 const result = parseAccept('text/html;__proto__=x')76 expect(Object.getPrototypeOf(result[0].params)).toBeNull()77 expect(result[0].params['__proto__']).toBe('x')78 })79 })8081 describe('Media Type Edge Cases', () => {82 test('handles malformed media types', () => {
185 unmodified lines
modified:src/utils/accept.ts+1 added-1 removed
160 unmodified lines
161 startIndex: number162): [number, Accept | undefined] => {163 const accept: Accept = {164 type: '',165 params: {},165 params: Object.create(null),166 q: 1,167 }168 startIndex = consumeWhitespace(acceptHeader, startIndex)169 let i = startIndex
97 unmodified lines
modified:src/utils/url.test.ts+9 added
357 unmodified lines
358 expect(getQueryParams('http://example.com/?toString')).toEqual({359 toString: [''],360 })361 })362363 it('should treat `__proto__` as a normal key without changing the prototype', () => {364 const params = getQueryParams('http://example.com/?__proto__=a&__proto__=b') as Record<365 string,366 string[]367 >368 expect(Object.getPrototypeOf(params)).toBeNull()369 expect(params['__proto__']).toEqual(['a', 'b'])370 })371 })372})
modified:src/utils/url.ts+1 added-1 removed
250 unmodified lines
251 }252 // fallback to default routine253 }254255 const results: Record<string, string> | Record<string, string[]> = {}255 const results: Record<string, string> | Record<string, string[]> = Object.create(null)256 encoded ??= /[%+]/.test(url)257258 let keyIndex = url.indexOf('?', 8)259 while (keyIndex !== -1) {
60 unmodified lines
modified:src/request.test.ts+6 added
234 unmodified lines
235 )236 expect(req.header('Content-Type')).toBe('application/json')237 expect(req.header('ApiKey')).toBe('abc')238 })239240 test('req.header() is not affected by a `__proto__` header name', () => {241 const req = new HonoRequest(new Request('http://localhost', { headers: { __proto__: 'evil' } }))242 const headers = req.header()243 expect(Object.getPrototypeOf(headers)).toBeNull()244 })245})246247const text = '{"foo":"bar"}'248const json = { foo: 'bar' }
398 unmodified lines
modified:src/request.ts+1 added-1 removed
188 unmodified lines
189 if (name) {190 return this.raw.headers.get(name) ?? undefined191 }192193 const headerData: Record<string, string | undefined> = {}193 const headerData: Record<string, string | undefined> = Object.create(null)194 this.raw.headers.forEach((value, key) => {195 headerData[key] = value196 })197 return headerData
316 unmodified lines