Skip to content

Commit cd18f6b

Browse files
mcollinaaduh95
authored andcommitted
http: redact proxy credentials in invalid proxy URL errors
The earlier fix to redact proxy credentials only covered the tunnel error and the ProxyConfig.href property. Two other error paths still interpolate the raw proxy URL, credentials included, into ERR_PROXY_INVALID_CONFIG messages: * lib/internal/http.js - ProxyConfig constructor when new URL() fails. * lib/internal/http.js - parseProxyConfigFromEnv when the URL contains CR or LF. Add a redactProxyUrl() helper that strips userinfo when parsing succeeds and falls back to a textual strip for the unparseable cases that reach the vulnerable paths, then route both sites through it. Refs: https://hackerone.com/reports/3966924 Signed-off-by: Matteo Collina <hello@matteocollina.com> Backport-PR-URL: nodejs-private/node-private#1048 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: nodejs-private/node-private#1001 CVE-ID: CVE-2026-123871
1 parent 67da864 commit cd18f6b

2 files changed

Lines changed: 126 additions & 2 deletions

File tree

‎lib/internal/http.js‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,40 @@ function ipToInt(ip) {
107107
* @property {Array<string>} bypassList - List of hosts to bypass the proxy.
108108
* @property {object} proxyConnectionOptions - Options for connecting to the proxy server.
109109
*/
110+
111+
/**
112+
* Redacts the userinfo (username/password) from a proxy URL so that credentials
113+
* never surface in error messages, which can end up in logs and crash reports.
114+
* This must not depend on parsing succeeding: it is used precisely when the URL
115+
* failed to parse.
116+
* @param {string} proxyUrl - The proxy URL to redact.
117+
* @returns {string} The proxy URL with any userinfo removed.
118+
*/
119+
function redactProxyUrl(proxyUrl) {
120+
const raw = `${proxyUrl}`;
121+
try {
122+
const parsedURL = new URL(raw);
123+
if (parsedURL.username || parsedURL.password) {
124+
parsedURL.username = '';
125+
parsedURL.password = '';
126+
return parsedURL.href;
127+
}
128+
return raw;
129+
} catch {
130+
// Unparseable: strip the userinfo textually. Greedy up to the last '@' in the
131+
// authority so a password containing '@' is redacted whole rather than
132+
// leaving a trailing fragment.
133+
return raw.replace(/\/\/[^/]*@/, '//[redacted]@');
134+
}
135+
}
136+
110137
class ProxyConfig {
111138
constructor(proxyUrl, keepAlive, noProxyList) {
112139
let parsedUrl;
113140
try {
114141
parsedUrl = new URL(proxyUrl);
115142
} catch {
116-
throw new ERR_PROXY_INVALID_CONFIG(`Invalid proxy URL: ${proxyUrl}`);
143+
throw new ERR_PROXY_INVALID_CONFIG(`Invalid proxy URL: ${redactProxyUrl(proxyUrl)}`);
117144
}
118145
const { host, hostname, port, protocol, username, password } = parsedUrl;
119146
this.href = proxyUrl; // Full URL of the proxy server.
@@ -209,7 +236,7 @@ function parseProxyConfigFromEnv(env, protocol, keepAlive) {
209236
}
210237

211238
if (proxyUrl.includes('\r') || proxyUrl.includes('\n')) {
212-
throw new ERR_PROXY_INVALID_CONFIG(`Invalid proxy URL: ${proxyUrl}`);
239+
throw new ERR_PROXY_INVALID_CONFIG(`Invalid proxy URL: ${redactProxyUrl(proxyUrl)}`);
213240
}
214241

215242
// Only http:// and https:// proxies are supported.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Regression test for the incomplete CVE-2026-48615 fix: proxy credentials must
2+
// never appear in ERR_PROXY_INVALID_CONFIG messages. Two error paths interpolate
3+
// the raw proxy URL (userinfo included):
4+
// 1. lib/internal/http.js - new URL() fails in the ProxyConfig constructor.
5+
// 2. lib/internal/http.js - proxy URL contains CR or LF.
6+
//
7+
// The well-formed credential control confirms redaction on the normal path is
8+
// preserved.
9+
10+
import '../common/index.mjs';
11+
import assert from 'node:assert';
12+
import { spawnSync } from 'node:child_process';
13+
14+
// A proxy credential that both contains '@' and yields substrings in every
15+
// meaningful piece that must not survive in any error message.
16+
const SECRET = 'S3cr3t-Pr0xy-P@ssw0rd';
17+
18+
function assertNoLeak(message, label) {
19+
assert.doesNotMatch(message, /S3cr3t/, `${label}: leaked username`);
20+
assert.doesNotMatch(message, /Pr0xy/, `${label}: leaked password fragment`);
21+
assert.doesNotMatch(message, /ssw0rd/, `${label}: leaked password fragment`);
22+
assert.doesNotMatch(message, /P@ssw0rd/, `${label}: leaked password`);
23+
}
24+
25+
function runNode(code, env = {}) {
26+
const res = spawnSync(process.execPath, ['-e', code], {
27+
env: { ...process.env, ...env },
28+
encoding: 'utf8',
29+
});
30+
return res;
31+
}
32+
33+
function agentWithProxyEnv(proxyEnv) {
34+
return `try {
35+
new (require('http').Agent)({ proxyEnv: ${JSON.stringify(proxyEnv)} });
36+
console.log('ok');
37+
} catch (e) {
38+
console.log(e.message);
39+
}`;
40+
}
41+
42+
// --- Site 1: ProxyConfig constructor, new URL() throws ---
43+
44+
{
45+
const { stdout, stderr } = runNode(agentWithProxyEnv({
46+
HTTP_PROXY: `http://alice:${SECRET}@::bad`,
47+
}));
48+
assertNoLeak(stdout, 'site 1');
49+
assertNoLeak(stderr, 'site 1 stderr');
50+
assert.match(stdout, /Invalid proxy URL: http:\/\/\[redacted\]@/);
51+
}
52+
53+
// --- Site 2: proxy URL containing CR or LF ---
54+
55+
{
56+
const { stdout, stderr } = runNode(agentWithProxyEnv({
57+
HTTP_PROXY: `http://alice:${SECRET}@\nhost`,
58+
}));
59+
assertNoLeak(stdout, 'site 2');
60+
assertNoLeak(stderr, 'site 2 stderr');
61+
assert.match(stdout, /Invalid proxy URL: /);
62+
}
63+
64+
// --- Sites 1 & 2 via the environment-driven, opt-in path (NODE_USE_ENV_PROXY) ---
65+
66+
{
67+
const code = `require('http').request('http://example.com/').on('error', () => {}).end()`;
68+
const { stdout, stderr, status } = runNode(code, {
69+
NODE_USE_ENV_PROXY: '1',
70+
http_proxy: `http://alice:${SECRET}@:::bad`,
71+
});
72+
// The error is thrown while the global agent is created, so stderr carries
73+
// the message and the process exits non-zero.
74+
assertNoLeak(stdout, 'env-driven stdout');
75+
assertNoLeak(stderr, 'env-driven stderr');
76+
assert.notStrictEqual(status, 0);
77+
}
78+
79+
// --- Control: well-formed URL with credentials does not throw ---
80+
81+
{
82+
const { stdout, status } = runNode(agentWithProxyEnv({
83+
HTTP_PROXY: `http://alice:${SECRET}@localhost:8080`,
84+
}));
85+
assert.strictEqual(status, 0);
86+
assert.strictEqual(stdout.trim(), 'ok');
87+
}
88+
89+
// --- Control: an unparseable URL with no credentials is left untouched ---
90+
91+
{
92+
const { stdout } = runNode(agentWithProxyEnv({
93+
HTTP_PROXY: 'http://host:port',
94+
}));
95+
assert.strictEqual(stdout.trim(), 'Invalid proxy URL: http://host:port');
96+
assert.doesNotMatch(stdout, /\[redacted\]/);
97+
}

0 commit comments

Comments
 (0)