Skip to content

Commit 81416fc

Browse files
metcoder95mcollina
andauthored
fix(#3937): respect correct host header (#3940)
* fix(#3937): respect correct host header * Update test/interceptors/dns.js Co-authored-by: Matteo Collina <hello@matteocollina.com> --------- Co-authored-by: Matteo Collina <hello@matteocollina.com>
1 parent 290b24d commit 81416fc

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

lib/interceptor/dns.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ module.exports = interceptorOpts => {
358358
servername: origin.hostname, // For SNI on TLS
359359
origin: newOrigin,
360360
headers: {
361-
host: origin.hostname,
361+
host: origin.host,
362362
...origDispatchOpts.headers
363363
}
364364
}

test/interceptors/dns.js

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ test('Should respect DNS origin hostname for SNI on TLS', async t => {
124124
}
125125

126126
server.on('request', (req, res) => {
127-
t.equal(req.headers.host, 'localhost')
127+
t.equal(req.headers.host, `localhost:${server.address().port}`)
128128
res.writeHead(200, { 'content-type': 'text/plain' })
129129
res.end('hello world!')
130130
})
@@ -1731,3 +1731,86 @@ test('Should handle max cached items', async t => {
17311731
t.equal(response3.statusCode, 200)
17321732
t.equal(await response3.body.text(), 'hello world! (x2)')
17331733
})
1734+
1735+
test('#3937 - Handle host correctly', async t => {
1736+
t = tspl(t, { plan: 10 })
1737+
1738+
const hostsnames = []
1739+
const server = createServer()
1740+
const requestOptions = {
1741+
method: 'GET',
1742+
path: '/',
1743+
headers: {
1744+
'content-type': 'application/json'
1745+
}
1746+
}
1747+
1748+
server.on('request', (req, res) => {
1749+
t.equal(req.headers.host, `localhost:${server.address().port}`)
1750+
1751+
res.writeHead(200, { 'content-type': 'text/plain' })
1752+
res.end('hello world!')
1753+
})
1754+
1755+
server.listen(0)
1756+
1757+
await once(server, 'listening')
1758+
1759+
const client = new Agent().compose([
1760+
dispatch => {
1761+
return (opts, handler) => {
1762+
const url = new URL(opts.origin)
1763+
1764+
t.equal(hostsnames.includes(url.hostname), false)
1765+
1766+
if (url.hostname[0] === '[') {
1767+
// [::1] -> ::1
1768+
t.equal(isIP(url.hostname.slice(1, 4)), 6)
1769+
} else {
1770+
t.equal(isIP(url.hostname), 4)
1771+
}
1772+
1773+
hostsnames.push(url.hostname)
1774+
1775+
return dispatch(opts, handler)
1776+
}
1777+
},
1778+
dns({
1779+
lookup: (_origin, _opts, cb) => {
1780+
cb(null, [
1781+
{
1782+
address: '::1',
1783+
family: 6
1784+
},
1785+
{
1786+
address: '127.0.0.1',
1787+
family: 4
1788+
}
1789+
])
1790+
}
1791+
})
1792+
])
1793+
1794+
after(async () => {
1795+
await client.close()
1796+
server.close()
1797+
1798+
await once(server, 'close')
1799+
})
1800+
1801+
const response = await client.request({
1802+
...requestOptions,
1803+
origin: `http://localhost:${server.address().port}`
1804+
})
1805+
1806+
t.equal(response.statusCode, 200)
1807+
t.equal(await response.body.text(), 'hello world!')
1808+
1809+
const response2 = await client.request({
1810+
...requestOptions,
1811+
origin: `http://localhost:${server.address().port}`
1812+
})
1813+
1814+
t.equal(response2.statusCode, 200)
1815+
t.equal(await response2.body.text(), 'hello world!')
1816+
})

0 commit comments

Comments
 (0)