Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
fixup! fixup! util: protect against monkeypatched Object prototype fo…
…r inspect()
  • Loading branch information
Trott committed Feb 8, 2019
commit 8734b6cafe34dd377f2fc4a6389289172ac95b5c
48 changes: 23 additions & 25 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const {

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

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

const ReflectApply = Reflect.apply;

// This function is borrowed from the function with the same name on V8 Extras'
Expand All @@ -76,9 +79,7 @@ function uncurryThis(func) {
return (thisArg, ...args) => ReflectApply(func, thisArg, args);
}

const propertyIsEnumerable = uncurryThis(
primordials.Object.prototype.propertyIsEnumerable
);
const propertyIsEnumerable = uncurryThis(Object.prototype.propertyIsEnumerable);
const regExpToString = uncurryThis(RegExp.prototype.toString);
const dateToISOString = uncurryThis(Date.prototype.toISOString);
const dateToString = uncurryThis(Date.prototype.toString);
Expand All @@ -93,10 +94,10 @@ const stringValueOf = uncurryThis(String.prototype.valueOf);
const setValues = uncurryThis(Set.prototype.values);
const mapEntries = uncurryThis(Map.prototype.entries);
const dateGetTime = uncurryThis(Date.prototype.getTime);
const hasOwnProperty = uncurryThis(primordials.Object.prototype.hasOwnProperty);
const hasOwnProperty = uncurryThis(Object.prototype.hasOwnProperty);
let hexSlice;

const inspectDefaultOptions = primordials.Object.seal({
const inspectDefaultOptions = Object.seal({
showHidden: false,
depth: 2,
colors: false,
Expand Down Expand Up @@ -189,7 +190,7 @@ function inspect(value, opts) {
if (typeof opts === 'boolean') {
ctx.showHidden = opts;
} else if (opts) {
const optKeys = primordials.Object.keys(opts);
const optKeys = Object.keys(opts);
for (var i = 0; i < optKeys.length; i++) {
ctx[optKeys[i]] = opts[optKeys[i]];
}
Expand All @@ -201,20 +202,20 @@ function inspect(value, opts) {
}
inspect.custom = customInspectSymbol;

primordials.Object.defineProperty(inspect, 'defaultOptions', {
Object.defineProperty(inspect, 'defaultOptions', {
get() {
return inspectDefaultOptions;
},
set(options) {
if (options === null || typeof options !== 'object') {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
return primordials.Object.assign(inspectDefaultOptions, options);
return Object.assign(inspectDefaultOptions, options);
}
});

// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = primordials.Object.assign(primordials.Object.create(null), {
inspect.colors = Object.assign(Object.create(null), {
bold: [1, 22],
italic: [3, 23],
underline: [4, 24],
Expand All @@ -231,7 +232,7 @@ inspect.colors = primordials.Object.assign(primordials.Object.create(null), {
});

// Don't use 'blue' not visible on cmd.exe
inspect.styles = primordials.Object.assign(primordials.Object.create(null), {
inspect.styles = Object.assign(Object.create(null), {
special: 'cyan',
number: 'yellow',
bigint: 'yellow',
Expand Down Expand Up @@ -331,15 +332,14 @@ function getEmptyFormatArray() {
function getConstructorName(obj, ctx) {
let firstProto;
while (obj) {
const descriptor =
primordials.Object.getOwnPropertyDescriptor(obj, 'constructor');
const descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
if (descriptor !== undefined &&
typeof descriptor.value === 'function' &&
descriptor.value.name !== '') {
return descriptor.value.name;
}

obj = primordials.Object.getPrototypeOf(obj);
obj = Object.getPrototypeOf(obj);
if (firstProto === undefined) {
firstProto = obj;
}
Expand Down Expand Up @@ -374,9 +374,9 @@ const getBoxedValue = formatPrimitive.bind(null, stylizeNoColor);
// Look up the keys of the object.
function getKeys(value, showHidden) {
let keys;
const symbols = primordials.Object.getOwnPropertySymbols(value);
const symbols = Object.getOwnPropertySymbols(value);
if (showHidden) {
keys = primordials.Object.getOwnPropertyNames(value);
keys = Object.getOwnPropertyNames(value);
if (symbols.length !== 0)
keys.push(...symbols);
} else {
Expand All @@ -386,11 +386,11 @@ function getKeys(value, showHidden) {
// TODO(devsnek): track https://github.com/tc39/ecma262/issues/1209
// and modify this logic as needed.
try {
keys = primordials.Object.keys(value);
keys = Object.keys(value);
Comment thread
addaleax marked this conversation as resolved.
} catch (err) {
assert(isNativeError(err) && err.name === 'ReferenceError' &&
isModuleNamespaceObject(value));
keys = primordials.Object.getOwnPropertyNames(value);
keys = Object.getOwnPropertyNames(value);
Comment thread
addaleax marked this conversation as resolved.
}
if (symbols.length !== 0) {
keys.push(...symbols.filter((key) => propertyIsEnumerable(value, key)));
Expand Down Expand Up @@ -455,8 +455,8 @@ function clazzWithNullPrototype(clazz, name) {
return '';
}
}
primordials.Object.defineProperty(NullPrototype.prototype.constructor, 'name',
{ value: `[${name}: null prototype]` });
Object.defineProperty(NullPrototype.prototype.constructor, 'name',
{ value: `[${name}: null prototype]` });
lazyNullPrototypeCache.set(clazz, NullPrototype);
return NullPrototype;
}
Expand All @@ -478,9 +478,7 @@ function noPrototypeIterator(ctx, value, recurseTimes) {
newVal = new clazz(value);
}
if (newVal !== undefined) {
primordials.Object.defineProperties(
newVal, primordials.Object.getOwnPropertyDescriptors(value)
);
Object.defineProperties(newVal, Object.getOwnPropertyDescriptors(value));
return formatRaw(ctx, newVal, recurseTimes);
}
}
Expand Down Expand Up @@ -809,7 +807,7 @@ function handleMaxCallStackSize(ctx, err, constructor, tag, indentationLvl) {

function formatNumber(fn, value) {
// Format -0 as '-0'. Checking `value === -0` won't distinguish 0 from -0.
return fn(primordials.Object.is(value, -0) ? '-0' : `${value}`, 'number');
return fn(Object.is(value, -0) ? '-0' : `${value}`, 'number');
}

function formatBigInt(fn, value) {
Expand Down Expand Up @@ -897,7 +895,7 @@ function formatNamespaceObject(ctx, value, recurseTimes, keys) {

// The array is sparse and/or has extra keys
function formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) {
const keys = primordials.Object.keys(value);
const keys = Object.keys(value);
let index = i;
for (; i < keys.length && output.length < maxLength; i++) {
const key = keys[i];
Expand Down Expand Up @@ -1128,7 +1126,7 @@ function formatPromise(ctx, value, recurseTimes) {
function formatProperty(ctx, value, recurseTimes, key, type) {
let name, str;
let extra = ' ';
const desc = primordials.Object.getOwnPropertyDescriptor(value, key) ||
const desc = Object.getOwnPropertyDescriptor(value, key) ||
{ value: value[key], enumerable: true };
if (desc.value !== undefined) {
const diff = (type !== kObjectType || ctx.compact === false) ? 2 : 3;
Expand Down