Skip to content

Commit

Permalink
util: escaping object keys in util.inspect()
Browse files Browse the repository at this point in the history
PR-URL: #16986
Fixes: #16979
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
ah-yu authored and MylesBorins committed Dec 11, 2017
1 parent 23c98fa commit a37eb32
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 35 deletions.
32 changes: 1 addition & 31 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ var Debug;

/* eslint-disable */
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
const keyEscapeSequencesRegExp = /[\x00-\x1f\x27]/;
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
const keyEscapeSequencesReplacer = /[\x00-\x1f\x27]/g;
/* eslint-enable */
const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
const colorRegExp = /\u001b\[\d\d?m/g;
Expand Down Expand Up @@ -137,34 +135,6 @@ function strEscape(str) {
return `'${result}'`;
}

// Escape control characters and single quotes.
// Note: for performance reasons this is not combined with strEscape
function keyEscape(str) {
if (str.length < 5000 && !keyEscapeSequencesRegExp.test(str))
return `'${str}'`;
if (str.length > 100)
return `'${str.replace(keyEscapeSequencesReplacer, escapeFn)}'`;
var result = '';
var last = 0;
for (var i = 0; i < str.length; i++) {
const point = str.charCodeAt(i);
if (point === 39 || point < 32) {
if (last === i) {
result += meta[point];
} else {
result += `${str.slice(last, i)}${meta[point]}`;
}
last = i + 1;
}
}
if (last === 0) {
result = str;
} else if (last !== i) {
result += str.slice(last);
}
return `'${result}'`;
}

function tryStringify(arg) {
try {
return JSON.stringify(arg);
Expand Down Expand Up @@ -859,7 +829,7 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
} else if (keyStrRegExp.test(key)) {
name = ctx.stylize(key, 'name');
} else {
name = ctx.stylize(keyEscape(key), 'string');
name = ctx.stylize(strEscape(key), 'string');
}

return `${name}: ${str}`;
Expand Down
14 changes: 10 additions & 4 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,25 +567,31 @@ assert.doesNotThrow(() => {
assert.strictEqual(util.inspect(x).includes('inspect'), true);
}

// util.inspect should not display the escaped value of a key.
// util.inspect should display the escaped value of a key.
{
const w = {
'\\': 1,
'\\\\': 2,
'\\\\\\': 3,
'\\\\\\\\': 4,
'\n': 5,
'\r': 6
};

const y = ['a', 'b', 'c'];
y['\\\\\\'] = 'd';
y['\\\\'] = 'd';
y['\n'] = 'e';
y['\r'] = 'f';

assert.strictEqual(
util.inspect(w),
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }'
'{ \'\\\\\': 1, \'\\\\\\\\\': 2, \'\\\\\\\\\\\\\': 3, ' +
'\'\\\\\\\\\\\\\\\\\': 4, \'\\n\': 5, \'\\r\': 6 }'
);
assert.strictEqual(
util.inspect(y),
'[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]'
'[ \'a\', \'b\', \'c\', \'\\\\\\\\\': \'d\', ' +
'\'\\n\': \'e\', \'\\r\': \'f\' ]'
);
}

Expand Down

0 comments on commit a37eb32

Please sign in to comment.