From 0f78dcc86d9af8f742f76505c5a104c6dff17ca9 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 3 Oct 2019 03:41:38 +0200 Subject: util: escape C1 control characters and switch to hex format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C1 control characters will from now on also be escaped to prevent altering the terminal behavior. Fixes: https://github.com/nodejs/node/issues/29450 PR-URL: https://github.com/nodejs/node/pull/29826 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso --- lib/internal/util/inspect.js | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 2f922f6659..cc7f23f321 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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]*$/; @@ -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) { @@ -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 { -- cgit v1.2.3