Skip to content

Commit fcaaa73

Browse files
author
lazerg
committed
http: ignore trailing dot when matching NO_PROXY
Assisted-by: Claude Code Signed-off-by: lazerg <lazerg2@gmail.com>
1 parent 8df2dd8 commit fcaaa73

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎lib/internal/http.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ function ipToInt(ip) {
7575
return result >>> 0;
7676
}
7777

78+
// Follow curl's behavior: ignore a trailing dot in both the host and the NO_PROXY entries.
79+
function stripTrailingDot(host) {
80+
return host.length > 1 && host[host.length - 1] === '.' ? host.slice(0, -1) : host;
81+
}
82+
7883
// There are two factors in play when proxying the request:
7984
// 1. What the request protocol is, that is, whether users are sending it via
8085
// http.request or https.request, or whether they are sending
@@ -137,7 +142,7 @@ class ProxyConfig {
137142
this.auth = `Basic ${Buffer.from(auth).toString('base64')}`;
138143
}
139144
if (noProxyList) {
140-
this.bypassList = noProxyList.split(',').map((entry) => entry.trim().toLowerCase());
145+
this.bypassList = noProxyList.split(',').map((entry) => stripTrailingDot(entry.trim().toLowerCase()));
141146
} else {
142147
this.bypassList = []; // No bypass list provided.
143148
}
@@ -159,7 +164,7 @@ class ProxyConfig {
159164
return true; // No bypass list, always use the proxy.
160165
}
161166

162-
const host = hostname.toLowerCase();
167+
const host = stripTrailingDot(hostname.toLowerCase());
163168
const hostWithPort = port ? `${host}:${port}` : host;
164169

165170
for (let i = 0; i < bypassList.length; i++) {

‎test/client-proxy/test-http-proxy-request-no-proxy-domain.mjs‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { runProxiedRequest } from '../common/proxy-server.js';
1010
const server = http.createServer(common.mustCall((req, res) => {
1111
res.writeHead(200, { 'Content-Type': 'text/plain' });
1212
res.end('Hello World\n');
13-
}, 5));
13+
}, 7));
1414
server.on('error', common.mustNotCall((err) => { console.error('Server error', err); }));
1515
server.listen(0, '127.0.0.1');
1616
await once(server, 'listening');
@@ -78,6 +78,44 @@ await once(proxy, 'listening');
7878
assert.strictEqual(signal, null);
7979
}
8080

81+
// Test NO_PROXY ignores a trailing dot in the request hostname.
82+
{
83+
const { code, signal, stderr, stdout } = await runProxiedRequest({
84+
NODE_USE_ENV_PROXY: 1,
85+
REQUEST_URL: `http://test.example.com.:${server.address().port}/test`,
86+
HTTP_PROXY: `http://localhost:${proxy.address().port}`,
87+
RESOLVE_TO_LOCALHOST: 'test.example.com.',
88+
NO_PROXY: 'test.example.com',
89+
});
90+
91+
// The request should succeed and bypass proxy.
92+
assert.match(stdout, /Status Code: 200/);
93+
assert.match(stdout, /Hello World/);
94+
assert.match(stdout, /Resolving lookup for test\.example\.com\./);
95+
assert.strictEqual(stderr.trim(), '');
96+
assert.strictEqual(code, 0);
97+
assert.strictEqual(signal, null);
98+
}
99+
100+
// Test NO_PROXY ignores a trailing dot in the entry.
101+
{
102+
const { code, signal, stderr, stdout } = await runProxiedRequest({
103+
NODE_USE_ENV_PROXY: 1,
104+
REQUEST_URL: `http://test.example.com:${server.address().port}/test`,
105+
HTTP_PROXY: `http://localhost:${proxy.address().port}`,
106+
RESOLVE_TO_LOCALHOST: 'test.example.com',
107+
NO_PROXY: 'test.example.com.',
108+
});
109+
110+
// The request should succeed and bypass proxy.
111+
assert.match(stdout, /Status Code: 200/);
112+
assert.match(stdout, /Hello World/);
113+
assert.match(stdout, /Resolving lookup for test\.example\.com/);
114+
assert.strictEqual(stderr.trim(), '');
115+
assert.strictEqual(code, 0);
116+
assert.strictEqual(signal, null);
117+
}
118+
81119
// Test NO_PROXY with leading-dot entry should NOT match partial domain names.
82120
// Regression test: .example.com must not match notexample.com or badexample.com.
83121
{

0 commit comments

Comments
 (0)