Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle data URL with hashes. #2475

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions lib/fetch/dataURL.js
Expand Up @@ -119,17 +119,14 @@ function dataURLProcessor (dataURL) {
* @param {boolean} excludeFragment
*/
function URLSerializer (url, excludeFragment = false) {
const href = url.href

if (!excludeFragment) {
return href
return url.href
}

const hash = href.lastIndexOf('#')
if (hash === -1) {
return href
}
return href.slice(0, hash)
const href = url.href
const hashLength = url.hash.length

return hashLength === 0 ? href : href.substring(0, href.length - hashLength)
}

// https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
Expand Down
21 changes: 21 additions & 0 deletions test/fetch/data-uri.js
Expand Up @@ -191,3 +191,24 @@ test('https://domain.com/?', (t) => {
const serialized = URLSerializer(new URL(domain))
t.equal(serialized, domain)
})

// https://github.com/nodejs/undici/issues/2474
test('hash url', (t) => {
t.plan(1)
const domain = 'https://domain.com/#a#b'
const url = new URL(domain)
const serialized = URLSerializer(url, true)
t.equal(serialized, url.href.substring(0, url.href.length - url.hash.length))
})

// https://github.com/nodejs/undici/issues/2474
test('data url that includes the hash', async (t) => {
ronag marked this conversation as resolved.
Show resolved Hide resolved
t.plan(1)
const dataURL = 'data:,node#js#'
try {
const res = await fetch(dataURL)
t.equal(await res.text(), 'node')
} catch (error) {
t.fail(`failed to fetch ${dataURL}`)
}
})