Skip to content
Myr

accept.ts

This file

Path

src/utils/accept.ts

266 lines · 6.8 KiB · TypeScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 82bdc4d point at it.

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 1–6why, one step back
1export interface Accept {
2 type: string3 params: Record<string, string>4 q: number5}6

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

lines 7–15why, one step back
7const isWhitespace = (char: number): boolean =>
8 char === 32 || char === 9 || char === 10 || char === 13910const consumeWhitespace = (acceptHeader: string, startIndex: number): number => {11 while (startIndex < acceptHeader.length) {12 if (!isWhitespace(acceptHeader.charCodeAt(startIndex))) {13 break14 }15 startIndex++

4.9.0Yusuke Wadabefore adoption82bdc4d

line 16why, one step back
16 }

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

lines 17–18why, one step back
17 return startIndex
18}

4.9.0Yusuke Wadabefore adoption82bdc4d

line 19why, one step back
19

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

lines 20–28why, one step back
20const ignoreTrailingWhitespace = (acceptHeader: string, startIndex: number): number => {
21 while (startIndex > 0) {22 if (!isWhitespace(acceptHeader.charCodeAt(startIndex - 1))) {23 break24 }25 startIndex--26 }27 return startIndex28}

4.9.0Yusuke Wadabefore adoption82bdc4d

line 29why, one step back
29

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

lines 30–43why, one step back
30const skipInvalidParam = (acceptHeader: string, startIndex: number): [number, boolean] => {
31 while (startIndex < acceptHeader.length) {32 const char = acceptHeader.charCodeAt(startIndex)33 if (char === 59) {34 // ';' => next param35 return [startIndex + 1, true]36 }37 if (char === 44) {38 // ',' => next accept value39 return [startIndex + 1, false]40 }41 startIndex++42 }43 return [startIndex, false]

4.9.0Yusuke Wadabefore adoption82bdc4d

line 44why, one step back
44}

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

lines 45–61why, one step back
45
46const skipInvalidAcceptValue = (acceptHeader: string, startIndex: number): number => {47 let i = startIndex48 let inQuotes = false49 while (i < acceptHeader.length) {50 const char = acceptHeader.charCodeAt(i)51 if (inQuotes && char === 92) {52 // '\' => escape53 i++54 } else if (char === 34) {55 // '"' => toggle quotes56 inQuotes = !inQuotes57 } else if (!inQuotes && char === 44) {58 // ',' => next accept value59 return i + 160 }61 i++

4.9.0Yusuke Wadabefore adoption82bdc4d

line 62why, one step back
62 }

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

lines 63–64why, one step back
63 return i
64}

4.9.0Yusuke Wadabefore adoption82bdc4d

line 65why, one step back
65

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

lines 66–95why, one step back
66const getNextParam = (
67 acceptHeader: string,68 startIndex: number69): [number, string | undefined, string | undefined, boolean] => {70 startIndex = consumeWhitespace(acceptHeader, startIndex)71 let i = startIndex72 let key: string | undefined73 let value: string | undefined74 let hasNext = false75 while (i < acceptHeader.length) {76 const char = acceptHeader.charCodeAt(i)77 if (char === 61) {78 // '=' => end of key79 key = acceptHeader.slice(startIndex, ignoreTrailingWhitespace(acceptHeader, i))80 i++81 break82 }83 if (char === 59) {84 // ';' => invalid empty param, continue parsing params85 return [i + 1, undefined, undefined, true]86 }87 if (char === 44) {88 // ',' => invalid empty param, move to next accept value89 return [i + 1, undefined, undefined, false]90 }91 i++92 }93 if (key === undefined) {94 return [i, undefined, undefined, false]95 }

4.9.0Yusuke Wadabefore adoption82bdc4d

line 96why, one step back
96

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

lines 97–156why, one step back
97 i = consumeWhitespace(acceptHeader, i)
98 if (acceptHeader.charCodeAt(i) === 61) {99 // '=' is invalid as a value, so return undefined100 const skipResult = skipInvalidParam(acceptHeader, i + 1)101 return [skipResult[0], key, undefined, skipResult[1]]102 }103104 let inQuotes = false105 const paramStartIndex = i106 while (i < acceptHeader.length) {107 const char = acceptHeader.charCodeAt(i)108109 if (inQuotes && char === 92) {110 // '\' => escape111 i++112 } else if (char === 34) {113 // '"' => start of quotes114 if (inQuotes) {115 let nextIndex = consumeWhitespace(acceptHeader, i + 1)116 const nextChar = acceptHeader.charCodeAt(nextIndex)117 if (nextIndex < acceptHeader.length && !(nextChar === 59 || nextChar === 44)) {118 // not ';' or ',' => invalid trailing chars119 const skipResult = skipInvalidParam(acceptHeader, nextIndex)120 return [skipResult[0], key, undefined, skipResult[1]]121 }122 value = acceptHeader.slice(paramStartIndex + 1, i)123 if (value.includes('\\')) {124 value = value.replace(/\\(.)/g, '$1')125 }126 if (nextChar === 44) {127 // ',' => end of accept value128 return [nextIndex + 1, key, value, false]129 }130 if (nextChar === 59) {131 // ';' => has next param132 hasNext = true133 nextIndex++134 }135 i = nextIndex136 break137 }138 inQuotes = true139 } else if (!inQuotes && (char === 59 || char === 44)) {140 // ';' or ',' => end of value141 value = acceptHeader.slice(paramStartIndex, ignoreTrailingWhitespace(acceptHeader, i))142 if (char === 59) {143 // ';' => has next param144 hasNext = true145 }146 i++147 break148 }149 i++150 }151 return [152 i,153 key,154 value ?? acceptHeader.slice(paramStartIndex, ignoreTrailingWhitespace(acceptHeader, i)),155 hasNext,156 ]

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 157–158why, one step back
157}
158

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

lines 159–183why, one step back
159const getNextAcceptValue = (
160 acceptHeader: string,161 startIndex: number162): [number, Accept | undefined] => {163 const accept: Accept = {164 type: '',165 params: {},166 q: 1,167 }168 startIndex = consumeWhitespace(acceptHeader, startIndex)169 let i = startIndex170 while (i < acceptHeader.length) {171 const char = acceptHeader.charCodeAt(i)172 if (char === 59 || char === 44) {173 // ';' or ',' => end of type174 accept.type = acceptHeader.slice(startIndex, ignoreTrailingWhitespace(acceptHeader, i))175 i++176 if (char === 44) {177 // ',' => end of value178 return [i, accept.type ? accept : undefined]179 }180 if (!accept.type) {181 return [skipInvalidAcceptValue(acceptHeader, i), undefined]182 }183 break // parse params

4.9.0Yusuke Wadabefore adoption82bdc4d

line 184why, one step back
184 }

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

lines 185–208why, one step back
185 i++
186 }187 if (!accept.type) {188 accept.type = acceptHeader.slice(189 startIndex,190 ignoreTrailingWhitespace(acceptHeader, acceptHeader.length)191 )192 return [acceptHeader.length, accept.type ? accept : undefined]193 }194195 let param: string | undefined196 let value: string | undefined197 let hasNext: boolean198 while (i < acceptHeader.length) {199 ;[i, param, value, hasNext] = getNextParam(acceptHeader, i)200 if (param && value) {201 accept.params[param] = value202 }203 if (!hasNext) {204 break205 }206 }207208 return [i, accept] as [number, Accept]

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 209–210why, one step back
209}
210

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

lines 211–239why, one step back
211export const parseAccept = (acceptHeader: string): Accept[] => {
212 if (!acceptHeader) {213 return []214 }215216 const values: Accept[] = []217 let i = 0218 let accept: Accept | undefined219 let requiresSort = false // in many cases, accept values are already sorted by quality (e.g. "text/html, application/json;q=0.9, */*;q=0.8")220 let lastAccept: Accept | undefined221 while (i < acceptHeader.length) {222 ;[i, accept] = getNextAcceptValue(acceptHeader, i)223 if (accept) {224 accept.q = parseQuality(accept.params.q)225 values.push(accept)226 if (lastAccept && lastAccept.q < accept.q) {227 // find higher quality accept value, so we need to sort228 requiresSort = true229 }230 lastAccept = accept231 }232 }233 if (requiresSort) {234 values.sort((a, b) => b.q - a.q)235 }236237 return values238}239

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 240–266why, one step back
240const parseQuality = (qVal?: string): number => {
241 if (qVal === undefined) {242 return 1243 }244 if (qVal === '') {245 return 1246 }247 if (qVal === 'NaN') {248 return 0249 }250251 const num = Number(qVal)252 if (num === Infinity) {253 return 1254 }255 if (num === -Infinity) {256 return 0257 }258 if (Number.isNaN(num)) {259 return 1260 }261 if (num < 0 || num > 1) {262 return 1263 }264265 return num266}