Skip to content

Commit

Permalink
disallow setting host header in fetch (#2322)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Oct 9, 2023
1 parent e5c9d70 commit 470ee38
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 470ee38

Please sign in to comment.