Skip to content

Commit d9e95d8

Browse files
committed
net: validate fds passed to Socket constructor
This commit validates the file descriptor passed to the TTY wrap's guessHandleType() function. Prior to this commit, a bad file descriptor would trigger an abort in the binding layer. PR-URL: #21429 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 7ec6951 commit d9e95d8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/net.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const {
7575
ERR_SOCKET_BAD_PORT,
7676
ERR_SOCKET_CLOSED
7777
} = errors.codes;
78-
78+
const { validateInt32 } = require('internal/validators');
7979
const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
8080

8181
// Lazy loaded to improve startup performance.
@@ -93,6 +93,7 @@ const {
9393
function noop() {}
9494

9595
function createHandle(fd, is_server) {
96+
validateInt32(fd, 'fd', 0);
9697
const type = TTYWrap.guessHandleType(fd);
9798
if (type === 'PIPE') {
9899
return new Pipe(

test/parallel/test-net-socket-constructor.js

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const common = require('../common');
44
const assert = require('assert');
55
const net = require('net');
66

7+
common.expectsError(() => {
8+
new net.Socket({ fd: -1 });
9+
}, { code: 'ERR_OUT_OF_RANGE' });
10+
11+
common.expectsError(() => {
12+
new net.Socket({ fd: 'foo' });
13+
}, { code: 'ERR_INVALID_ARG_TYPE' });
14+
715
function test(sock, readable, writable) {
816
let socket;
917
if (sock instanceof net.Socket) {

0 commit comments

Comments
 (0)