Skip to content

Commit 310c722

Browse files
committed
net: check hex hostname
1 parent 4cbbbdc commit 310c722

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

‎doc/api/net.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,11 @@ added:
445445
* Returns: {net.SocketAddress} Returns a `SocketAddress` if parsing was successful.
446446
Otherwise returns `undefined`.
447447

448-
The `input` may contain only hexadecimal digits, `x`, `.`, `:`, `[`, and `]`.
449-
Anything else returns `undefined`, including other URL components such as
450-
`user@1.2.3.4` or `1.2.3.4/foo`, whitespace, control characters,
448+
The address portion of `input` must be a valid IPv4 or IPv6 address as
449+
recognized by the [WHATWG URL host parser][], and `input` may contain only
450+
hexadecimal digits, `x`, `.`, `:`, `[`, and `]`. Anything else returns
451+
`undefined`, including host names such as `example.com`, other URL components
452+
such as `user@1.2.3.4` or `1.2.3.4/foo`, whitespace, control characters,
451453
percent-encoding, and non-ASCII characters.
452454

453455
## Class: `net.Server`
@@ -2579,6 +2581,7 @@ console.log('listening on', server.address().port);
25792581
[RFC 8305]: https://www.rfc-editor.org/rfc/rfc8305.txt
25802582
[Readable Stream]: stream.md#class-streamreadable
25812583
[Transferring TCP handles to other threads]: #transferring-tcp-handles-to-other-threads
2584+
[WHATWG URL host parser]: https://url.spec.whatwg.org/#host-parsing
25822585
[`'close'`]: #event-close
25832586
[`'connect'`]: #event-connect
25842587
[`'connection'`]: #event-connection

‎lib/internal/socketaddress.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ const { URLParse } = require('internal/url');
4343
const kHandle = Symbol('kHandle');
4444
const kDetail = Symbol('kDetail');
4545

46-
// The complete character set of an "${address}:${port}" input. Everything else
47-
// is rejected: delimiters that would introduce a userinfo, path, query, or
48-
// fragment component, and characters the URL parser strips, decodes, or remaps.
46+
// The complete character set of an "${address}:${port}" input.
4947
const kValidInput = /^[0-9a-fA-FxX.:[\]]+$/;
5048

5149
class SocketAddress {
@@ -155,9 +153,6 @@ class SocketAddress {
155153
*/
156154
static parse(input) {
157155
validateString(input, 'input');
158-
// The URL parser below silently discards anything that is not the host or
159-
// port, and percent-decodes and IDNA-maps what remains, so restrict the
160-
// input to characters that can appear in an address and port first.
161156
if (RegExpPrototypeExec(kValidInput, input) === null) return;
162157
// While URL.parse is not expected to throw, there are several
163158
// other pieces here that do... the destucturing, the SocketAddress

‎test/parallel/test-socketaddress.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ describe('net.SocketAddress...', () => {
162162
'abc.123',
163163
'259.1.1.1',
164164
'12:12:12',
165+
// Host names.
166+
'cafe',
167+
'bad.cafe',
168+
'dead.beef',
165169
// Arbitrary URL components.
166170
'user:80@5.6.7.8',
167171
'user@1.2.3.4',

0 commit comments

Comments
 (0)