Skip to content

fix: preserve DNS origin hostname on sockets#5577

Open
cyphercodes wants to merge 4 commits into
nodejs:mainfrom
cyphercodes:fix/dns-original-hostname-5573
Open

fix: preserve DNS origin hostname on sockets#5577
cyphercodes wants to merge 4 commits into
nodejs:mainfrom
cyphercodes:fix/dns-original-hostname-5573

Conversation

@cyphercodes

Copy link
Copy Markdown

This relates to...

Fixes #5573

Rationale

The DNS interceptor connects to a selected IP address, which causes Node's net.client.socket diagnostics channel to expose the IP as socket._host. Consumers can no longer correlate TCP connection timing with the original request hostname.

Changes

  • pass the request hostname through HTTP connection setup
  • connect to the interceptor-selected IP through a fixed lookup while preserving the logical hostname on the socket
  • add regression coverage for socket._host and remoteAddress

Features

N/A

Bug Fixes

  • preserve the original DNS hostname in HTTP socket diagnostics when the DNS interceptor selects an IP

Breaking Changes and Deprecations

N/A

Status

Testing:

  • node --test --test-name-pattern='#5573' test/interceptors/dns.js
  • npm run test:interceptors
  • npm run lint
  • npm run test:typescript

Signed-off-by: cyphercodes <cyphercodes@users.noreply.github.com>
@codecov-commenter

codecov-commenter commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.48%. Comparing base (f36ea9c) to head (4de0540).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5577   +/-   ##
=======================================
  Coverage   93.47%   93.48%           
=======================================
  Files         110      110           
  Lines       37564    37610   +46     
=======================================
+ Hits        35114    35159   +45     
- Misses       2450     2451    +1     

☔ View full report in Codecov by Harness.
📢 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.

Signed-off-by: cyphercodes <cyphercodes@users.noreply.github.com>
@cyphercodes

Copy link
Copy Markdown
Author

Pushed a small test-only follow-up in 306602e to avoid relying on the net.client.socket diagnostics channel in the new DNS socket regression. The test now captures the socket/options through net.connect, which should avoid the Windows timeout while preserving the same assertions.

Verification run locally:

  • node --test test/interceptors/dns.js
  • npm run lint -- test/interceptors/dns.js
  • git diff --check

@cyphercodes

Copy link
Copy Markdown
Author

Pushed a Windows-focused test follow-up in 67808e2. The HTTP DNS regression no longer asserts socket.remoteAddress, which is empty on the Windows runner after the request completes; it now verifies the preserved hostname and the injected lookup result instead.

Verification run locally:

  • node --test --test-name-pattern "#5573" test/interceptors/dns.js
  • npm run lint -- test/interceptors/dns.js
  • git diff --check

Note: a full local node --test test/interceptors/dns.js run hit the existing TLS/SNI local-environment failure in Should respect DNS origin hostname for SNI on TLS, while the targeted #5573 regression passed.

Comment thread lib/dispatcher/client.js Outdated
if (!client[kHTTPContext]) {
if (client[kUrl].protocol === 'http:') {
client[kServerName] = request.servername
}

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.

what about https?

@cyphercodes

Copy link
Copy Markdown
Author

Pushed a small follow-up in 4de0540 for the HTTPS review note. The client now stores request.servername before opening a new connection for all protocols; HTTPS keeps the existing SNI/change handling, while HTTP still passes the preserved hostname through the connector lookup path.

Verification run locally:

  • node --test --test-name-pattern "#5573" test/interceptors/dns.js
  • npm run lint -- test/interceptors/dns.js lib/dispatcher/client.js
  • git diff --check

@mcollina mcollina left a comment

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.

lgtm

Comment thread lib/core/connect.js
const family = net.isIP(hostname)
if (family !== 0 && servername && servername !== hostname) {
connectOptions.host = servername
connectOptions.lookup = (_hostname, lookupOptions, cb) => {

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.

The _hostname here could use a comment. At first glance, the _hostname here and the address: hostname below look like a typo/mistake. A comment indicating that the _hostname is intentionally being ignored is helpful.

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.

Original hostname is lost for HTTP connections when using the DNS interceptor

5 participants