Skip to content

Commit e2d7b34

Browse files
authored
dns: validate port range in setServers()
Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk> PR-URL: #65021 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent e43bedc commit e2d7b34

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

lib/internal/dns/utils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
validateArray,
2525
validateInt32,
2626
validateOneOf,
27+
validatePort,
2728
validateString,
2829
validateUint32,
2930
} = require('internal/validators');
@@ -129,6 +130,7 @@ class ResolverBase {
129130
if (ipVersion !== 0) {
130131
const port = NumberParseInt(
131132
RegExpPrototypeSymbolReplace(addrSplitRE, serv, '$2')) || IANA_DNS_PORT;
133+
validatePort(port);
132134
return ArrayPrototypePush(newSet, [ipVersion, match[1], port]);
133135
}
134136
}
@@ -138,13 +140,13 @@ class ResolverBase {
138140

139141
if (addrSplitMatch) {
140142
const hostIP = addrSplitMatch[1];
141-
const port = addrSplitMatch[2] || IANA_DNS_PORT;
143+
const port = NumberParseInt(addrSplitMatch[2]) || IANA_DNS_PORT;
142144

143145
ipVersion = isIP(hostIP);
144146

145147
if (ipVersion !== 0) {
146-
return ArrayPrototypePush(
147-
newSet, [ipVersion, hostIP, NumberParseInt(port)]);
148+
validatePort(port);
149+
return ArrayPrototypePush(newSet, [ipVersion, hostIP, port]);
148150
}
149151
}
150152

test/parallel/test-dns.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ assert(existing.length > 0);
9090
});
9191
}
9292

93+
{
94+
// Out-of-range ports, which should throw a clean error.
95+
const invalidPorts = [2 ** 16, 2 ** 32, 2 ** 64];
96+
invalidPorts.forEach((port) => {
97+
assert.throws(
98+
() => {
99+
dns.setServers([`1.2.3.4:${port}`]);
100+
},
101+
{
102+
name: 'RangeError',
103+
code: 'ERR_SOCKET_BAD_PORT'
104+
}
105+
);
106+
});
107+
}
108+
93109
const goog = [
94110
'8.8.8.8',
95111
'8.8.4.4',

0 commit comments

Comments
 (0)