Skip to content

Commit

Permalink
fix: fetch a long base64 url will crash and nothing happens (close: n…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaoboy authored and crysmags committed Feb 27, 2024
1 parent ea8444f commit db02f0c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/fetch/dataURL.js
Expand Up @@ -255,7 +255,7 @@ function percentDecode (input) {
}

// 3. Return output.
return Uint8Array.of(...output)
return Uint8Array.from(output)
}

// https://mimesniff.spec.whatwg.org/#parse-a-mime-type
Expand Down
18 changes: 17 additions & 1 deletion test/fetch/data-uri.js
Expand Up @@ -104,7 +104,7 @@ test('https://url.spec.whatwg.org/#string-percent-decode', (t) => {
const percentDecoded = stringPercentDecode(input)
const expected = [...input].map(c => c.charCodeAt(0))

t.same(percentDecoded, Uint8Array.of(...expected))
t.same(percentDecoded, Uint8Array.from(expected))
t.end()
})

Expand Down Expand Up @@ -211,5 +211,21 @@ test('processing.any.js', async (t) => {
}
}

// https://github.com/nodejs/undici/issues/1574
test('too long base64 url', async (t) => {
const inputStr = 'a'.repeat(1 << 20)
const base64 = Buffer.from(inputStr).toString('base64')
const dataURIPrefix = 'data:application/octet-stream;base64,'
const dataURL = dataURIPrefix + base64
try {
const res = await fetch(dataURL)
const buf = await res.arrayBuffer()
const outputStr = Buffer.from(buf).toString('ascii')
t.same(outputStr, inputStr)
} catch (e) {
t.fail(`failed to fetch ${dataURL}`)
}
})

t.end()
})

0 comments on commit db02f0c

Please sign in to comment.