Skip to content

Commit a04f2f7

Browse files
committed
util: escape symbol and non-enumerable keys
These keys require escaping as they might also contain line breaks and other special characters. PR-URL: nodejs#22300 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent c535bde commit a04f2f7

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,9 +1256,10 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
12561256
return str;
12571257
}
12581258
if (typeof key === 'symbol') {
1259-
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
1259+
const tmp = key.toString().replace(strEscapeSequencesReplacer, escapeFn);
1260+
name = `[${ctx.stylize(tmp, 'symbol')}]`;
12601261
} else if (desc.enumerable === false) {
1261-
name = `[${key}]`;
1262+
name = `[${key.replace(strEscapeSequencesReplacer, escapeFn)}]`;
12621263
} else if (keyStrRegExp.test(key)) {
12631264
name = ctx.stylize(key, 'name');
12641265
} else {

test/parallel/test-util-inspect.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -838,22 +838,22 @@ if (typeof Symbol !== 'undefined') {
838838
const options = { showHidden: true };
839839
let subject = {};
840840

841-
subject[Symbol('symbol')] = 42;
841+
subject[Symbol('sym\nbol')] = 42;
842842

843-
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
843+
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
844844
assert.strictEqual(
845845
util.inspect(subject, options),
846-
'{ [Symbol(symbol)]: 42 }'
846+
'{ [Symbol(sym\\nbol)]: 42 }'
847847
);
848848

849849
Object.defineProperty(
850850
subject,
851851
Symbol(),
852852
{ enumerable: false, value: 'non-enum' });
853-
assert.strictEqual(util.inspect(subject), '{ [Symbol(symbol)]: 42 }');
853+
assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }');
854854
assert.strictEqual(
855855
util.inspect(subject, options),
856-
"{ [Symbol(symbol)]: 42, [Symbol()]: 'non-enum' }"
856+
"{ [Symbol(sym\\nbol)]: 42, [Symbol()]: 'non-enum' }"
857857
);
858858

859859
subject = [1, 2, 3];
@@ -1637,3 +1637,13 @@ assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
16371637
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
16381638
assert.strictEqual(inspect(new BigInt64Array([0n])), 'BigInt64Array [ 0n ]');
16391639
assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 0n ]');
1640+
1641+
// Verify non-enumerable keys get escaped.
1642+
{
1643+
const obj = {};
1644+
Object.defineProperty(obj, 'Non\nenumerable\tkey', { value: true });
1645+
assert.strictEqual(
1646+
util.inspect(obj, { showHidden: true }),
1647+
'{ [Non\\nenumerable\\tkey]: true }'
1648+
);
1649+
}

0 commit comments

Comments
 (0)