Skip to content

Commit 272ddb1

Browse files
cjihrigaddaleax
authored andcommitted
console: improve inspectOptions validation
This commit adds stricter type checking to the inspectOptions option to the Console constructor. PR-URL: #25090 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 65d485b commit 272ddb1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/internal/console/constructor.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
9191
if (typeof colorMode !== 'boolean' && colorMode !== 'auto')
9292
throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode);
9393

94-
if (inspectOptions) {
94+
if (typeof inspectOptions === 'object' && inspectOptions !== null) {
9595
if (inspectOptions.colors !== undefined &&
9696
options.colorMode !== undefined) {
9797
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
9898
'inspectOptions.color', 'colorMode');
9999
}
100100
optionsMap.set(this, inspectOptions);
101+
} else if (inspectOptions !== undefined) {
102+
throw new ERR_INVALID_ARG_TYPE('inspectOptions', 'object', inspectOptions);
101103
}
102104

103105
// Bind the prototype functions to this Console instance

test/parallel/test-console-instance.js

+18
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,21 @@ out.write = err.write = (d) => {};
128128
assert.throws(() => c2.warn('foo'), /^Error: err$/);
129129
assert.throws(() => c2.dir('foo'), /^Error: out$/);
130130
}
131+
132+
// Console constructor throws if inspectOptions is not an object.
133+
[null, true, false, 'foo', 5, Symbol()].forEach((inspectOptions) => {
134+
assert.throws(
135+
() => {
136+
new Console({
137+
stdout: out,
138+
stderr: err,
139+
inspectOptions
140+
});
141+
},
142+
{
143+
message: 'The "inspectOptions" argument must be of type object.' +
144+
` Received type ${typeof inspectOptions}`,
145+
code: 'ERR_INVALID_ARG_TYPE'
146+
}
147+
);
148+
});

0 commit comments

Comments
 (0)