Skip to content

Commit 4481850

Browse files
authored
fetch: improve regexes in data-uri.js (#4483)
1 parent 973a1ba commit 4481850

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/web/fetch/data-url.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ const encoder = new TextEncoder()
88
/**
99
* @see https://mimesniff.spec.whatwg.org/#http-token-code-point
1010
*/
11-
const HTTP_TOKEN_CODEPOINTS = /^[!#$%&'*+\-.^_|~A-Za-z0-9]+$/
12-
const HTTP_WHITESPACE_REGEX = /[\u000A\u000D\u0009\u0020]/ // eslint-disable-line
11+
const HTTP_TOKEN_CODEPOINTS = /^[-!#$%&'*+.^_|~A-Za-z0-9]+$/u
12+
const HTTP_WHITESPACE_REGEX = /[\u000A\u000D\u0009\u0020]/u // eslint-disable-line
13+
1314
/**
1415
* @see https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
1516
*/
16-
const HTTP_QUOTED_STRING_TOKENS = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/ // eslint-disable-line
17+
const HTTP_QUOTED_STRING_TOKENS = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/u // eslint-disable-line
1718

1819
// https://fetch.spec.whatwg.org/#data-url-processor
1920
/** @param {URL} dataURL */
@@ -68,7 +69,7 @@ function dataURLProcessor (dataURL) {
6869
// 11. If mimeType ends with U+003B (;), followed by
6970
// zero or more U+0020 SPACE, followed by an ASCII
7071
// case-insensitive match for "base64", then:
71-
if (/;(\u0020){0,}base64$/i.test(mimeType)) {
72+
if (/;(?:\u0020*)base64$/ui.test(mimeType)) {
7273
// 1. Let stringBody be the isomorphic decode of body.
7374
const stringBody = isomorphicDecode(body)
7475

@@ -86,7 +87,7 @@ function dataURLProcessor (dataURL) {
8687

8788
// 5. Remove trailing U+0020 SPACE code points from mimeType,
8889
// if any.
89-
mimeType = mimeType.replace(/(\u0020)+$/, '')
90+
mimeType = mimeType.replace(/(\u0020+)$/u, '')
9091

9192
// 6. Remove the last U+003B (;) code point from mimeType.
9293
mimeType = mimeType.slice(0, -1)
@@ -176,8 +177,9 @@ function percentDecode (input) {
176177
/** @type {Uint8Array} */
177178
const output = new Uint8Array(length)
178179
let j = 0
180+
let i = 0
179181
// 2. For each byte byte in input:
180-
for (let i = 0; i < length; ++i) {
182+
while (i < length) {
181183
const byte = input[i]
182184

183185
// 1. If byte is not 0x25 (%), then append byte to output.
@@ -205,6 +207,7 @@ function percentDecode (input) {
205207
// 3. Skip the next two bytes in input.
206208
i += 2
207209
}
210+
++i
208211
}
209212

210213
// 3. Return output.
@@ -490,7 +493,7 @@ function serializeAMimeType (mimeType) {
490493
if (!HTTP_TOKEN_CODEPOINTS.test(value)) {
491494
// 1. Precede each occurrence of U+0022 (") or
492495
// U+005C (\) in value with U+005C (\).
493-
value = value.replace(/(\\|")/g, '\\$1')
496+
value = value.replace(/[\\"]/ug, '\\$&')
494497

495498
// 2. Prepend U+0022 (") to value.
496499
value = '"' + value

0 commit comments

Comments
 (0)