Skip to content

Commit

Permalink
fix(#3937): respect correct host header
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Dec 10, 2024
1 parent 290b24d commit d7ec3b6
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', { only: true }, async t => {

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / test (22, macos-latest) / Test with Node.js 22 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / test (23, macos-latest) / Test with Node.js 23 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / test (23, ubuntu-latest) / Test with Node.js 23 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 23 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / test (22, ubuntu-latest) / Test with Node.js 22 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 20 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 1735 in test/interceptors/dns.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 22 compiled --without-intl

'only' and 'runOnly' require the --test-only command-line option.
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 d7ec3b6

Please sign in to comment.