From 351ac8e1ff4d43390846d69dd9a572509cf74ff2 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Sat, 14 May 2022 22:44:26 +0900 Subject: [PATCH] console: fix console.dir crash on a revoked proxy Fixes: https://github.com/nodejs/node/issues/43095 Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com --- lib/internal/util/inspect.js | 8 ++++++-- test/parallel/test-console-issue-43095.js | 10 ++++++++++ test/parallel/test-util-inspect-proxy.js | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-console-issue-43095.js diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index d78a4e97d218a6..1a6f842fb67b5a 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -761,6 +761,9 @@ function formatValue(ctx, value, recurseTimes, typedArray) { if (proxy !== undefined) { if (ctx.showProxy) { return formatProxy(ctx, proxy, recurseTimes); + } else if (proxy === null) { + // The proxy is revoked. + return ctx.stylize('null', 'null'); } value = proxy; } @@ -1966,9 +1969,10 @@ function hasBuiltInToString(value) { // Prevent triggering proxy traps. const getFullProxy = false; const proxyTarget = getProxyDetails(value, getFullProxy); - if (proxyTarget !== undefined) { - value = proxyTarget; + if (proxyTarget === undefined || proxyTarget === null) { + return false; } + value = proxyTarget; // Count objects that have no `toString` function as built-in. if (typeof value.toString !== 'function') { diff --git a/test/parallel/test-console-issue-43095.js b/test/parallel/test-console-issue-43095.js new file mode 100644 index 00000000000000..0e01ddec429178 --- /dev/null +++ b/test/parallel/test-console-issue-43095.js @@ -0,0 +1,10 @@ +'use strict'; + +require('../common'); + +const r = Proxy.revocable({}, {}); +r.revoke(); + +console.dir(r); +console.dir(r.proxy); +console.log(r.proxy); diff --git a/test/parallel/test-util-inspect-proxy.js b/test/parallel/test-util-inspect-proxy.js index 3e1341fadfc9f9..cf59059af122c2 100644 --- a/test/parallel/test-util-inspect-proxy.js +++ b/test/parallel/test-util-inspect-proxy.js @@ -57,6 +57,25 @@ 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), 'null'); +assert.strictEqual( + util.inspect(r.proxy, { showProxy: true }), + 'Proxy [ null, null ]', +); + assert.strictEqual( util.inspect(proxyObj, opts), 'Proxy [\n' +