|
| 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 | +} |