Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: protect against monkeypatched Object prototype for inspect() #25953

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ const {
isBigUint64Array
} = require('internal/util/types');

const assert = require('internal/assert');

// Avoid monkey-patched built-ins.
const { Object } = primordials;
addaleax marked this conversation as resolved.
Show resolved Hide resolved

const ReflectApply = Reflect.apply;

// This function is borrowed from the function with the same name on V8 Extras'
Expand Down Expand Up @@ -383,13 +388,9 @@ function getKeys(value, showHidden) {
try {
keys = Object.keys(value);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
if (isNativeError(err) &&
err.name === 'ReferenceError' &&
isModuleNamespaceObject(value)) {
keys = Object.getOwnPropertyNames(value);
} else {
throw err;
}
assert(isNativeError(err) && err.name === 'ReferenceError' &&
isModuleNamespaceObject(value));
keys = Object.getOwnPropertyNames(value);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
}
if (symbols.length !== 0) {
keys.push(...symbols.filter((key) => propertyIsEnumerable(value, key)));
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-util-primordial-monkeypatching.js
Original file line number Diff line number Diff line change
@@ -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({}), '{}');