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

console: fix console.dir crash on a revoked proxy #43100

Merged
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
6 changes: 6 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,9 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
// any proxy handlers.
const proxy = getProxyDetails(value, !!ctx.showProxy);
if (proxy !== undefined) {
if (proxy === null || proxy[0] === null) {
return ctx.stylize('<Revoked Proxy>', 'special');
}
if (ctx.showProxy) {
return formatProxy(ctx, proxy, recurseTimes);
}
Expand Down Expand Up @@ -1967,6 +1970,9 @@ function hasBuiltInToString(value) {
const getFullProxy = false;
const proxyTarget = getProxyDetails(value, getFullProxy);
if (proxyTarget !== undefined) {
if (proxyTarget === null) {
return true;
}
value = proxyTarget;
}

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-console-issue-43095.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

require('../common');
const { inspect } = require('node:util');

const r = Proxy.revocable({}, {});
r.revoke();

console.dir(r);
console.dir(r.proxy);
console.log(r.proxy);
console.log(inspect(r.proxy, { showProxy: true }));
21 changes: 21 additions & 0 deletions test/parallel/test-util-inspect-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ assert.strictEqual(handler, details[1]);
details = processUtil.getProxyDetails(proxyObj, false);
assert.strictEqual(target, details);

details = processUtil.getProxyDetails({}, true);
assert.strictEqual(details, undefined);

const r = Proxy.revocable({}, {});
r.revoke();

details = processUtil.getProxyDetails(r.proxy, true);
assert.strictEqual(details[0], null);
assert.strictEqual(details[1], null);

details = processUtil.getProxyDetails(r.proxy, false);
assert.strictEqual(details, null);

assert.strictEqual(util.inspect(r.proxy), '<Revoked Proxy>');
assert.strictEqual(
util.inspect(r, { showProxy: true }),
'{ proxy: <Revoked Proxy>, revoke: [Function (anonymous)] }',
);

assert.strictEqual(util.format('%s', r.proxy), '<Revoked Proxy>');

assert.strictEqual(
util.inspect(proxyObj, opts),
'Proxy [\n' +
Expand Down