Skip to content
Myr

fix(accept): replace regex split to mitigate ReDoS (#4758)

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

* fix(accept): replace regex split to mitigate ReDoS * test(accept): cover escaped quote and backslash parsing paths * fix(accept): handle quoted params and comma safely * refactor: improve accept header parsing ---------

authored by EdamAmex · committed by GitHub · parent 18cc595 · Browse files · 2 files+328 added−45 removed
This commit

State

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

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+328 added−45 removed

  • tests:+107 added−4 removed
  • other:+221 added−41 removed

2 files+328 added−45 removed

modified:src/utils/accept.test.ts+107 added-4 removed
15 unmodified lines
16 expect(parseAccept(undefined as any)).toEqual([])17 // eslint-disable-next-line @typescript-eslint/no-explicit-any18 expect(parseAccept(null as any)).toEqual([])19 })2021 test('handles whitespace-only header', () => {22 expect(parseAccept(' ')).toEqual([])23 expect(parseAccept(' \t\n ')).toEqual([])24 })25 })2627 describe('Quality Values', () => {28 test('handles extreme q values', () => {
21 unmodified lines
50 const header = 'type;a=1;b="2";c=\'3\';d="semi;colon";e="nested"quoted""'51 const result = parseAccept(header)52 expect(result[0].params).toEqual({53 a: '1',49 b: '"2"',5054 b: '2',55 c: "'3'",52 d: '"semi;colon"',53 e: '"nested"quoted""',56 d: 'semi;colon',57 })58 })5960 test('handles malformed parameters', () => {
46 unmodified lines
107 const header = '\n\t a \t\n ; \n\t q=0.9 \t\n , \n\t b \t\n'108 const result = parseAccept(header)109 expect(result.map((x) => x.type)).toEqual(['b', 'a'])110 })111112 test('handles comma inside quoted parameter value', () => {113 const header = 'text/plain;meta="a,b";q=0.8,application/json;q=0.7'114 const result = parseAccept(header)115 expect(result).toEqual([116 {117 type: 'text/plain',118 params: {119 meta: 'a,b',120 q: '0.8',121 },122 q: 0.8,123 },124 {125 type: 'application/json',126 params: {127 q: '0.7',128 },129 q: 0.7,130 },131 ])132 })133134 test('handles escaped quote and semicolon inside quoted parameter', () => {135 const header = 'text/plain;meta="a\\\";b";q=0.5'136 const result = parseAccept(header)137 expect(result).toEqual([138 {139 type: 'text/plain',140 params: {141 meta: 'a";b',142 q: '0.5',143 },144 q: 0.5,145 },146 ])147 })148149 test('handles escaped character inside quoted parameter', () => {150 const header = 'text/plain;meta="a\\\\z;c";q=0.3'151 const result = parseAccept(header)152 expect(result).toEqual([153 {154 type: 'text/plain',155 params: {156 meta: 'a\\z;c',157 q: '0.3',158 },159 q: 0.3,160 },161 ])162 })163164 test('skips invalid param without swallowing next media type', () => {165 const header = 'a;foo, b;q=0.5'166 const result = parseAccept(header)167 expect(result).toEqual([168 { type: 'a', params: {}, q: 1 },169 { type: 'b', params: { q: '0.5' }, q: 0.5 },170 ])171 })172173 test('skips malformed quoted param tail without creating bogus media type', () => {174 const header = 'a;foo="x"bar,b'175 const result = parseAccept(header)176 expect(result).toEqual([177 { type: 'a', params: {}, q: 1 },178 { type: 'b', params: {}, q: 1 },179 ])180 })181182 test('parses params after quoted value with trailing whitespace', () => {183 const header = 'a;foo="x" ;q=0.5,b;q=0.4'184 const result = parseAccept(header)185 expect(result).toEqual([186 { type: 'a', params: { foo: 'x', q: '0.5' }, q: 0.5 },187 { type: 'b', params: { q: '0.4' }, q: 0.4 },188 ])189 })190191 test('handles quoted param followed immediately by comma', () => {192 const header = 'a;foo="x",b'193 const result = parseAccept(header)194 expect(result).toEqual([195 { type: 'a', params: { foo: 'x' }, q: 1 },196 { type: 'b', params: {}, q: 1 },197 ])198 })199200 test('skips empty media type that starts with semicolon', () => {201 const header = ';q=0.5,b;q=0.4'202 const result = parseAccept(header)203 expect(result).toEqual([{ type: 'b', params: { q: '0.4' }, q: 0.4 }])204 })205 })206207 describe('Security Cases', () => {208 test('handles potential injection patterns', () => {
8 unmodified lines
217 expect(() => parseAccept(header)).not.toThrow()218 })219 })220221 test('handles many semicolons with an unbalanced quote', () => {222 const header = `text/plain;${'a;'.repeat(8000)}"`223 const result = parseAccept(header)224 expect(result[0].type).toBe('text/plain')225 })226227 test('handles extremely large input', () => {228 const header = 'a;q=0.9,'.repeat(100000)229 expect(() => parseAccept(header)).not.toThrow()230 })
31 unmodified lines
modified:src/utils/accept.ts+221 added-41 removed