From 19db590a9992bf7218752c7e26372b00aabff086 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 05:02:34 +0200 Subject: [PATCH] fix(hooks): compare ANSI-C delimiter bytes exactly and decode them without spreading Invalid UTF-8 in a $'...' delimiter became U+FFFD, which a body line could match although Bash compares raw bytes. Such a delimiter now matches no line, so its body runs to the end of the command, as in Bash. The bytes were also spread into one push() call, which threw for a delimiter of 200,000 characters. The hook then failed open. The bytes are now collected as buffers and concatenated once. --- scripts/lib/shell-split.js | 28 +++++++++++++++++++--------- tests/lib/shell-split.test.js | 5 +++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/scripts/lib/shell-split.js b/scripts/lib/shell-split.js index 0addc1ec4..56c9a29e9 100644 --- a/scripts/lib/shell-split.js +++ b/scripts/lib/shell-split.js @@ -17,26 +17,34 @@ function ansiCEscape(code, escape) { return ANSI_C_ESCAPES[code] ?? (`\\'"?`.includes(code) ? code : escape); } -// The text of $'...' quoting with its escapes decoded, as Bash decodes them. Hex and octal -// escapes are bytes, so $'\xc3\xa9' is the UTF-8 text é. +const UTF8 = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }); + +// The text of $'...' quoting with its escapes decoded, as Bash decodes them, or null when the +// bytes are not UTF-8. Hex and octal escapes are bytes, so $'\xc3\xa9' is the UTF-8 text é. function decodeAnsiC(text) { - const bytes = []; + const chunks = []; for (const [token, byte, code] of text.matchAll(ANSI_C_TOKEN_RE)) { if (byte !== undefined) { - bytes.push(byte[0] === 'x' ? parseInt(byte.slice(1), 16) : parseInt(byte, 8) & 0xff); + chunks.push(Buffer.of(byte[0] === 'x' ? parseInt(byte.slice(1), 16) : parseInt(byte, 8) & 0xff)); } else { - bytes.push(...Buffer.from(code === undefined ? token : ansiCEscape(code, token), 'utf8')); + chunks.push(Buffer.from(code === undefined ? token : ansiCEscape(code, token), 'utf8')); } } - return Buffer.from(bytes).toString('utf8'); + try { + return UTF8.decode(Buffer.concat(chunks)); + } catch { + return null; + } } // The shell word at `start`, up to an unquoted blank or operator character, with its quoting -// removed, and whether any part of it was quoted. Null if a quote does not close. +// removed, and whether any part of it was quoted. Null if a quote does not close. The word is +// null when it holds bytes that are not UTF-8: no line of the command can then be equal to it. function readWord(command, start) { let word = ''; let quote = null; let quoted = false; + let utf8 = true; let i = start; for (; i < command.length; i++) { const ch = command[i]; @@ -47,7 +55,9 @@ function readWord(command, start) { else word += ch; } else if (ansiC) { // $'...' decodes its escapes, so $'E\x4fF' is EOF - word += decodeAnsiC(ansiC[1]); + const decoded = decodeAnsiC(ansiC[1]); + if (decoded === null) utf8 = false; + else word += decoded; quoted = true; i += ansiC[0].length - 1; } else if (ch === "'" || ch === '"') { @@ -65,7 +75,7 @@ function readWord(command, start) { word += ch; } } - return quote ? null : { word, quoted, end: i }; + return quote ? null : { word: utf8 ? word : null, quoted, end: i }; } // A here-document opener at `index`, but not a here-string (<<<): `<<` or `<<-`, then the diff --git a/tests/lib/shell-split.test.js b/tests/lib/shell-split.test.js index 75d095cee..c20c1a21b 100644 --- a/tests/lib/shell-split.test.js +++ b/tests/lib/shell-split.test.js @@ -147,6 +147,11 @@ test('a here-document delimiter is a shell word, read without its quotes', () => // Hex and octal escapes are bytes, so both of these are the UTF-8 delimiter é assert.deepStrictEqual(splitShellSegments("cat <<$'\\xc3\\xa9'\n\u00c3\u00a9\nnpm run dev\n\u00e9\nls"), ["cat <<$'\\xc3\\xa9'", 'ls']); assert.deepStrictEqual(splitShellSegments("cat <<$'\\303\\251'\nnpm run dev\n\u00e9\nls"), ["cat <<$'\\303\\251'", 'ls']); + // A delimiter whose bytes are not UTF-8 matches no line, so the body runs to the end + assert.deepStrictEqual(splitShellSegments("cat <<$'\\xff'\n\ufffd\nnpm run dev"), ["cat <<$'\\xff'"]); + // A long delimiter is read in one pass + const long = 'a'.repeat(200000); + assert.deepStrictEqual(splitShellSegments(`cat <<$'${long}'\ntext\n${long}\nnpm run dev`), [`cat <<$'${long}'`, 'npm run dev']); // An empty delimiter ends the body at the first empty line assert.deepStrictEqual(splitShellSegments("cat <<''\nnpm run dev\n\nls"), ["cat <<''", 'ls']); });