From aa726ddd6617b0f72763d69c1968466b090b3023 Mon Sep 17 00:00:00 2001 From: Daijiro Wachi Date: Thu, 7 Oct 2021 15:29:40 +0900 Subject: [PATCH] net: throw error to object mode in Socket Fixes: https://github.com/nodejs/node/issues/40336 --- lib/net.js | 15 +++++++++++ .../test-net-connect-options-invalid.js | 27 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/parallel/test-net-connect-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) + }); + }); +}