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

net: validate fds passed to Socket constructor #21429

Merged
merged 1 commit into from
Jun 22, 2018
Merged
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
3 changes: 2 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-net-socket-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a test where fd is non-numeric?


common.expectsError(() => {
new net.Socket({ fd: 'foo' });
}, { code: 'ERR_INVALID_ARG_TYPE' });

function test(sock, readable, writable) {
let socket;
if (sock instanceof net.Socket) {
Expand Down