Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 40c6e83

Browse filesBrowse files
VoltrexKeyvatargos
authored andcommitted
dgram: tighten address validation in socket.send
PR-URL: #39190 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6bfe5a6 commit 40c6e83
Copy full SHA for 40c6e83

File tree

Expand file treeCollapse file tree

3 files changed

+23
-7
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+23
-7
lines changed
Open diff view settings
Collapse file

‎doc/api/dgram.md‎

Copy file name to clipboardExpand all lines: doc/api/dgram.md
+5-1Lines changed: 5 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ if the socket is not connected.
468468
<!-- YAML
469469
added: v0.1.99
470470
changes:
471+
- version: REPLACEME
472+
pr-url: https://github.com/nodejs/node/pull/39190
473+
description: The `address` parameter now only accepts a `string`, `null`
474+
or `undefined`.
471475
- version:
472476
- v14.5.0
473477
- v12.19.0
@@ -517,7 +521,7 @@ If `msg` is an array, `offset` and `length` must not be specified.
517521

518522
The `address` argument is a string. If the value of `address` is a host name,
519523
DNS will be used to resolve the address of the host. If `address` is not
520-
provided or otherwise falsy, `'127.0.0.1'` (for `udp4` sockets) or `'::1'`
524+
provided or otherwise nullish, `'127.0.0.1'` (for `udp4` sockets) or `'::1'`
521525
(for `udp6` sockets) will be used by default.
522526

523527
If the socket has not been previously bound with a call to `bind`, the socket
Collapse file

‎lib/dgram.js‎

Copy file name to clipboardExpand all lines: lib/dgram.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,8 @@ Socket.prototype.send = function(buffer,
636636
if (typeof address === 'function') {
637637
callback = address;
638638
address = undefined;
639-
} else if (address && typeof address !== 'string') {
640-
throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy'], address);
639+
} else if (address != null) {
640+
validateString(address, 'address');
641641
}
642642

643643
healthCheck(this);
Collapse file

‎test/parallel/test-dgram-send-address-types.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-dgram-send-address-types.js
+16-4Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,39 @@ const dgram = require('dgram');
55

66
const buf = Buffer.from('test');
77

8+
const defaultCases = ['', null, undefined];
9+
810
const onMessage = common.mustSucceed((bytes) => {
911
assert.strictEqual(bytes, buf.length);
10-
}, 6);
12+
}, defaultCases.length + 1);
1113

1214
const client = dgram.createSocket('udp4').bind(0, () => {
1315
const port = client.address().port;
1416

1517
// Check valid addresses
16-
[false, '', null, 0, undefined].forEach((address) => {
18+
defaultCases.forEach((address) => {
1719
client.send(buf, port, address, onMessage);
1820
});
1921

2022
// Valid address: not provided
2123
client.send(buf, port, onMessage);
2224

2325
// Check invalid addresses
24-
[[], 1, true].forEach((invalidInput) => {
26+
[
27+
[],
28+
0,
29+
1,
30+
true,
31+
false,
32+
0n,
33+
1n,
34+
{},
35+
Symbol(),
36+
].forEach((invalidInput) => {
2537
const expectedError = {
2638
code: 'ERR_INVALID_ARG_TYPE',
2739
name: 'TypeError',
28-
message: 'The "address" argument must be of type string or falsy.' +
40+
message: 'The "address" argument must be of type string.' +
2941
`${common.invalidArgTypeHelper(invalidInput)}`
3042
};
3143
assert.throws(() => client.send(buf, port, invalidInput), expectedError);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.