Skip to content
Open
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
2 changes: 2 additions & 0 deletions lib/dispatcher/socks5-proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Socks5ProxyAgent extends DispatcherBase {
const authenticationReady = Promise.withResolvers()

const authenticationTimeout = setTimeout(() => {
socks5Client.destroy()
authenticationReady.reject(new Error('SOCKS5 authentication timeout'))
}, 5000)

Expand Down Expand Up @@ -148,6 +149,7 @@ class Socks5ProxyAgent extends DispatcherBase {
const connectionReady = Promise.withResolvers()

const connectionTimeout = setTimeout(() => {
socks5Client.destroy()
connectionReady.reject(new Error('SOCKS5 connection timeout'))
}, 5000)

Expand Down
33 changes: 33 additions & 0 deletions test/socks5-proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,39 @@ test('Socks5ProxyAgent - proxy connection refused', async (t) => {
await p.completed
})

test('Socks5ProxyAgent - SOCKS5 negotiation timeout destroys the underlying socket (#5704)', async (t) => {
const p = tspl(t, { plan: 2 })

// Proxy that accepts the TCP connection and reads the SOCKS5 greeting but
// never replies, stalling negotiation so the auth timeout fires.
const sockets = []
let resolveClosed
const proxySocketClosed = new Promise((resolve) => { resolveClosed = resolve })
const proxy = net.createServer((socket) => {
sockets.push(socket)
socket.on('data', () => {}) // drain the greeting, never reply
socket.once('close', () => resolveClosed())
})

await new Promise(resolve => proxy.listen(0, '127.0.0.1', resolve))

const agent = new Socks5ProxyAgent(`socks5://127.0.0.1:${proxy.address().port}`)

await request('http://example.invalid/', { dispatcher: agent }).catch((err) => {
p.equal(err.message, 'SOCKS5 authentication timeout', 'request should reject with authentication timeout')
})

await agent.close()

// The socket opened for the stalled negotiation must have been destroyed by
// the timeout path (previously it leaked untracked for the process lifetime).
await proxySocketClosed
p.equal(sockets[0].destroyed, true, 'proxy-side socket should be destroyed after negotiation timeout')

proxy.close()
await p.completed
})

test('Socks5ProxyAgent - close and destroy', async (t) => {
const p = tspl(t, { plan: 2 })

Expand Down
Loading