Skip to content

Commit

Permalink
fix: fetch a long base64 url will crash and nothing happens (close: #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaoboy committed Jul 25, 2022
1 parent 2944587 commit 221a309
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/fetch/dataURL.js
Expand Up @@ -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
Expand Down
21 changes: 20 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,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()
})

0 comments on commit 221a309

Please sign in to comment.