From 46446623f511c0d8c7d8d0e87db9f3ad8e8bc77d Mon Sep 17 00:00:00 2001 From: Daijiro Wachi Date: Thu, 7 Oct 2021 15:44:19 +0900 Subject: [PATCH] net: throw error to object mode in Socket Fixes: https://github.com/nodejs/node/issues/40336 PR-URL: https://github.com/nodejs/node/pull/40344 Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Minwoo Jung Reviewed-By: Zijian Liu Reviewed-By: Robert Nagy --- lib/net.js | 15 ++++++++++ .../test-net-connect-options-invalid.js | 27 ++++++++++++++++++ test/parallel/test-socket-options-invalid.js | 28 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 test/parallel/test-net-connect-options-invalid.js create mode 100644 test/parallel/test-socket-options-invalid.js diff --git a/lib/net.js b/lib/net.js index ded48732c2e11e..666854a3ccff60 100644 --- a/lib/net.js +++ b/lib/net.js @@ -30,6 +30,7 @@ const { NumberIsNaN, NumberParseInt, ObjectDefineProperty, + ObjectKeys, ObjectSetPrototypeOf, Symbol, } = primordials; @@ -282,6 +283,20 @@ const kSetNoDelay = Symbol('kSetNoDelay'); function Socket(options) { if (!(this instanceof Socket)) return new Socket(options); + const invalidKeys = [ + 'objectMode', + 'readableObjectMode', + 'writableObjectMode', + ]; + invalidKeys.forEach((invalidKey) => { + if (ObjectKeys(options).includes(invalidKey)) { + throw new ERR_INVALID_ARG_VALUE( + `options.${invalidKey}`, + options[invalidKey], + 'is not supported' + ); + } + }); this.connecting = false; // Problem with this is that users can supply their own handle, that may not diff --git a/test/parallel/test-net-connect-options-invalid.js b/test/parallel/test-net-connect-options-invalid.js new file mode 100644 index 00000000000000..441e2583f3ee99 --- /dev/null +++ b/test/parallel/test-net-connect-options-invalid.js @@ -0,0 +1,27 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +{ + const invalidKeys = [ + 'objectMode', + 'readableObjectMode', + 'writableObjectMode', + ]; + invalidKeys.forEach((invalidKey) => { + const option = { + ...common.localhostIPv4, + [invalidKey]: true + }; + const message = `The property 'options.${invalidKey}' is not supported. Received true`; + + assert.throws(() => { + net.createConnection(option); + }, { + code: 'ERR_INVALID_ARG_VALUE', + name: 'TypeError', + message: new RegExp(message) + }); + }); +} diff --git a/test/parallel/test-socket-options-invalid.js b/test/parallel/test-socket-options-invalid.js new file mode 100644 index 00000000000000..96dc500cab8f08 --- /dev/null +++ b/test/parallel/test-socket-options-invalid.js @@ -0,0 +1,28 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +{ + const invalidKeys = [ + 'objectMode', + 'readableObjectMode', + 'writableObjectMode', + ]; + invalidKeys.forEach((invalidKey) => { + const option = { + ...common.localhostIPv4, + [invalidKey]: true + }; + const message = `The property 'options.${invalidKey}' is not supported. Received true`; + + assert.throws(() => { + const socket = new net.Socket(option); + socket.connect({ port: 8080 }); + }, { + code: 'ERR_INVALID_ARG_VALUE', + name: 'TypeError', + message: new RegExp(message) + }); + }); +}