Skip to content

Commit

Permalink
util: escape C1 control characters and switch to hex format
Browse files Browse the repository at this point in the history
C1 control characters will from now on also be escaped to prevent
altering the terminal behavior.

Fixes: #29450

PR-URL: #29826
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
BridgeAR authored and addaleax committed Nov 19, 2019
1 parent 5e4d99a commit 0f78dcc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
43 changes: 24 additions & 19 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ const kArrayType = 1;
const kArrayExtrasType = 2;

/* eslint-disable no-control-regex */
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/;
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
const strEscapeSequencesRegExpSingle = /[\x00-\x1f\x5c]/;
const strEscapeSequencesReplacerSingle = /[\x00-\x1f\x5c]/g;
const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c\x7f-\x9f]/;
const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c\x7f-\x9f]/g;
const strEscapeSequencesRegExpSingle = /[\x00-\x1f\x5c\x7f-\x9f]/;
const strEscapeSequencesReplacerSingle = /[\x00-\x1f\x5c\x7f-\x9f]/g;
/* eslint-enable no-control-regex */

const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
Expand All @@ -134,21 +134,23 @@ const kWeak = 0;
const kIterator = 1;
const kMapEntries = 2;

// Escaped special characters. Use empty strings to fill up unused entries.
// Escaped control characters (plus the single quote and the backslash). Use
// empty strings to fill up unused entries.
const meta = [
'\\u0000', '\\u0001', '\\u0002', '\\u0003', '\\u0004',
'\\u0005', '\\u0006', '\\u0007', '\\b', '\\t',
'\\n', '\\u000b', '\\f', '\\r', '\\u000e',
'\\u000f', '\\u0010', '\\u0011', '\\u0012', '\\u0013',
'\\u0014', '\\u0015', '\\u0016', '\\u0017', '\\u0018',
'\\u0019', '\\u001a', '\\u001b', '\\u001c', '\\u001d',
'\\u001e', '\\u001f', '', '', '',
'', '', '', '', "\\'", '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '\\\\'
'\\x00', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\x07', // x07
'\\b', '\\t', '\\n', '\\x0B', '\\f', '\\r', '\\x0E', '\\x0F', // x0F
'\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17', // x17
'\\x18', '\\x19', '\\x1A', '\\x1B', '\\x1C', '\\x1D', '\\x1E', '\\x1F', // x1F
'', '', '', '', '', '', '', "\\'", '', '', '', '', '', '', '', '', // x2F
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x3F
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x4F
'', '', '', '', '', '', '', '', '', '', '', '', '\\\\', '', '', '', // x5F
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x6F
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '\\x7F', // x7F
'\\x80', '\\x81', '\\x82', '\\x83', '\\x84', '\\x85', '\\x86', '\\x87', // x87
'\\x88', '\\x89', '\\x8A', '\\x8B', '\\x8C', '\\x8D', '\\x8E', '\\x8F', // x8F
'\\x90', '\\x91', '\\x92', '\\x93', '\\x94', '\\x95', '\\x96', '\\x97', // x97
'\\x98', '\\x99', '\\x9A', '\\x9B', '\\x9C', '\\x9D', '\\x9E', '\\x9F', // x9F
];

function getUserOptions(ctx) {
Expand Down Expand Up @@ -317,7 +319,10 @@ function strEscape(str) {
const lastIndex = str.length;
for (let i = 0; i < lastIndex; i++) {
const point = str.charCodeAt(i);
if (point === singleQuote || point === 92 || point < 32) {
if (point === singleQuote ||
point === 92 ||
point < 32 ||
(point > 126 && point < 160)) {
if (last === i) {
result += meta[point];
} else {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-whatwg-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if (common.isWindows) {
code: 'ERR_INVALID_ARG_VALUE',
type: TypeError,
message: 'The argument \'path\' must be a string or Uint8Array without ' +
'null bytes. Received \'c:\\\\tmp\\\\\\u0000test\''
"null bytes. Received 'c:\\\\tmp\\\\\\x00test'"
}
);
} else {
Expand Down Expand Up @@ -96,8 +96,8 @@ if (common.isWindows) {
{
code: 'ERR_INVALID_ARG_VALUE',
type: TypeError,
message: 'The argument \'path\' must be a string or Uint8Array without ' +
'null bytes. Received \'/tmp/\\u0000test\''
message: "The argument 'path' must be a string or Uint8Array without " +
"null bytes. Received '/tmp/\\x00test'"
}
);
}
7 changes: 4 additions & 3 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ assert.strictEqual(
new Date('2010-02-14T12:48:40+01:00').toISOString()
);
assert.strictEqual(util.inspect(new Date('')), (new Date('')).toString());
assert.strictEqual(util.inspect('\n\u0001'), "'\\n\\u0001'");
assert.strictEqual(util.inspect('\n\x01'), "'\\n\\x01'");
assert.strictEqual(
util.inspect(`${Array(75).fill(1)}'\n\u001d\n\u0003`),
`"${Array(75).fill(1)}'\\n" +\n '\\u001d\\n' +\n '\\u0003'`
util.inspect(`${Array(75).fill(1)}'\n\x1d\n\x03\x85\x7f\x7e\x9f\xa0`),
// eslint-disable-next-line no-irregular-whitespace
`"${Array(75).fill(1)}'\\n" +\n '\\x1D\\n' +\n '\\x03\\x85\\x7F~\\x9F '`
);
assert.strictEqual(util.inspect([]), '[]');
assert.strictEqual(util.inspect(Object.create([])), 'Array {}');
Expand Down

0 comments on commit 0f78dcc

Please sign in to comment.