Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions lib/internal/socketaddress.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
NumberParseInt,
ObjectSetPrototypeOf,
Symbol,
} = primordials;
Expand Down Expand Up @@ -153,18 +154,22 @@ class SocketAddress {
// other pieces here that do... the destucturing, the SocketAddress
// constructor, etc. So we wrap this in a try/catch to be safe.
try {
const {
hostname: address,
// url.port strips default HTTP ports (e.g. 80), so parse the port from
// the raw input string instead.
const { hostname: address } = URLParse(`http://${input}`);

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

// For IPv6, indexOf(']:') + 1 points at ':' (or 0 if absent, treated as no port).
// For IPv4, lastIndexOf(':') points at ':' (or -1 if absent).
const sepIdx = isIPv6 ? input.indexOf(']:') + 1 : input.lastIndexOf(':');
const port = sepIdx > 0 ? NumberParseInt(input.slice(sepIdx + 1), 10) || 0 : 0;

return new SocketAddress({
address: isIPv6 ? address.slice(1, -1) : address,
port,
} = URLParse(`http://${input}`);
if (address.startsWith('[') && address.endsWith(']')) {
return new SocketAddress({
address: address.slice(1, -1),
port: port | 0,
family: 'ipv6',
});
}
return new SocketAddress({ address, port: port | 0 });
family: isIPv6 ? 'ipv6' : 'ipv4',
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this method is stated as Parse an "${ip}:${port}" formatted string into a SocketAddress. Surely we want to be rejecting arbitrary URL components anyway?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Reverted the userinfo handling and its tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can reject the other URL components in a follow-up semver-major PR.

} catch {
// Ignore errors here. Return undefined if the input cannot
// be successfully parsed or is not a proper socket address.
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-socketaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ describe('net.SocketAddress...', () => {
{ input: '0x.0x.0', address: '0.0.0.0', port: 0, family: 'ipv4' },
{ input: '[1:0::]', address: '1::', port: 0, family: 'ipv6' },
{ input: '[1::8]:123', address: '1::8', port: 123, family: 'ipv6' },
{ input: '127.0.0.1:80', address: '127.0.0.1', port: 80, family: 'ipv4' },
{ input: '127.0.0.1:443', address: '127.0.0.1', port: 443, family: 'ipv4' },
{ input: '[::1]:80', address: '::1', port: 80, family: 'ipv6' },
{ input: '[::1]:443', address: '::1', port: 443, family: 'ipv6' },
];

good.forEach((i) => {
Expand Down
Loading