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

Properly parse set-cookie header using http2 #2886

Merged
merged 2 commits into from
Feb 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2145,7 +2145,15 @@ async function httpNetworkFetch (
const keys = Object.keys(rawHeaders)
for (let i = 0; i < keys.length; ++i) {
// The header names are already in lowercase.
headersList.append(keys[i], rawHeaders[keys[i]], true)
const key = keys[i]
const value = rawHeaders[key]
if (key === 'set-cookie') {
for (let j = 0; j < value.length; ++j) {
headersList.append(key, value[j], true)
}
} else {
headersList.append(key, value, true)
}
}
// For H2, The header names are already in lowercase,
// so we can avoid the `HeadersList#get` call here.
Expand Down
45 changes: 44 additions & 1 deletion test/fetch/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ const { createServer } = require('node:http')
const { test } = require('node:test')
const assert = require('node:assert')
const { tspl } = require('@matteo.collina/tspl')
const { fetch, Headers } = require('../..')
const { Client, fetch, Headers } = require('../..')
const { closeServerAsPromise } = require('../utils/node-http')
const pem = require('https-pem')
const { createSecureServer } = require('node:http2')
const { closeClientAndServerAsPromise } = require('../utils/node-http')

test('Can receive set-cookie headers from a server using fetch - issue #1262', async (t) => {
const server = createServer((req, res) => {
Expand Down Expand Up @@ -66,3 +69,43 @@ test('Cookie header is delimited with a semicolon rather than a comma - issue #1
]
})
})

test('Can receive set-cookie headers from a http2 server using fetch - issue #2885', async (t) => {
const server = createSecureServer(pem)
server.on('stream', async (stream, headers) => {
stream.respond({
'content-type': 'text/plain; charset=utf-8',
'x-method': headers[':method'],
'set-cookie': 'Space=Cat; Secure; HttpOnly',
':status': 200
})

stream.end('test')
})

server.listen()
await once(server, 'listening')

const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
},
allowH2: true
})

const response = await fetch(
`https://localhost:${server.address().port}/`,
// Needs to be passed to disable the reject unauthorized
{
method: 'GET',
dispatcher: client,
headers: {
'content-type': 'text-plain'
}
}
)

t.after(closeClientAndServerAsPromise(client, server))

assert.deepStrictEqual(response.headers.getSetCookie(), ['Space=Cat; Secure; HttpOnly'])
})
Loading