Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 51 additions & 29 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,38 @@ function connect (client) {

client[kConnecting] = true

// The name this attempt verifies the server's certificate for. While it
// connects, its requests can be aborted and _resume() can switch
// client[kServerName] to the name of the next queued request.
const servername = client[kServerName]

let calledBack = false
let cancelled = false

// Ends this attempt without a 'connectionError': no request failed, and a
// Pool would retire this client.
const abandon = (socket, err) => {
client[kConnecting] = false

if (channels.connectError.hasSubscribers) {
channels.connectError.publish({
connectParams: {
host,
hostname,
protocol,
port,
version: client[kHTTPContext]?.version,
servername,
localAddress: client[kLocalAddress]
},
connector: client[kConnector],
error: err
})
}

util.destroy(socket.on('error', noop), err)
}

if (channels.beforeConnect.hasSubscribers) {
channels.beforeConnect.publish({
connectParams: {
Expand All @@ -590,7 +619,7 @@ function connect (client) {
protocol,
port,
version: client[kHTTPContext]?.version,
servername: client[kServerName],
servername,
localAddress: client[kLocalAddress]
},
connector: client[kConnector]
Expand All @@ -603,7 +632,7 @@ function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName],
servername,
localAddress: client[kLocalAddress]
}, (err, socket) => {
calledBack = true
Expand All @@ -618,7 +647,7 @@ function connect (client) {
client[kCancelConnect] = null

if (err) {
handleConnectError(client, err, { host, hostname, protocol, port })
handleConnectError(client, err, { host, hostname, protocol, port, servername })
client[kResume]()
return
}
Expand All @@ -631,13 +660,21 @@ function connect (client) {

assert(socket)

if (client[kServerName] !== servername) {
// The server proved its identity for another name than the one the
// queued requests are for: never send them on this connection.
abandon(socket, new InformationalError('servername changed'))
client[kResume]()
return
}

try {
client[kHTTPContext] = socket.alpnProtocol === 'h2'
? connectH2(client, socket)
: connectH1(client, socket)
} catch (err) {
socket.destroy().on('error', noop)
handleConnectError(client, err, { host, hostname, protocol, port })
handleConnectError(client, err, { host, hostname, protocol, port, servername })
client[kResume]()
return
}
Expand All @@ -657,7 +694,7 @@ function connect (client) {
protocol,
port,
version: client[kHTTPContext]?.version,
servername: client[kServerName],
servername,
localAddress: client[kLocalAddress]
},
connector: client[kConnector],
Expand All @@ -675,35 +712,16 @@ function connect (client) {
client[kCancelConnect] = (err) => {
cancelled = true
client[kCancelConnect] = null
client[kConnecting] = false

// Not a 'connectionError': that would make a Pool retire this client.
if (channels.connectError.hasSubscribers) {
channels.connectError.publish({
connectParams: {
host,
hostname,
protocol,
port,
version: client[kHTTPContext]?.version,
servername: client[kServerName],
localAddress: client[kLocalAddress]
},
connector: client[kConnector],
error: err
})
}

util.destroy(connecting.on('error', noop), err)
abandon(connecting, err)
}
}
} catch (err) {
handleConnectError(client, err, { host, hostname, protocol, port })
handleConnectError(client, err, { host, hostname, protocol, port, servername })
client[kResume]()
}
}

function handleConnectError (client, err, { host, hostname, protocol, port }) {
function handleConnectError (client, err, { host, hostname, protocol, port, servername }) {
if (client.destroyed) {
return
}
Expand All @@ -718,7 +736,7 @@ function handleConnectError (client, err, { host, hostname, protocol, port }) {
protocol,
port,
version: client[kHTTPContext]?.version,
servername: client[kServerName],
servername,
localAddress: client[kLocalAddress]
},
connector: client[kConnector],
Expand All @@ -734,7 +752,8 @@ function handleConnectError (client, err, { host, hostname, protocol, port }) {
util.errorRequest(client, running[i], err)
}

while (client[kPending] > 0 && client[kQueue][client[kPendingIdx]].servername === client[kServerName]) {
// Only the requests for the name the certificate failed for.
while (client[kPending] > 0 && client[kQueue][client[kPendingIdx]].servername === servername) {
const request = client[kQueue].splice(client[kPendingIdx], 1)[0]
util.errorRequest(client, request, err)
}
Expand Down Expand Up @@ -825,6 +844,9 @@ function _resume (client, sync) {
client[kHTTPContext] = null
resume(client)
})
// A pending attempt verifies the server for the previous name, and its
// connection is discarded once it connects: do not wait for it.
client[kCancelConnect]?.(new InformationalError('servername changed'))
}

if (client[kConnecting]) {
Expand Down
195 changes: 195 additions & 0 deletions test/servername-switch-while-connecting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
'use strict'

// A Client serves requests for several TLS server names, one at a time. A
// connection attempt verifies the server for one name. If its requests are
// aborted while it connects and the next queued request is for another name,
// that request must not be sent on this connection: the server never proved
// its identity for that name.

const { test } = require('node:test')
const assert = require('node:assert')
const { once } = require('node:events')
const net = require('node:net')
const https = require('node:https')
const http2 = require('node:http2')
const pem = require('@metcoder95/https-pem')
const { Client, Agent, interceptors, buildConnector } = require('..')

// Fails the test instead of hanging it when the promise does not settle.
function settlesWithin (promise, ms = 2000) {
let timer
return Promise.race([
promise,
new Promise((resolve, reject) => {
timer = setTimeout(() => reject(new Error(`still pending after ${ms}ms`)), ms)
})
]).finally(() => clearTimeout(timer))
}

// The certificate is trusted, but valid only for a.test.
function onlyATest (host) {
if (host !== 'a.test') {
return Object.assign(new Error(`Hostname/IP does not match certificate's altnames: Host: ${host}`), {
code: 'ERR_TLS_CERT_ALTNAME_INVALID'
})
}
}

// The certificate is trusted for any name.
function anyName () {}

// A TLS server behind a TCP front. The front holds the first connection, so
// its TLS handshake is pending when the request is aborted: it never forwards
// it, or forwards it after `delayFirst` ms. Later connections are forwarded at
// once. The server records the Host and the TLS server name of each request.
async function startServer (t, { h2 = false, delayFirst = null } = {}) {
const served = []
const handler = (req, res) => {
const host = (req.headers[':authority'] ?? req.headers.host).split(':')[0]
served.push({ host, servername: req.socket.servername })
res.end(host)
}
const server = h2
? http2.createSecureServer({ ...pem, allowHTTP1: true }, handler)
: https.createServer(pem, handler)
server.listen(0, '127.0.0.1')
await once(server, 'listening')

const sockets = []
let first = null
let timer = null
const front = net.createServer((socket) => {
sockets.push(socket)
const forward = () => {
if (socket.destroyed) {
return
}
const upstream = net.connect(server.address().port, '127.0.0.1')
sockets.push(upstream)
socket.pipe(upstream).pipe(socket)
socket.on('error', () => upstream.destroy())
upstream.on('error', () => socket.destroy())
}

if (first !== null) {
forward()
return
}

first = socket
if (delayFirst !== null) {
timer = setTimeout(forward, delayFirst)
} else {
socket.resume()
}
})
front.listen(0, '127.0.0.1')
await once(front, 'listening')

t.after(() => {
clearTimeout(timer)
for (const socket of sockets) socket.destroy()
front.close()
server.close()
})

return { port: front.address().port, served }
}

// Starts a request for a.test, queues one for b.test, then aborts the first
// while its connection attempt is pending.
async function abortFirstWhileConnecting (dispatch) {
const ac = new AbortController()
const a = dispatch('a.test', ac.signal)
const b = dispatch('b.test')
await new Promise(resolve => setTimeout(resolve, 50))
ac.abort()
await assert.rejects(a, { name: 'AbortError' })
return b
}

function clientDispatch (client) {
return (host, signal) => client.request({
path: '/',
method: 'GET',
servername: host,
headers: { host },
signal
})
}

for (const h2 of [false, true]) {
const name = h2 ? 'h2' : 'h1'

test(`${name}: a request is not sent on a connection verified for another servername`, async (t) => {
const { port, served } = await startServer(t, { h2, delayFirst: 200 })
const client = new Client(`https://127.0.0.1:${port}`, {
allowH2: h2,
connect: { ca: pem.cert, checkServerIdentity: onlyATest }
})
t.after(() => client.destroy())

const b = abortFirstWhileConnecting(clientDispatch(client))

await assert.rejects(settlesWithin(b), { code: 'ERR_TLS_CERT_ALTNAME_INVALID' })
assert.deepStrictEqual(served, [])
})

test(`${name}: the next request connects with its own servername without waiting for the abandoned attempt`, async (t) => {
// The first connection never completes its handshake.
const { port, served } = await startServer(t, { h2 })
const client = new Client(`https://127.0.0.1:${port}`, {
allowH2: h2,
connect: { ca: pem.cert, checkServerIdentity: anyName }
})
t.after(() => client.destroy())

const b = abortFirstWhileConnecting(clientDispatch(client))

const res = await settlesWithin(b)
assert.strictEqual(await res.body.text(), 'b.test')
assert.deepStrictEqual(served, [{ host: 'b.test', servername: 'b.test' }])
})
}

test('Agent with the dns interceptor: a request is not sent on a connection verified for another host', async (t) => {
const { port, served } = await startServer(t, { delayFirst: 200 })
const agent = new Agent({
connections: 1,
connect: { ca: pem.cert, checkServerIdentity: onlyATest }
}).compose(interceptors.dns({
lookup: (_origin, _opts, cb) => cb(null, [{ address: '127.0.0.1', family: 4, ttl: 10000 }])
}))
t.after(() => agent.destroy())

const b = abortFirstWhileConnecting((host, signal) => agent.request({
origin: `https://${host}:${port}`,
path: '/',
method: 'GET',
signal
}))

await assert.rejects(settlesWithin(b), { code: 'ERR_TLS_CERT_ALTNAME_INVALID' })
assert.deepStrictEqual(served, [])
})

test('a connection that cannot be cancelled is discarded once it connects for another servername', async (t) => {
const { port, served } = await startServer(t, { delayFirst: 200 })
const connector = buildConnector({ ca: pem.cert, checkServerIdentity: anyName })
const servernames = []
const client = new Client(`https://127.0.0.1:${port}`, {
// Does not return the socket, so the Client cannot cancel the attempt.
connect (opts, cb) {
servernames.push(opts.servername)
connector(opts, cb)
}
})
t.after(() => client.destroy())

const b = abortFirstWhileConnecting(clientDispatch(client))

const res = await settlesWithin(b)
assert.strictEqual(await res.body.text(), 'b.test')
assert.deepStrictEqual(servernames, ['a.test', 'b.test'])
assert.deepStrictEqual(served, [{ host: 'b.test', servername: 'b.test' }])
})
Loading