Skip to content

Commit

Permalink
disallow setting host header in fetch (nodejs#2322)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent ee773b7 commit 69619ae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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'
}
})
})

0 comments on commit 69619ae

Please sign in to comment.