Skip to content

fix: preserve original hostname on HTTP socket when using DNS interceptor#5581

Closed
mcollina wants to merge 1 commit into
mainfrom
fix/issue-5573-preserve-original-hostname-dns-interceptor
Closed

fix: preserve original hostname on HTTP socket when using DNS interceptor#5581
mcollina wants to merge 1 commit into
mainfrom
fix/issue-5573-preserve-original-hostname-dns-interceptor

Conversation

@mcollina

Copy link
Copy Markdown
Member

Fixes

#5573

Problem

When the DNS interceptor resolves a hostname and replaces the connection origin with the resolved IP address, the original hostname is lost for HTTP connections. For HTTPS, the hostname remains accessible through TLS SNI (servername), but for plain HTTP it is nowhere available on the socket.

This breaks diagnostics channel instrumentation that relies on the original hostname for metrics attribution.

Changes

  1. lib/dispatcher/client.js: Sets client[kServerName] for HTTP connections when request.servername differs (the DNS interceptor already sets servername in dispatch opts)

  2. lib/core/connect.js: Overrides socket._host with the original hostname for HTTP connections when servername is provided

  3. test/interceptors/dns.js: Adds test verifying socket._host is the original hostname when using the DNS interceptor with HTTP

Verification

$ node -e "...
const { Agent, fetch, interceptors } = require('.');
const { dns } = interceptors;
...
const dispatcher = new Agent().compose(dns({
  lookup: (_origin, _opts, cb) => {
    cb(null, [{ address: '127.0.0.1', family: 4, ttl: 60_000 }]);
  }
}));
const response = await fetch('http://localhost:PORT', { dispatcher });
// socket._host is 'localhost', not '127.0.0.1'

All existing DNS interceptor tests pass.

…ptor

When the DNS interceptor resolves the hostname and replaces the
connection origin with the resolved IP address, the original hostname
was lost for HTTP connections. For HTTPS, the hostname was still
available through TLS SNI (servername), but for plain HTTP it was
nowhere accessible on the socket.

This fix:
1. Sets client[kServerName] for HTTP connections when request.servername
   differs (the DNS interceptor already sets servername in dispatch opts)
2. Overrides socket._host with the original hostname for HTTP connections
   when servername is provided

Fixes: #5573
@mcollina mcollina closed this Jul 22, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.46%. Comparing base (197a83d) to head (f89935a).
⚠️ Report is 25 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5581      +/-   ##
==========================================
+ Coverage   93.44%   93.46%   +0.02%     
==========================================
  Files         110      110              
  Lines       37443    37601     +158     
==========================================
+ Hits        34987    35144     +157     
- Misses       2456     2457       +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.

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.

2 participants