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
Provenance
- Commit
c85aead- Tree
c673f18- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
a88c89d
6 files changed+24 added−3 removed
modified:src/utils/accept.test.ts+6 added
68 unmodified lines69 const result = parseAccept(header)70 expect(result[0].params.key).toBe('2')71 expect(result[0].params.KEY).toBe('3')72 })73+74+ 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 })80 81 describe('Media Type Edge Cases', () => {82 test('handles malformed media types', () => {185 unmodified linesmodified:src/utils/accept.ts+1 added-1 removed
160 unmodified lines161 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 = startIndex97 unmodified linesmodified:src/utils/url.test.ts+9 added
357 unmodified lines358 expect(getQueryParams('http://example.com/?toString')).toEqual({359 toString: [''],360 })361 })362+363+ 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 lines251 }252 // fallback to default routine253 }254 255- const results: Record<string, string> | Record<string, string[]> = {}255+ const results: Record<string, string> | Record<string, string[]> = Object.create(null)256 encoded ??= /[%+]/.test(url)257 258 let keyIndex = url.indexOf('?', 8)259 while (keyIndex !== -1) {60 unmodified linesmodified:src/request.test.ts+6 added
234 unmodified lines235 )236 expect(req.header('Content-Type')).toBe('application/json')237 expect(req.header('ApiKey')).toBe('abc')238 })239+240+ 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 })246 247 const text = '{"foo":"bar"}'248 const json = { foo: 'bar' }398 unmodified linesmodified:src/request.ts+1 added-1 removed
188 unmodified lines189 if (name) {190 return this.raw.headers.get(name) ?? undefined191 }192 193- 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 headerData316 unmodified lines