Skip to content

Commit 1dd15c9

Browse files
BridgeARRafaelGSS
authored andcommitted
util: limit inspect to only show own properties
`Error`'s `cause` and `errors` properties would be visible even if these were not own properties. This is changed to align with all other parts of the `inspect` handling. Fixes: #60717 PR-URL: #61032 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8f46bd9 commit 1dd15c9

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lib/internal/util/inspect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,15 +1955,15 @@ function formatError(err, constructor, tag, ctx, keys) {
19551955
}
19561956
name ??= 'Error';
19571957

1958-
if ('cause' in err &&
1958+
if (ObjectPrototypeHasOwnProperty(err, 'cause') &&
19591959
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'cause'))) {
19601960
ArrayPrototypePush(keys, 'cause');
19611961
}
19621962

19631963
// Print errors aggregated into AggregateError
19641964
try {
19651965
const errors = err.errors;
1966-
if (ArrayIsArray(errors) &&
1966+
if (ArrayIsArray(errors) && ObjectPrototypeHasOwnProperty(err, 'errors') &&
19671967
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'errors'))) {
19681968
ArrayPrototypePush(keys, 'errors');
19691969
}

test/parallel/test-util-inspect.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,53 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
688688
);
689689
}
690690

691+
{
692+
// No own errors or cause property.
693+
const { stackTraceLimit } = Error;
694+
Error.stackTraceLimit = 0;
695+
696+
const e1 = new Error('e1');
697+
const e2 = new TypeError('e2');
698+
const e3 = false;
699+
700+
const errors = [e1, e2, e3];
701+
const aggregateError = new AggregateError(errors, 'Foobar');
702+
703+
assert.deepStrictEqual(aggregateError.errors, errors);
704+
assert.strictEqual(
705+
util.inspect(aggregateError),
706+
'[AggregateError: Foobar] {\n [errors]: [ [Error: e1], [TypeError: e2], false ]\n}'
707+
);
708+
709+
710+
const custom = new Error('No own errors property');
711+
Object.setPrototypeOf(custom, aggregateError);
712+
713+
assert.strictEqual(
714+
util.inspect(custom),
715+
'[AggregateError: No own errors property]'
716+
);
717+
718+
const cause = [new Error('cause')];
719+
const causeError = new TypeError('Foobar', { cause: [new Error('cause')] });
720+
721+
assert.strictEqual(
722+
util.inspect(causeError),
723+
'[TypeError: Foobar] { [cause]: [ [Error: cause] ] }'
724+
);
725+
726+
const custom2 = new Error('No own cause property');
727+
Object.setPrototypeOf(custom2, causeError);
728+
729+
assert.deepStrictEqual(custom2.cause, cause);
730+
assert.strictEqual(
731+
util.inspect(custom2),
732+
'[TypeError: No own cause property]'
733+
);
734+
735+
Error.stackTraceLimit = stackTraceLimit;
736+
}
737+
691738
{
692739
const tmp = Error.stackTraceLimit;
693740
// Force stackTraceLimit = 0 for this test, but make it non-enumerable

0 commit comments

Comments
 (0)