util: protect against monkeypatched Object prototype for inspect()
Prevent affects of monkeypatching (for example) Object.keys() when calling util.inspect(). PR-URL: https://github.com/nodejs/node/pull/25953 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
ba4df925eb
commit
1847696f4b
@ -63,6 +63,11 @@ const {
|
|||||||
isBigUint64Array
|
isBigUint64Array
|
||||||
} = require('internal/util/types');
|
} = require('internal/util/types');
|
||||||
|
|
||||||
|
const assert = require('internal/assert');
|
||||||
|
|
||||||
|
// Avoid monkey-patched built-ins.
|
||||||
|
const { Object } = primordials;
|
||||||
|
|
||||||
const ReflectApply = Reflect.apply;
|
const ReflectApply = Reflect.apply;
|
||||||
|
|
||||||
// This function is borrowed from the function with the same name on V8 Extras'
|
// This function is borrowed from the function with the same name on V8 Extras'
|
||||||
@ -383,13 +388,9 @@ function getKeys(value, showHidden) {
|
|||||||
try {
|
try {
|
||||||
keys = Object.keys(value);
|
keys = Object.keys(value);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (isNativeError(err) &&
|
assert(isNativeError(err) && err.name === 'ReferenceError' &&
|
||||||
err.name === 'ReferenceError' &&
|
isModuleNamespaceObject(value));
|
||||||
isModuleNamespaceObject(value)) {
|
|
||||||
keys = Object.getOwnPropertyNames(value);
|
keys = Object.getOwnPropertyNames(value);
|
||||||
} else {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (symbols.length !== 0) {
|
if (symbols.length !== 0) {
|
||||||
keys.push(...symbols.filter((key) => propertyIsEnumerable(value, key)));
|
keys.push(...symbols.filter((key) => propertyIsEnumerable(value, key)));
|
||||||
|
11
test/parallel/test-util-primordial-monkeypatching.js
Normal file
11
test/parallel/test-util-primordial-monkeypatching.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Monkeypatch Object.keys() so that it throws an unexpected error. This tests
|
||||||
|
// that `util.inspect()` is unaffected by monkey-patching `Object`.
|
||||||
|
|
||||||
|
require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
|
Object.keys = () => { throw new Error('fhqwhgads'); };
|
||||||
|
assert.strictEqual(util.inspect({}), '{}');
|
Loading…
x
Reference in New Issue
Block a user