Skip to content

fix(socks5): enforce authenticated state before CONNECT#5097

Merged
mcollina merged 1 commit intonodejs:mainfrom
trivikr:socks5-client-connect
Apr 24, 2026
Merged

fix(socks5): enforce authenticated state before CONNECT#5097
mcollina merged 1 commit intonodejs:mainfrom
trivikr:socks5-client-connect

Conversation

@trivikr
Copy link
Copy Markdown
Member

@trivikr trivikr commented Apr 24, 2026

This relates to...

Fixes: #5096

Rationale

Socks5Client emitted 'authenticated' on successful handshake/authentication but never transitioned into an authenticated state. As a result, connect() only rejected the already-connected state and could write a SOCKS CONNECT frame from invalid states such as initial, handshaking, authenticating, closed, or error.

This also affected Socks5ProxyAgent, which checks socks5Client.state === 'authenticated' in the NO_AUTH flow. Since that state was never set, the agent could miss the already-emitted 'authenticated' event and wait until the authentication timeout.

Changes

  • add an explicit AUTHENTICATED state to Socks5Client
  • transition to AUTHENTICATED before emitting 'authenticated' in both auth-success paths
  • reject Socks5Client.connect() unless the client is authenticated
  • update Socks5ProxyAgent to check STATES.AUTHENTICATED

Features

N/A

Bug Fixes

  • prevent Socks5Client.connect() from sending CONNECT before authentication completes
  • prevent Socks5Client.connect() from sending CONNECT after invalid lifecycle states
  • fix Socks5ProxyAgent’s NO_AUTH authentication readiness check to use the actual authenticated state

Breaking Changes and Deprecations

This changes the internal Socks5Client.state value after successful authentication from the previous implicit/incorrect behavior to an explicit authenticated state. Socks5Client is not part of the public top-level undici API, so there is no intended public API break.

Status

Assisted-by: openai:gpt-5.4

Assisted-by: openai:gpt-5.4
Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.13%. Comparing base (2a6f9c7) to head (0c2bf29).

Files with missing lines Patch % Lines
lib/core/socks5-client.js 85.71% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5097   +/-   ##
=======================================
  Coverage   93.13%   93.13%           
=======================================
  Files         110      110           
  Lines       35816    35826   +10     
=======================================
+ Hits        33356    33368   +12     
+ Misses       2460     2458    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 1dec881 into nodejs:main Apr 24, 2026
35 checks passed
@trivikr trivikr deleted the socks5-client-connect branch April 24, 2026 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Socks5Client.connect() can send CONNECT before authentication completes and after close/error

3 participants