-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
util: prevent proxy traps being triggered by .inspect() #26241
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -495,18 +495,23 @@ function formatValue(ctx, value, recurseTimes, typedArray) { | |
return ctx.stylize('null', 'null'); | ||
} | ||
|
||
// Memorize the context for custom inspection on proxies. | ||
const context = value; | ||
// Always check for proxies to prevent side effects and to prevent triggering | ||
// any proxy handlers. | ||
const proxy = getProxyDetails(value); | ||
if (proxy !== undefined) { | ||
if (ctx.showProxy && ctx.stop === undefined) { | ||
return formatProxy(ctx, proxy, recurseTimes); | ||
} | ||
value = proxy[0]; | ||
} | ||
|
||
if (ctx.stop !== undefined) { | ||
const name = getConstructorName(value, ctx) || value[Symbol.toStringTag]; | ||
return ctx.stylize(`[${name || 'Object'}]`, 'special'); | ||
} | ||
|
||
if (ctx.showProxy) { | ||
const proxy = getProxyDetails(value); | ||
if (proxy !== undefined) { | ||
return formatProxy(ctx, proxy, recurseTimes); | ||
} | ||
} | ||
|
||
// Provide a hook for user-specified inspect functions. | ||
// Check that value is an object with an inspect function on it. | ||
if (ctx.customInspect) { | ||
|
@@ -523,11 +528,10 @@ function formatValue(ctx, value, recurseTimes, typedArray) { | |
// This makes sure the recurseTimes are reported as before while using | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this PR the above ☝️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would contradict the actual intention of this PR: prevent traps from being called similar to browsers. Otherwise we'll keep on getting requests about proxied values not being inspectable. Relying on the get trap for anything like that is never a good idea. Some people asked for changing traps e.g., from a normal access to checking the descriptor and that could also happen anytime. I'll run CITGM after the security release is done to see if we find any impacted modules. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The core of what they're trying to accomplish is provide a customizer without one needing to be bolted on to the value(s) being inspected. Perhaps that could be an inspect option? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be possible but is really superior to adding the custom inspect symbol? The symbol is easily available ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The benefit is that you can customize inspection of objects you don't own. Some may not like bolting on symbol properties to objects they don't own because maybe the objects could be frozen, or have their own traps to contend with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I absolutely agree and that is the crux in this case: using a generic custom inspect function as the dev inspecting the object is the same as inspecting the value from the custom inspect function directly. Or do I miss something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the cases where no customization is needed the customizer could defer to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIC that's always possible without a custom inspect function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without a custom function the user would lack There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With a customizer option util.inspect(object, {
customInspect(value, depth, ctx) {
// Will always trigger with `value === object` and nothing else.
// `depth === util.inspect.defaultOptions.depth`
// `ctx` ==> is in this case identical to `util.inspect.defaultOptions` besides some internal state that is about to be removed.
}
}) |
||
// a counter internally. | ||
const depth = ctx.depth === null ? null : ctx.depth - recurseTimes; | ||
const ret = maybeCustom.call(value, depth, plainCtx); | ||
|
||
const ret = maybeCustom.call(context, depth, plainCtx); | ||
// If the custom inspection method returned `this`, don't go into | ||
// infinite recursion. | ||
if (ret !== value) { | ||
if (ret !== context) { | ||
if (typeof ret !== 'string') { | ||
return formatValue(ctx, ret, recurseTimes); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.