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: do not rely on mutable Object and Function' constructor prop #56188

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
30 changes: 25 additions & 5 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ const {
DatePrototypeToISOString,
DatePrototypeToString,
ErrorPrototypeToString,
Function,
FunctionPrototype,
FunctionPrototypeBind,
FunctionPrototypeCall,
FunctionPrototypeSymbolHasInstance,
FunctionPrototypeToString,
JSONStringify,
MapPrototypeEntries,
Expand All @@ -50,6 +53,7 @@ const {
ObjectGetPrototypeOf,
ObjectIs,
ObjectKeys,
ObjectPrototype,
ObjectPrototypeHasOwnProperty,
ObjectPrototypePropertyIsEnumerable,
ObjectSeal,
Expand Down Expand Up @@ -593,10 +597,26 @@ function isInstanceof(object, proto) {
}
}

// Special-case for some builtin prototypes in case their `constructor` property has been tampered.
const wellKnownPrototypes = new SafeMap();
wellKnownPrototypes.set(ObjectPrototype, { name: 'Object', constructor: Object });
wellKnownPrototypes.set(FunctionPrototype, { name: 'Function', constructor: Function });
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

function getConstructorName(obj, ctx, recurseTimes, protoProps) {
let firstProto;
const tmp = obj;
while (obj || isUndetectableObject(obj)) {
const wellKnownPrototypeNameAndConstructor = wellKnownPrototypes.get(obj);
if (wellKnownPrototypeNameAndConstructor != null) {
const { name, constructor } = wellKnownPrototypeNameAndConstructor;
if (FunctionPrototypeSymbolHasInstance(constructor, tmp)) {
if (protoProps !== undefined && firstProto !== obj) {
addPrototypeProperties(
ctx, tmp, firstProto || tmp, recurseTimes, protoProps);
}
return name;
}
}
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
const descriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor');
if (descriptor !== undefined &&
typeof descriptor.value === 'function' &&
Expand Down Expand Up @@ -954,7 +974,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
if (noIterator) {
keys = getKeys(value, ctx.showHidden);
braces = ['{', '}'];
if (constructor === 'Object') {
if (typeof value === 'function') {
base = getFunctionBase(value, constructor, tag);
if (keys.length === 0 && protoProps === undefined)
return ctx.stylize(base, 'special');
} else if (constructor === 'Object') {
if (isArgumentsObject(value)) {
braces[0] = '[Arguments] {';
} else if (tag !== '') {
Expand All @@ -963,10 +987,6 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
if (keys.length === 0 && protoProps === undefined) {
return `${braces[0]}}`;
}
} else if (typeof value === 'function') {
base = getFunctionBase(value, constructor, tag);
if (keys.length === 0 && protoProps === undefined)
return ctx.stylize(base, 'special');
} else if (isRegExp(value)) {
// Make RegExps say that they are RegExps
base = RegExpPrototypeToString(
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3355,3 +3355,33 @@ assert.strictEqual(
'}',
);
}

{
const o = {};
const { prototype: BuiltinPrototype } = Object;
const desc = Reflect.getOwnPropertyDescriptor(BuiltinPrototype, 'constructor');
Object.defineProperty(BuiltinPrototype, 'constructor', {
get: () => BuiltinPrototype,
configurable: true,
});
assert.strictEqual(
util.inspect(o),
'{}',
);
Object.defineProperty(BuiltinPrototype, 'constructor', desc);
}

{
const o = { f() {} };
const { prototype: BuiltinPrototype } = Function;
const desc = Reflect.getOwnPropertyDescriptor(BuiltinPrototype, 'constructor');
Object.defineProperty(BuiltinPrototype, 'constructor', {
get: () => BuiltinPrototype,
configurable: true,
});
assert.strictEqual(
util.inspect(o),
'{ f: [Function: f] }',
);
Object.defineProperty(BuiltinPrototype, 'constructor', desc);
}
Loading