Skip to content

Commit 1847696

Browse files
committed
util: protect against monkeypatched Object prototype for inspect()
Prevent affects of monkeypatching (for example) Object.keys() when calling util.inspect(). PR-URL: #25953 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent ba4df92 commit 1847696

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lib/internal/util/inspect.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ const {
6363
isBigUint64Array
6464
} = require('internal/util/types');
6565

66+
const assert = require('internal/assert');
67+
68+
// Avoid monkey-patched built-ins.
69+
const { Object } = primordials;
70+
6671
const ReflectApply = Reflect.apply;
6772

6873
// This function is borrowed from the function with the same name on V8 Extras'
@@ -383,13 +388,9 @@ function getKeys(value, showHidden) {
383388
try {
384389
keys = Object.keys(value);
385390
} catch (err) {
386-
if (isNativeError(err) &&
387-
err.name === 'ReferenceError' &&
388-
isModuleNamespaceObject(value)) {
389-
keys = Object.getOwnPropertyNames(value);
390-
} else {
391-
throw err;
392-
}
391+
assert(isNativeError(err) && err.name === 'ReferenceError' &&
392+
isModuleNamespaceObject(value));
393+
keys = Object.getOwnPropertyNames(value);
393394
}
394395
if (symbols.length !== 0) {
395396
keys.push(...symbols.filter((key) => propertyIsEnumerable(value, key)));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
// Monkeypatch Object.keys() so that it throws an unexpected error. This tests
4+
// that `util.inspect()` is unaffected by monkey-patching `Object`.
5+
6+
require('../common');
7+
const assert = require('assert');
8+
const util = require('util');
9+
10+
Object.keys = () => { throw new Error('fhqwhgads'); };
11+
assert.strictEqual(util.inspect({}), '{}');

0 commit comments

Comments
 (0)