Skip to content

Commit

Permalink
fix(#3937): respect correct host header (#3940)
Browse files Browse the repository at this point in the history
* fix(#3937): respect correct host header

* Update test/interceptors/dns.js

Co-authored-by: Matteo Collina <[email protected]>

---------

Co-authored-by: Matteo Collina <[email protected]>
  • Loading branch information
metcoder95 and mcollina authored Dec 10, 2024
1 parent 290b24d commit 81416fc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/interceptor/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ module.exports = interceptorOpts => {
servername: origin.hostname, // For SNI on TLS
origin: newOrigin,
headers: {
host: origin.hostname,
host: origin.host,
...origDispatchOpts.headers
}
}
Expand Down
85 changes: 84 additions & 1 deletion test/interceptors/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ test('Should respect DNS origin hostname for SNI on TLS', async t => {
}

server.on('request', (req, res) => {
t.equal(req.headers.host, 'localhost')
t.equal(req.headers.host, `localhost:${server.address().port}`)
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('hello world!')
})
Expand Down Expand Up @@ -1731,3 +1731,86 @@ test('Should handle max cached items', async t => {
t.equal(response3.statusCode, 200)
t.equal(await response3.body.text(), 'hello world! (x2)')
})

test('#3937 - Handle host correctly', async t => {
t = tspl(t, { plan: 10 })

const hostsnames = []
const server = createServer()
const requestOptions = {
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
}

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

res.writeHead(200, { 'content-type': 'text/plain' })
res.end('hello world!')
})

server.listen(0)

await once(server, 'listening')

const client = new Agent().compose([
dispatch => {
return (opts, handler) => {
const url = new URL(opts.origin)

t.equal(hostsnames.includes(url.hostname), false)

if (url.hostname[0] === '[') {
// [::1] -> ::1
t.equal(isIP(url.hostname.slice(1, 4)), 6)
} else {
t.equal(isIP(url.hostname), 4)
}

hostsnames.push(url.hostname)

return dispatch(opts, handler)
}
},
dns({
lookup: (_origin, _opts, cb) => {
cb(null, [
{
address: '::1',
family: 6
},
{
address: '127.0.0.1',
family: 4
}
])
}
})
])

after(async () => {
await client.close()
server.close()

await once(server, 'close')
})

const response = await client.request({
...requestOptions,
origin: `http://localhost:${server.address().port}`
})

t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

const response2 = await client.request({
...requestOptions,
origin: `http://localhost:${server.address().port}`
})

t.equal(response2.statusCode, 200)
t.equal(await response2.body.text(), 'hello world!')
})

0 comments on commit 81416fc

Please sign in to comment.