From 272ddb1765f732b119d3d74150b00591a363a56f Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 17 Dec 2018 11:55:13 -0500 Subject: [PATCH] console: improve inspectOptions validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds stricter type checking to the inspectOptions option to the Console constructor. PR-URL: https://github.com/nodejs/node/pull/25090 Reviewed-By: Michaƫl Zasso Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: Anto Aravinth Reviewed-By: James M Snell --- lib/internal/console/constructor.js | 4 +++- test/parallel/test-console-instance.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 89987aeec1ffd3..4d566394bfb23c 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -91,13 +91,15 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { if (typeof colorMode !== 'boolean' && colorMode !== 'auto') throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode); - if (inspectOptions) { + if (typeof inspectOptions === 'object' && inspectOptions !== null) { if (inspectOptions.colors !== undefined && options.colorMode !== undefined) { throw new ERR_INCOMPATIBLE_OPTION_PAIR( 'inspectOptions.color', 'colorMode'); } optionsMap.set(this, inspectOptions); + } else if (inspectOptions !== undefined) { + throw new ERR_INVALID_ARG_TYPE('inspectOptions', 'object', inspectOptions); } // Bind the prototype functions to this Console instance diff --git a/test/parallel/test-console-instance.js b/test/parallel/test-console-instance.js index 127e59e6e5b981..1c8b54ef4ca725 100644 --- a/test/parallel/test-console-instance.js +++ b/test/parallel/test-console-instance.js @@ -128,3 +128,21 @@ out.write = err.write = (d) => {}; assert.throws(() => c2.warn('foo'), /^Error: err$/); assert.throws(() => c2.dir('foo'), /^Error: out$/); } + +// Console constructor throws if inspectOptions is not an object. +[null, true, false, 'foo', 5, Symbol()].forEach((inspectOptions) => { + assert.throws( + () => { + new Console({ + stdout: out, + stderr: err, + inspectOptions + }); + }, + { + message: 'The "inspectOptions" argument must be of type object.' + + ` Received type ${typeof inspectOptions}`, + code: 'ERR_INVALID_ARG_TYPE' + } + ); +});