dgram: prefer strict equality, type validation
- Enforces strict comparisons in dgram - bindState should always be strictly equal to one of the defined constant states, and newHandle type is a string. - Check that the argument `type` in createSocket is not null when it is of type 'object', before using its `type` property. - Adds a test to check dgram.createSocket is properly validating its `type` argument. PR-URL: https://github.com/nodejs/node/pull/8011 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com> Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
This commit is contained in:
parent
a634554fca
commit
e9b6fbbf17
12
lib/dgram.js
12
lib/dgram.js
@ -39,13 +39,13 @@ function lookup6(address, callback) {
|
|||||||
|
|
||||||
|
|
||||||
function newHandle(type) {
|
function newHandle(type) {
|
||||||
if (type == 'udp4') {
|
if (type === 'udp4') {
|
||||||
const handle = new UDP();
|
const handle = new UDP();
|
||||||
handle.lookup = lookup4;
|
handle.lookup = lookup4;
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == 'udp6') {
|
if (type === 'udp6') {
|
||||||
const handle = new UDP();
|
const handle = new UDP();
|
||||||
handle.lookup = lookup6;
|
handle.lookup = lookup6;
|
||||||
handle.bind = handle.bind6;
|
handle.bind = handle.bind6;
|
||||||
@ -78,7 +78,7 @@ exports._createSocketHandle = function(address, port, addressType, fd, flags) {
|
|||||||
function Socket(type, listener) {
|
function Socket(type, listener) {
|
||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
|
|
||||||
if (typeof type === 'object') {
|
if (type !== null && typeof type === 'object') {
|
||||||
var options = type;
|
var options = type;
|
||||||
type = options.type;
|
type = options.type;
|
||||||
}
|
}
|
||||||
@ -137,7 +137,7 @@ Socket.prototype.bind = function(port_ /*, address, callback*/) {
|
|||||||
|
|
||||||
self._healthCheck();
|
self._healthCheck();
|
||||||
|
|
||||||
if (this._bindState != BIND_STATE_UNBOUND)
|
if (this._bindState !== BIND_STATE_UNBOUND)
|
||||||
throw new Error('Socket is already bound');
|
throw new Error('Socket is already bound');
|
||||||
|
|
||||||
this._bindState = BIND_STATE_BINDING;
|
this._bindState = BIND_STATE_BINDING;
|
||||||
@ -346,7 +346,7 @@ Socket.prototype.send = function(buffer,
|
|||||||
|
|
||||||
self._healthCheck();
|
self._healthCheck();
|
||||||
|
|
||||||
if (self._bindState == BIND_STATE_UNBOUND)
|
if (self._bindState === BIND_STATE_UNBOUND)
|
||||||
self.bind({port: 0, exclusive: true}, null);
|
self.bind({port: 0, exclusive: true}, null);
|
||||||
|
|
||||||
if (list.length === 0)
|
if (list.length === 0)
|
||||||
@ -354,7 +354,7 @@ Socket.prototype.send = function(buffer,
|
|||||||
|
|
||||||
// If the socket hasn't been bound yet, push the outbound packet onto the
|
// If the socket hasn't been bound yet, push the outbound packet onto the
|
||||||
// send queue and send after binding is complete.
|
// send queue and send after binding is complete.
|
||||||
if (self._bindState != BIND_STATE_BOUND) {
|
if (self._bindState !== BIND_STATE_BOUND) {
|
||||||
enqueue(self, self.send.bind(self, list, port, address, callback));
|
enqueue(self, self.send.bind(self, list, port, address, callback));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
36
test/parallel/test-dgram-createSocket-type.js
Normal file
36
test/parallel/test-dgram-createSocket-type.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
'use strict';
|
||||||
|
require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const dgram = require('dgram');
|
||||||
|
const invalidTypes = [
|
||||||
|
'test',
|
||||||
|
['udp4'],
|
||||||
|
new String('udp4'),
|
||||||
|
1,
|
||||||
|
{},
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
null,
|
||||||
|
undefined
|
||||||
|
];
|
||||||
|
const validTypes = [
|
||||||
|
'udp4',
|
||||||
|
'udp6',
|
||||||
|
{ type: 'udp4' },
|
||||||
|
{ type: 'udp6' }
|
||||||
|
];
|
||||||
|
|
||||||
|
// Error must be thrown with invalid types
|
||||||
|
invalidTypes.forEach((invalidType) => {
|
||||||
|
assert.throws(() => {
|
||||||
|
dgram.createSocket(invalidType);
|
||||||
|
}, /Bad socket type specified/);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Error must not be thrown with valid types
|
||||||
|
validTypes.forEach((validType) => {
|
||||||
|
assert.doesNotThrow(() => {
|
||||||
|
const socket = dgram.createSocket(validType);
|
||||||
|
socket.close();
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user