Skip to content

Commit 7b67469

Browse files
committed
util: prevent leaking internal properties
This prevents leaking of the internal `inspect()` properties when using a custom inspect function. It also aligns the indentation to the way it was in v8.0.0 since that changed unintentionally. All strings returned by the custom inspect function will now be indented appropriately to the current depth. PR-URL: #24971 Refs: #24765 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent be78266 commit 7b67469

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

doc/api/util.md

+4
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ stream.write('With ES6');
388388
<!-- YAML
389389
added: v0.3.0
390390
changes:
391+
- version: REPLACEME
392+
pr-url: https://github.com/nodejs/node/pull/24971
393+
description: Internal properties no longer appear in the context argument
394+
of a custom inspection function.
391395
- version: v11.7.0
392396
pr-url: https://github.com/nodejs/node/pull/25006
393397
description: ArrayBuffers now also show their binary contents.

lib/internal/util/inspect.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -516,18 +516,22 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
516516
maybeCustom !== inspect &&
517517
// Also filter out any prototype objects using the circular check.
518518
!(value.constructor && value.constructor.prototype === value)) {
519+
// Remove some internal properties from the options before passing it
520+
// through to the user function. This also prevents option manipulation.
521+
// eslint-disable-next-line no-unused-vars
522+
const { budget, seen, indentationLvl, ...plainCtx } = ctx;
519523
// This makes sure the recurseTimes are reported as before while using
520524
// a counter internally.
521525
const depth = ctx.depth === null ? null : ctx.depth - recurseTimes;
522-
const ret = maybeCustom.call(value, depth, ctx);
526+
const ret = maybeCustom.call(value, depth, plainCtx);
523527

524528
// If the custom inspection method returned `this`, don't go into
525529
// infinite recursion.
526530
if (ret !== value) {
527531
if (typeof ret !== 'string') {
528532
return formatValue(ctx, ret, recurseTimes);
529533
}
530-
return ret;
534+
return ret.replace(/\n/g, `\n${' '.repeat(ctx.indentationLvl)}`);
531535
}
532536
}
533537
}

test/parallel/test-util-inspect.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -775,9 +775,21 @@ util.inspect({ hasOwnProperty: null });
775775

776776
assert.strictEqual(util.inspect(subject), "{ foo: 'bar' }");
777777

778-
subject[util.inspect.custom] = (depth, opts) => {
779-
assert.strictEqual(opts.customInspectOptions, true);
780-
};
778+
subject[util.inspect.custom] = common.mustCall((depth, opts) => {
779+
const clone = { ...opts };
780+
// This might change at some point but for now we keep the stylize function.
781+
// The function should either be documented or an alternative should be
782+
// implemented.
783+
assert.strictEqual(typeof opts.stylize, 'function');
784+
assert.strictEqual(opts.seen, undefined);
785+
assert.strictEqual(opts.budget, undefined);
786+
assert.strictEqual(opts.indentationLvl, undefined);
787+
assert.strictEqual(opts.showHidden, false);
788+
opts.showHidden = true;
789+
return { [util.inspect.custom]: common.mustCall((depth, opts2) => {
790+
assert.deepStrictEqual(clone, opts2);
791+
}) };
792+
});
781793

782794
util.inspect(subject, { customInspectOptions: true });
783795

@@ -1593,7 +1605,7 @@ util.inspect(process);
15931605
);
15941606
const longList = util.inspect(list, { depth: Infinity });
15951607
const match = longList.match(/next/g);
1596-
assert(match.length > 1000 && match.length < 10000);
1608+
assert(match.length > 500 && match.length < 10000);
15971609
assert(longList.includes('[Object: Inspection interrupted ' +
15981610
'prematurely. Maximum call stack size exceeded.]'));
15991611
}

0 commit comments

Comments
 (0)