From b5c5bd1f51ec7fb34f737e410bb474ca8ca7c92e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 11 Mar 2021 14:20:44 +0100 Subject: [PATCH] util: inspect __proto__ key as written in object literal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since util.inspect() gives object-literal-like output, handle the special `__proto__` key in the way that it would be handled in object literals. PR-URL: https://github.com/nodejs/node/pull/37713 Reviewed-By: Ruben Bridgewater Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Gus Caplan Reviewed-By: Darshan Sen Reviewed-By: Gerhard Stöbich --- lib/internal/util/inspect.js | 2 ++ test/parallel/test-util-inspect.js | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 2b6124f61105a1..ff59a05e0dfab9 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1739,6 +1739,8 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc, if (typeof key === 'symbol') { const tmp = key.toString().replace(strEscapeSequencesReplacer, escapeFn); name = `[${ctx.stylize(tmp, 'symbol')}]`; + } else if (key === '__proto__') { + name = "['__proto__']"; } else if (desc.enumerable === false) { name = `[${key.replace(strEscapeSequencesReplacer, escapeFn)}]`; } else if (keyStrRegExp.test(key)) { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 3435f2801b2644..ee67d2e79aec9a 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -3094,3 +3094,10 @@ assert.strictEqual( ']' ); } + +{ + assert.strictEqual( + util.inspect({ ['__proto__']: { a: 1 } }), + "{ ['__proto__']: { a: 1 } }" + ); +}