diff --git a/lib/net.js b/lib/net.js index 0f80b4189718ba..2f341f53a1b41f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -75,7 +75,7 @@ const { ERR_SOCKET_BAD_PORT, ERR_SOCKET_CLOSED } = errors.codes; - +const { validateInt32 } = require('internal/validators'); const kLastWriteQueueSize = Symbol('lastWriteQueueSize'); // Lazy loaded to improve startup performance. @@ -93,6 +93,7 @@ const { function noop() {} function createHandle(fd, is_server) { + validateInt32(fd, 'fd', 0); const type = TTYWrap.guessHandleType(fd); if (type === 'PIPE') { return new Pipe( diff --git a/test/parallel/test-net-socket-constructor.js b/test/parallel/test-net-socket-constructor.js index 6758e286fb1be3..ec2e99deb84880 100644 --- a/test/parallel/test-net-socket-constructor.js +++ b/test/parallel/test-net-socket-constructor.js @@ -4,6 +4,14 @@ const common = require('../common'); const assert = require('assert'); const net = require('net'); +common.expectsError(() => { + new net.Socket({ fd: -1 }); +}, { code: 'ERR_OUT_OF_RANGE' }); + +common.expectsError(() => { + new net.Socket({ fd: 'foo' }); +}, { code: 'ERR_INVALID_ARG_TYPE' }); + function test(sock, readable, writable) { let socket; if (sock instanceof net.Socket) {