diff --git a/benchmark/net/net-socketaddress-parse.js b/benchmark/net/net-socketaddress-parse.js new file mode 100644 index 000000000000..738101c89ed2 --- /dev/null +++ b/benchmark/net/net-socketaddress-parse.js @@ -0,0 +1,43 @@ +'use strict'; + +const common = require('../common.js'); +const { SocketAddress } = require('net'); + +const inputs = { + 'ipv4': [ + '127.0.0.1', + '10.168.209.250', + '255.255.255.255', + ], + 'ipv4-port': [ + '127.0.0.1:80', + '10.168.209.250:8080', + '255.255.255.255:65535', + ], + 'ipv6': [ + '[::1]', + '[2001:db8::1]', + '[fe80::1ff:fe23:4567:890a]', + ], + 'ipv6-port': [ + '[::1]:80', + '[2001:db8::1]:8080', + '[::ffff:127.0.0.1]:65535', + ], +}; + +const bench = common.createBenchmark(main, { + n: [1e6], + input: Object.keys(inputs), +}); + +function main({ n, input }) { + const values = inputs[input]; + const length = values.length; + + bench.start(); + for (let i = 0; i < n; i++) { + SocketAddress.parse(values[i % length]); + } + bench.end(n); +} diff --git a/doc/api/net.md b/doc/api/net.md index 8588ffd0462f..8eb51d1a9e0e 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -438,6 +438,12 @@ added: added: - v23.4.0 - v22.13.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/00000 + description: Input is now parsed strictly. URL syntax and legacy IPv4 + formats such as octal, hexadecimal and shorthand notation + are no longer accepted. --> * `input` {string} An input string containing an IP address and optional port, @@ -445,6 +451,29 @@ added: * Returns: {net.SocketAddress} Returns a `SocketAddress` if parsing was successful. Otherwise returns `undefined`. +The entire input must match one of the following forms: + +```text +socket-address = ipv4-socket-address / ipv6-socket-address +ipv4-socket-address = ipv4-address [ ":" port ] +ipv6-socket-address = "[" ipv6-address [ "%" scope-id ] "]" [ ":" port ] + +ipv4-address = octet 3( "." octet ) +octet = 1*3DIGIT ; no leading zeros; value <= 255 +ipv6-address = RFC 4291 textual form: groups of 1*4HEXDIG (leading + zeros allowed, case-insensitive), "::" compression, and + an optional trailing embedded ipv4-address +port = 1*DIGIT ; leading zeros allowed; value <= 65535 +scope-id = 1*DIGIT ; leading zeros allowed; value <= 4294967295 +``` + +Anything else returns `undefined`, including URL components such as userinfo, +paths, queries and fragments, surrounding whitespace, host names, non-ASCII +digits, and the legacy IPv4 notations that permit octal (`0177.0.0.1`), +hexadecimal (`0x7f.0.0.1`), integer (`2130706433`) and shorthand (`127.1`) +addresses. An IPv6 zone id must be numeric; interface names such as +`%eth0` are not accepted. + ## Class: `net.Server`