Skip to content

Commit

Permalink
dgram: tighten address validation in socket.send
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
VoltrexKeyva authored and targos committed Sep 18, 2021
1 parent 6bfe5a6 commit 40c6e83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 5 additions & 1 deletion doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ if the socket is not connected.
<!-- YAML
added: v0.1.99
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/39190
description: The `address` parameter now only accepts a `string`, `null`
or `undefined`.
- version:
- v14.5.0
- v12.19.0
Expand Down Expand Up @@ -517,7 +521,7 @@ If `msg` is an array, `offset` and `length` must not be specified.

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

If the socket has not been previously bound with a call to `bind`, the socket
Expand Down
4 changes: 2 additions & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ Socket.prototype.send = function(buffer,
if (typeof address === 'function') {
callback = address;
address = undefined;
} else if (address && typeof address !== 'string') {
throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy'], address);
} else if (address != null) {
validateString(address, 'address');
}

healthCheck(this);
Expand Down
20 changes: 16 additions & 4 deletions test/parallel/test-dgram-send-address-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,39 @@ const dgram = require('dgram');

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

const defaultCases = ['', null, undefined];

const onMessage = common.mustSucceed((bytes) => {
assert.strictEqual(bytes, buf.length);
}, 6);
}, defaultCases.length + 1);

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

// Check valid addresses
[false, '', null, 0, undefined].forEach((address) => {
defaultCases.forEach((address) => {
client.send(buf, port, address, onMessage);
});

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

// Check invalid addresses
[[], 1, true].forEach((invalidInput) => {
[
[],
0,
1,
true,
false,
0n,
1n,
{},
Symbol(),
].forEach((invalidInput) => {
const expectedError = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "address" argument must be of type string or falsy.' +
message: 'The "address" argument must be of type string.' +
`${common.invalidArgTypeHelper(invalidInput)}`
};
assert.throws(() => client.send(buf, port, invalidInput), expectedError);
Expand Down

0 comments on commit 40c6e83

Please sign in to comment.