From bbf39bcfecd3eda49ae2993937ddbc0b73416b72 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 1 Dec 2019 01:08:37 +0100 Subject: [PATCH] util: never trigger any proxy traps using `format()` Backport-PR-URL: https://github.com/nodejs/node/pull/31431 PR-URL: https://github.com/nodejs/node/pull/30767 Reviewed-By: James M Snell Reviewed-By: Denys Otrishko Reviewed-By: Rich Trott --- lib/internal/util/inspect.js | 7 +++++++ test/parallel/test-util-inspect-proxy.js | 3 +++ 2 files changed, 10 insertions(+) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 09f755ac00649f..38665d1bc2a515 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1646,6 +1646,13 @@ function format(...args) { } function hasBuiltInToString(value) { + // Prevent triggering proxy traps. + const getFullProxy = false; + const proxyTarget = getProxyDetails(value, getFullProxy); + if (proxyTarget !== undefined) { + value = proxyTarget; + } + // Count objects that have no `toString` function as built-in. if (typeof value.toString !== 'function') { return true; diff --git a/test/parallel/test-util-inspect-proxy.js b/test/parallel/test-util-inspect-proxy.js index 5e85b92f713175..5789a6ffbcf6c5 100644 --- a/test/parallel/test-util-inspect-proxy.js +++ b/test/parallel/test-util-inspect-proxy.js @@ -41,6 +41,9 @@ proxyObj = new Proxy(target, handler); // Inspecting the proxy should not actually walk it's properties util.inspect(proxyObj, opts); +// Make sure inspecting object does not trigger any proxy traps. +util.format('%s', proxyObj); + // getProxyDetails is an internal method, not intended for public use. // This is here to test that the internals are working correctly. let details = processUtil.getProxyDetails(proxyObj, true);