From 221a3094a2680c8f289db23191c6f22030270d5a Mon Sep 17 00:00:00 2001 From: "yanyuhao.joy" Date: Mon, 25 Jul 2022 20:14:28 +0800 Subject: [PATCH] fix: fetch a long base64 url will crash and nothing happens (close: #1574) --- lib/fetch/dataURL.js | 3 ++- test/fetch/data-uri.js | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/fetch/dataURL.js b/lib/fetch/dataURL.js index 5eb0a514aed..4de4ff09f6f 100644 --- a/lib/fetch/dataURL.js +++ b/lib/fetch/dataURL.js @@ -255,7 +255,8 @@ function percentDecode (input) { } // 3. Return output. - return Uint8Array.of(...output) + // https://github.com/nodejs/undici/issues/1574 + return Uint8Array.from(output) } // https://mimesniff.spec.whatwg.org/#parse-a-mime-type diff --git a/test/fetch/data-uri.js b/test/fetch/data-uri.js index 89c8d2d9d89..f2a63e7d6c5 100644 --- a/test/fetch/data-uri.js +++ b/test/fetch/data-uri.js @@ -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() }) @@ -211,5 +211,24 @@ test('processing.any.js', async (t) => { } } + // https://github.com/nodejs/undici/issues/1574 + test('too long base64 url', async (t) => { + const data = 'a'.repeat(1 << 20) + const base64 = Buffer.from(data).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 str = Buffer.from(buf).toString('ascii') + t.same( + new Uint8Array(str), + new Uint8Array(data) + ) + } catch (e) { + t.fail(`failed to fetch ${dataURL}`) + } + }) + t.end() })