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

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎lib/internal/console/constructor.js‎

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

94-
if(inspectOptions){
94+
if(typeofinspectOptions==='object'&&inspectOptions!==null){
9595
if(inspectOptions.colors!==undefined&&
9696
options.colorMode!==undefined){
9797
thrownewERR_INCOMPATIBLE_OPTION_PAIR(
9898
'inspectOptions.color','colorMode');
9999
}
100100
optionsMap.set(this,inspectOptions);
101+
}elseif(inspectOptions!==undefined){
102+
thrownewERR_INVALID_ARG_TYPE('inspectOptions','object',inspectOptions);
101103
}
102104

103105
// Bind the prototype functions to this Console instance

‎test/parallel/test-console-instance.js‎

Lines changed: 18 additions & 0 deletions
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+
newConsole({
137+
stdout: out,
138+
stderr: err,
139+
inspectOptions
140+
});
141+
},
142+
{
143+
message: 'The "inspectOptions" argument must be of type object.'+
144+
` Received type ${typeofinspectOptions}`,
145+
code: 'ERR_INVALID_ARG_TYPE'
146+
}
147+
);
148+
});

0 commit comments

Comments
 (0)