Skip to content

Commit 5c4f673

Browse files
committed
net: fix userinfo handling
1 parent 7563aff commit 5c4f673

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

lib/internal/socketaddress.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,13 @@ class SocketAddress {
160160

161161
const isIPv6 = address[0] === '[' && address.endsWith(']');
162162

163+
// Strip userinfo (e.g. "user:pass@") before searching for port separator.
163164
// For IPv6, indexOf(']:') + 1 points at ':' (or 0 if absent, treated as no port).
164165
// For IPv4, lastIndexOf(':') points at ':' (or -1 if absent).
165-
const sepIdx = isIPv6 ? input.indexOf(']:') + 1 : input.lastIndexOf(':');
166-
const port = sepIdx > 0 ? NumberParseInt(input.slice(sepIdx + 1), 10) || 0 : 0;
166+
const atIdx = input.lastIndexOf('@');
167+
const hostPart = atIdx >= 0 ? input.slice(atIdx + 1) : input;
168+
const sepIdx = isIPv6 ? hostPart.indexOf(']:') + 1 : hostPart.lastIndexOf(':');
169+
const port = sepIdx > 0 ? NumberParseInt(hostPart.slice(sepIdx + 1), 10) || 0 : 0;
167170

168171
return new SocketAddress({
169172
address: isIPv6 ? address.slice(1, -1) : address,

test/parallel/test-socketaddress.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ describe('net.SocketAddress...', () => {
152152
{ input: '127.0.0.1:443', address: '127.0.0.1', port: 443, family: 'ipv4' },
153153
{ input: '[::1]:80', address: '::1', port: 80, family: 'ipv6' },
154154
{ input: '[::1]:443', address: '::1', port: 443, family: 'ipv6' },
155+
{ input: 'user:80@1.2.3.4', address: '1.2.3.4', port: 0, family: 'ipv4' },
156+
{ input: 'user:80@1.2.3.4:80', address: '1.2.3.4', port: 80, family: 'ipv4' },
157+
{ input: 'user:80@1.2.3.4:123', address: '1.2.3.4', port: 123, family: 'ipv4' },
158+
{ input: 'user:80@[::1]', address: '::1', port: 0, family: 'ipv6' },
159+
{ input: 'user:80@[::1]:80', address: '::1', port: 80, family: 'ipv6' },
160+
{ input: 'user:80@[::1]:123', address: '::1', port: 123, family: 'ipv6' },
155161
];
156162

157163
good.forEach((i) => {

0 commit comments

Comments
 (0)