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(fetch): ByteString checks & conversion in Headers #1560

Merged
merged 1 commit into from Jul 19, 2022
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
4 changes: 2 additions & 2 deletions lib/fetch/index.js
Expand Up @@ -1946,8 +1946,8 @@ async function httpNetworkFetch (

const headers = new Headers()
for (let n = 0; n < headersList.length; n += 2) {
const key = headersList[n + 0].toString()
const val = headersList[n + 1].toString()
const key = headersList[n + 0].toString('latin1')
const val = headersList[n + 1].toString('latin1')

if (key.toLowerCase() === 'content-encoding') {
codings = val.split(',').map((x) => x.trim())
Expand Down
5 changes: 3 additions & 2 deletions lib/fetch/webidl.js
Expand Up @@ -388,8 +388,9 @@ webidl.converters.DOMString = function (V, opts = {}) {
return String(V)
}

// Check for 0 or more characters outside of the latin1 range.
// eslint-disable-next-line no-control-regex
const isNotLatin1 = /[^\u0000-\u00ff]/
const isLatin1 = /^[\u0000-\u00ff]{0,}$/

// https://webidl.spec.whatwg.org/#es-ByteString
webidl.converters.ByteString = function (V) {
Expand All @@ -399,7 +400,7 @@ webidl.converters.ByteString = function (V) {

// 2. If the value of any element of x is greater than
// 255, then throw a TypeError.
if (isNotLatin1.test(x)) {
if (!isLatin1.test(x)) {
throw new TypeError('Argument is not a ByteString')
}

Expand Down
32 changes: 32 additions & 0 deletions test/fetch/client-fetch.js
Expand Up @@ -468,3 +468,35 @@ test('do not decode redirect body', (t) => {
t.strictSame(JSON.stringify(obj), await body.text())
})
})

test('Receiving non-Latin1 headers', async (t) => {
const ContentDisposition = [
'inline; filename=rock&roll.png',
'inline; filename="rock\'n\'roll.png"',
'inline; filename="image â\x80\x94 copy (1).png"; filename*=UTF-8\'\'image%20%E2%80%94%20copy%20(1).png',
'inline; filename="_å\x9C\x96ç\x89\x87_ð\x9F\x96¼_image_.png"; filename*=UTF-8\'\'_%E5%9C%96%E7%89%87_%F0%9F%96%BC_image_.png',
'inline; filename="100 % loading&perf.png"; filename*=UTF-8\'\'100%20%25%20loading%26perf.png'
]

const server = createServer((req, res) => {
for (let i = 0; i < ContentDisposition.length; i++) {
res.setHeader(`Content-Disposition-${i + 1}`, ContentDisposition[i])
}

res.end()
}).listen(0)

t.teardown(server.close.bind(server))
await once(server, 'listening')

const url = `http://localhost:${server.address().port}`
const response = await fetch(url, { method: 'HEAD' })
const cdHeaders = [...response.headers]
.filter(([k]) => k.startsWith('content-disposition'))
.map(([, v]) => v)
const lengths = cdHeaders.map(h => h.length)

t.same(cdHeaders, ContentDisposition)
t.same(lengths, [30, 34, 94, 104, 90])
t.end()
})
8 changes: 8 additions & 0 deletions test/webidl/converters.js
Expand Up @@ -183,3 +183,11 @@ test('BufferSource', (t) => {

t.end()
})

test('ByteString', (t) => {
t.doesNotThrow(() => {
webidl.converters.ByteString('')
})

t.end()
})