Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgram: tighten address validation in socket.send #39190

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
VoltrexKeyva marked this conversation as resolved.
Show resolved Hide resolved
true,
false,
0n,
1n,
VoltrexKeyva marked this conversation as resolved.
Show resolved Hide resolved
{},
Symbol(),
VoltrexKeyva marked this conversation as resolved.
Show resolved Hide resolved
].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