Skip to content

Commit 0c080fb

Browse files
andris9claude
andcommitted
fix: replace deprecated url.parse with a WHATWG URL wrapper
lib/fetch, lib/mailer, lib/smtp-connection and lib/shared parsed URLs with the deprecated url.parse()/url.resolve() (Node DEP0169, whose "behavior is not standardized and prone to errors that have security implications"). Route them through a new lib/shared/url.js that prefers the WHATWG URL and normalizes results back to the legacy field names, so every call site stays unchanged. Normalizing WHATWG output back to the legacy shape also fixes four regressions the raw WHATWG parser would otherwise introduce into the same remote-content / connection paths hardened in GHSA-55pm-vh62-rwjg: - IPv6 literal hosts kept their brackets ('[::1]'), which net/dns/ http.request treat as a DNS name -> ENOTFOUND. Strip them so fetch, the HTTP proxy client and SMTP connection URLs dial IPv6 again. - a password-only connection URL (smtps://:apikey@host) dropped the credential and connected unauthenticated; keep it. - non-ASCII hosts on non-special schemes (smtp:/smtps:/socks:) were percent-encoded instead of IDNA-mapped, so DNS failed; punycode them. - a host-less URL returned a null hostname that crashed Cookies.set; return '' as the legacy parser did. On Node < 6.13 (no WHATWG URL) and for input WHATWG rejects, the wrapper falls back to the legacy parser to preserve behavior. Refs: GHSA-55pm-vh62-rwjg Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 6a947ac commit 0c080fb

7 files changed

Lines changed: 393 additions & 5 deletions

File tree

‎lib/fetch/cookies.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// module to handle cookies
44

5-
const urllib = require('url');
5+
const urllib = require('../shared/url');
66

77
const SESSION_TIMEOUT = 1800; // 30 min
88

‎lib/fetch/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const http = require('http');
44
const https = require('https');
5-
const urllib = require('url');
5+
const urllib = require('../shared/url');
66
const zlib = require('zlib');
77
const { PassThrough } = require('stream');
88
const Cookies = require('./cookies');

‎lib/mailer/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const DKIM = require('../dkim');
88
const httpProxyClient = require('../smtp-connection/http-proxy-client');
99
const errors = require('../errors');
1010
const util = require('util');
11-
const urllib = require('url');
11+
const urllib = require('../shared/url');
1212
const packageData = require('../../package.json');
1313
const MailMessage = require('./mail-message');
1414
const net = require('net');

‎lib/shared/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
'use strict';
44

5-
const urllib = require('url');
5+
const urllib = require('./url');
66
const util = require('util');
77
const fs = require('fs');
88
const nmfetch = require('../fetch');

‎lib/shared/url.js‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
'use strict';
2+
3+
// URL parsing wrapper. Prefers the WHATWG `URL` (a global on Node 10+, and
4+
// available as `require('url').URL` since Node 6.13+) and only falls back to the
5+
// legacy, deprecation-warning-emitting `url.parse()` / `url.resolve()` on ancient
6+
// Node versions that predate the WHATWG implementation.
7+
//
8+
// The WHATWG `URL` exposes a different shape than the legacy parser, so results
9+
// are normalized back into the legacy field names the rest of the codebase reads
10+
// (`protocol`, `hostname`, `port`, `pathname`, `path`, `search`, `auth`, `query`,
11+
// `href`). This keeps every existing call site unchanged.
12+
//
13+
// Known, accepted divergences from the legacy parser:
14+
// - non-special schemes (smtp:/smtps:/direct:) are not host-lowercased by
15+
// WHATWG; cosmetic only, SMTP/DNS hosts are case-insensitive. (IDNA mapping
16+
// and IPv6 brackets are normalized back by normalizeHostname below.)
17+
// - a literal unescaped ':' inside a password is percent-encoded by WHATWG;
18+
// such passwords should be percent-encoded by the caller anyway.
19+
20+
const urllib = require('url');
21+
const punycode = require('../punycode');
22+
23+
// WHATWG URL constructor if available, otherwise undefined (Node < 6.13).
24+
const URLImpl = (typeof URL !== 'undefined' && URL) || urllib.URL;
25+
26+
// Matches a "scheme:" not followed by "//" (and with something after it), used
27+
// to re-insert the authority separator the legacy parser did not require.
28+
const SLASHLESS_AUTHORITY = /^([a-zA-Z][a-zA-Z0-9+.-]*:)(?!\/\/)(.+)$/;
29+
30+
// decodeURIComponent that never throws. Legacy url.parse() decodes the auth
31+
// component but tolerates malformed percent sequences, so mirror that.
32+
function safeDecode(str) {
33+
try {
34+
return decodeURIComponent(str);
35+
} catch (_err) {
36+
return str;
37+
}
38+
}
39+
40+
// Derives the legacy-shaped bare hostname from a WHATWG URL. WHATWG keeps IPv6
41+
// literals bracketed ('[::1]') and, for non-special schemes (smtp:/smtps:/socks:),
42+
// percent-encodes a non-ASCII host instead of IDNA-mapping it. Both forms are
43+
// un-resolvable when handed to net/dns/http.request — which is what every call
44+
// site does — so map them back to what legacy url.parse() returned: the bare
45+
// address and the punycode form. Idempotent on plain ASCII and already-punycode
46+
// hosts, so special-scheme hosts (already IDNA-mapped by WHATWG) pass through.
47+
function normalizeHostname(raw) {
48+
let hostname = raw || '';
49+
if (!hostname) {
50+
// Host-less URL (e.g. 'direct:'): legacy returned '' here, not null;
51+
// consumers do `hostname.length` / `'.' + hostname`, so keep it a string.
52+
return '';
53+
}
54+
if (hostname.charAt(0) === '[' && hostname.charAt(hostname.length - 1) === ']') {
55+
return hostname.slice(1, -1);
56+
}
57+
return punycode.toASCII(safeDecode(hostname));
58+
}
59+
60+
module.exports.parse = (input, parseQueryString) => {
61+
input = input || '';
62+
63+
if (!URLImpl) {
64+
// Node < 6.13: no WHATWG URL available, use the legacy parser.
65+
return urllib.parse(input, parseQueryString);
66+
}
67+
68+
// Legacy url.parse() parses a "user:pass@host:port" authority that follows
69+
// the scheme even without the "//" separator, for schemes outside its
70+
// built-in slashed-protocol list (smtp:/smtps:/socks:/...). The WHATWG
71+
// parser instead treats a scheme not followed by "//" as an opaque path.
72+
// Re-insert the "//" so slash-less connection/proxy URLs keep resolving to
73+
// an authority, as they did before. This assumes a slash-authority scheme,
74+
// which every consumer here uses (http/https/smtp/smtps/socks/direct); an
75+
// opaque scheme like mailto:/data:/tel: would be mis-split, but none reach
76+
// this module.
77+
const slashless = SLASHLESS_AUTHORITY.exec(input);
78+
const normalized = slashless ? slashless[1] + '//' + slashless[2] : input;
79+
80+
let u;
81+
try {
82+
u = new URLImpl(normalized);
83+
} catch (_err) {
84+
// WHATWG rejects some input the legacy parser tolerated (empty/relative
85+
// strings, scheme-relative '//host/path', out-of-range ports, ...). Fall
86+
// back to the legacy parser so behavior — including the downstream errors
87+
// callers rely on — is preserved. This is the only path that can still
88+
// emit a deprecation warning; it fires for anything WHATWG cannot
89+
// represent, including legitimate relative URLs, not just malformed input.
90+
return urllib.parse(input, parseQueryString);
91+
}
92+
93+
const hostname = normalizeHostname(u.hostname);
94+
const port = u.port || null;
95+
const pathname = u.pathname || null;
96+
const search = u.search || null;
97+
98+
// Legacy `.auth` is the decoded "user[:pass]" string; WHATWG keeps the
99+
// username/password percent-encoded, so decode to stay byte-compatible with
100+
// existing consumers (parseConnectionUrl, Basic/Proxy-Authorization headers).
101+
let auth = null;
102+
if (u.username || u.password) {
103+
// Gate on password too: legacy url.parse('smtps://:pass@host').auth was
104+
// ':pass'. Dropping it would silently connect unauthenticated.
105+
auth = safeDecode(u.username) + (u.password ? ':' + safeDecode(u.password) : '');
106+
}
107+
108+
let query;
109+
if (parseQueryString) {
110+
// Mirror querystring.parse(): null-prototype object, repeated keys → array.
111+
query = Object.create(null);
112+
u.searchParams.forEach((value, key) => {
113+
if (Object.prototype.hasOwnProperty.call(query, key)) {
114+
if (Array.isArray(query[key])) {
115+
query[key].push(value);
116+
} else {
117+
query[key] = [query[key], value];
118+
}
119+
} else {
120+
query[key] = value;
121+
}
122+
});
123+
} else {
124+
query = search ? search.slice(1) : null;
125+
}
126+
127+
return {
128+
protocol: u.protocol || null,
129+
host: u.host || null,
130+
hostname,
131+
port,
132+
pathname,
133+
search,
134+
path: (pathname || '') + (search || '') || null,
135+
href: u.href,
136+
auth,
137+
query
138+
};
139+
};
140+
141+
module.exports.resolve = (from, to) => {
142+
if (!URLImpl) {
143+
return urllib.resolve(from, to);
144+
}
145+
try {
146+
return new URLImpl(to, from).href;
147+
} catch (_err) {
148+
// Malformed target — fall back to the legacy resolver.
149+
return urllib.resolve(from, to);
150+
}
151+
};

‎lib/smtp-connection/http-proxy-client.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
const net = require('net');
88
const tls = require('tls');
9-
const urllib = require('url');
9+
const urllib = require('../shared/url');
1010
const errors = require('../errors');
1111

1212
/**

0 commit comments

Comments
 (0)