Skip to content

fix: stop buffering data after SOCKS5 connect#5118

Merged
mcollina merged 3 commits intonodejs:mainfrom
trivikr:socks5-tunnel-leak-buffer
Apr 27, 2026
Merged

fix: stop buffering data after SOCKS5 connect#5118
mcollina merged 3 commits intonodejs:mainfrom
trivikr:socks5-tunnel-leak-buffer

Conversation

@trivikr
Copy link
Copy Markdown
Member

@trivikr trivikr commented Apr 25, 2026

This relates to...

Fixes: #5117

Rationale

After a SOCKS5 CONNECT succeeds, the socket carries tunneled HTTP/TLS data and should no longer be parsed by Socks5Client. The existing permanent data listener kept appending tunneled response chunks to the internal protocol buffer, causing duplicate retained memory for the lifetime of the socket.

Changes

  • Store the SOCKS5 socket data handler so it can be removed after CONNECT succeeds.
  • Clear the SOCKS5 protocol buffer once the CONNECT response is parsed.
  • Preserve any tunneled bytes that arrive in the same socket read as the CONNECT response by pushing them back with socket.unshift().

Features

N/A

Bug Fixes

Stop buffering data after SOCKS5 connect

Breaking Changes and Deprecations

N/A

Status

Assisted-by: openai:gpt-5.5

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

codecov-commenter commented Apr 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.14%. Comparing base (efae3da) to head (f8a876f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5118      +/-   ##
==========================================
- Coverage   93.14%   93.14%   -0.01%     
==========================================
  Files         110      110              
  Lines       35908    36109     +201     
==========================================
+ Hits        33446    33633     +187     
- Misses       2462     2476      +14     

☔ 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.

Comment thread lib/core/socks5-client.js Outdated
@trivikr trivikr requested a review from mcollina April 26, 2026 02:39
Comment thread lib/core/socks5-client.js Outdated
const boundPort = this.buffer.readUInt16BE(offset)

this.buffer = this.buffer.subarray(responseLength)
this.buffer = Buffer.alloc(0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this need to be reallocated? We could just keep one around, they are just empty.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The SOCKS5 client removes its data listener, but the client object can still be retained by the socket’s error/close listeners. If this.buffer is not cleared, it can retain the SOCKS5 response buffer.

We don't need fresh allocation every time, as it's replaced using Buffer.concat(...)
I replaced this with EMPTY_BUFFER constant in f8a876f

@trivikr trivikr requested a review from mcollina April 26, 2026 16:10
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 74e8706 into nodejs:main Apr 27, 2026
35 checks passed
mcollina pushed a commit that referenced this pull request Apr 29, 2026
@trivikr trivikr deleted the socks5-tunnel-leak-buffer branch May 1, 2026 00:29
@github-actions github-actions Bot mentioned this pull request May 1, 2026
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.

SOCKS5 tunnel leaks tunneled response data into internal buffer

3 participants