Skip to content

Commit 027ad71

Browse files
committed
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. As drive-by I changed an array to a set for faster lookup in assert. Fixes: #60717 Closes: #60724
1 parent 14f02fc commit 027ad71

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

lib/internal/assert/assertion_error.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const kReadableOperator = {
4242
const kMaxShortStringLength = 12;
4343
const kMaxLongStringLength = 512;
4444

45-
const kMethodsWithCustomMessageDiff = ['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual'];
45+
const kMethodsWithCustomMessageDiff = new Set(['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual']);
4646

4747
function copyError(source) {
4848
const target = ObjectAssign(
@@ -263,7 +263,7 @@ class AssertionError extends Error {
263263
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
264264

265265
if (message != null) {
266-
if (kMethodsWithCustomMessageDiff.includes(operator)) {
266+
if (kMethodsWithCustomMessageDiff.has(operator)) {
267267
super(createErrDiff(actual, expected, operator, message, diff));
268268
} else {
269269
super(String(message));
@@ -283,7 +283,7 @@ class AssertionError extends Error {
283283
expected = copyError(expected);
284284
}
285285

286-
if (kMethodsWithCustomMessageDiff.includes(operator)) {
286+
if (kMethodsWithCustomMessageDiff.has(operator)) {
287287
super(createErrDiff(actual, expected, operator, message, diff));
288288
} else if (operator === 'notDeepStrictEqual' ||
289289
operator === 'notStrictEqual') {

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)