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

disallow setting host header in fetch #2322

Merged
merged 1 commit into from
Oct 9, 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
2 changes: 2 additions & 0 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,8 @@ async function httpNetworkOrCacheFetch (
}
}

httpRequest.headersList.delete('host')

// 20. If includeCredentials is true, then:
if (includeCredentials) {
// 1. If the user agent is not configured to block cookies for httpRequest
Expand Down
25 changes: 25 additions & 0 deletions test/fetch/issue-2318.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const { test } = require('tap')
const { once } = require('events')
const { createServer } = require('http')
const { fetch } = require('../..')

test('Undici overrides user-provided `Host` header', async (t) => {
t.plan(1)

const server = createServer((req, res) => {
t.equal(req.headers.host, `localhost:${server.address().port}`)

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

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

await fetch(`http://localhost:${server.address().port}`, {
headers: {
host: 'www.idk.org'
}
})
})