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 3 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
11 changes: 6 additions & 5 deletions lib/fetch/dataURL.js
Expand Up @@ -125,11 +125,12 @@ function URLSerializer (url, excludeFragment = false) {
return href
}

const hash = href.lastIndexOf('#')
if (hash === -1) {
return href
}
return href.slice(0, hash)
// Note: Since there are at least two characters in the url,
// the scheme and the U+003A (:) character,
// the hash searches after these.
const hash = href.indexOf('#', 2)

return hash === -1 ? href : href.substring(0, hash)
tsctx marked this conversation as resolved.
Show resolved Hide resolved
}

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

// 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(2)
const dataURL = 'data:,node#js#'
const serialized = URLSerializer(new URL(dataURL), true)
t.equal('data:,node', serialized)
try {
const res = await fetch(dataURL)
t.equal(await res.text(), 'node')
} catch (error) {
t.fail(`failed to fetch ${dataURL}`)
}
})