fix(core): keep unknown ANSI-C escapes the way each shell does (#64263)

This commit is contained in:
Filip Hejmowski
2026-11-16 18:15:20 +01:00
committed by GitHub
Unverified
parent 96a6c43b34
commit 6a2300da32
3 changed files with 79 additions and 18 deletions
+38 -18
View File
@@ -8,6 +8,7 @@ export type OpaqueReason =
| "heredoc"
| "invalid-redirect"
| "invalid-structure"
| "unsupported-escape"
| "unterminated-escape"
| "unterminated-quote"
@@ -733,7 +734,7 @@ function scanBashOperatorOrSeparator(
state.redirectWordCount = state.words.length
}
if (redirect === "<<" || redirect === "<<-") {
const delimiter = bashHeredocDelimiter(input, index + redirect.length, redirect === "<<-")
const delimiter = bashHeredocDelimiter(input, index + redirect.length, redirect === "<<-", context.dialect)
if (!delimiter) return { kind: "opaque", reason: "invalid-redirect" }
state.hasFileRedirect = true
state.redirectPrefixEnd ??=
@@ -1151,8 +1152,8 @@ function scanBashUnit(
return index + 1
}
if (mode === "word" && opener === "$'") {
const quote = bashAnsiQuote(input, index + 1)
if (!quote) return { kind: "opaque", reason: "unterminated-quote" }
const quote = bashAnsiQuote(input, index + 1, context.dialect)
if ("reason" in quote) return quote
const firstQuote = input.indexOf("'", index + 2)
// Dash does not support $'...' and closes the single-quoted span at the first `'`, even after `\`.
if (
@@ -1166,8 +1167,8 @@ function scanBashUnit(
return quote.end
}
if (mode === "arithmetic" && opener === "$'") {
const quote = bashAnsiQuote(input, index + 1)
if (!quote || quote.end !== input.indexOf("'", index + 2) || /[()[\];"}]/.test(quote.value))
const quote = bashAnsiQuote(input, index + 1, context.dialect)
if ("reason" in quote || quote.end !== input.indexOf("'", index + 2) || /[()[\];"}]/.test(quote.value))
return { kind: "opaque", reason: "invalid-structure" }
if (text) bashAppend(text, quote.value)
return scanBashSpan(input, index + 2, depth, context, commands, BASH_SPANS.arithmeticQuote, allowBracket, text)
@@ -1497,8 +1498,8 @@ function scanBashParameter(
if (!quoted && context.dialect !== "bash" && char === "(" && index > start)
return { kind: "opaque", reason: "dynamic-execution" }
if (quoted && char === "'" && input[index - 1] === "$") {
const ansi = bashAnsiQuote(input, index)
if (!ansi) return { kind: "opaque", reason: "unterminated-quote" }
const ansi = bashAnsiQuote(input, index, context.dialect)
if ("reason" in ansi) return ansi
text.literal += ansi.value
}
const subscript = index === colon && char === "["
@@ -1677,7 +1678,12 @@ function scanBashArrayOrPattern(
return { kind: "opaque", reason: "command-substitution" }
}
function bashAnsiQuote(input: string, start: number, allowUnicode = true) {
function bashAnsiQuote(
input: string,
start: number,
dialect: Dialect,
allowUnicode = true,
): { value: string; end: number } | Opaque {
let value = ""
for (let index = start + 1; index < input.length; index++) {
if (input[index] === "'") return { value, end: index }
@@ -1686,7 +1692,7 @@ function bashAnsiQuote(input: string, start: number, allowUnicode = true) {
continue
}
const escaped = input[++index]
if (escaped === undefined) return
if (escaped === undefined) break
if (escaped in BASH_ANSI_ESCAPES) {
value += BASH_ANSI_ESCAPES[escaped]
continue
@@ -1701,16 +1707,30 @@ function bashAnsiQuote(input: string, start: number, allowUnicode = true) {
: /[0-7]/.test(escaped)
? /^[0-7]{1,3}/.exec(input.slice(index, index + 3))?.[0]
: undefined
if (!digits) return
const octal = /[0-7]/.test(escaped)
const point = parseInt(digits, octal ? 8 : 16)
if (point === 0 || point > 0x10ffff || (point >= 0xd800 && point <= 0xdfff)) return
value += String.fromCodePoint(point)
index += digits.length - (octal ? 1 : 0)
if (digits) {
const octal = /[0-7]/.test(escaped)
const point = parseInt(digits, octal ? 8 : 16)
if (point === 0 || point > 0x10ffff || (point >= 0xd800 && point <= 0xdfff))
return { kind: "opaque", reason: "unsupported-escape" }
value += String.fromCodePoint(point)
index += digits.length - (octal ? 1 : 0)
continue
}
// Bash keeps the backslash of an unknown escape and Zsh drops it, so Posix, which covers both, can't know the value.
// Bash 3.2 and 5 decode \c control escapes differently, and \u or \U stay rejected where Unicode is. Zsh reads \C and \M as key escapes, and \x, \u or \U
// without digits as NUL.
if (
dialect === "posix" ||
(dialect === "bash" && (escaped === "c" || (!allowUnicode && /[uU]/.test(escaped)))) ||
(dialect === "zsh" && /[CMxuU]/.test(escaped))
)
return { kind: "opaque", reason: "unsupported-escape" }
value += dialect === "bash" ? `\\${escaped}` : escaped
}
return { kind: "opaque", reason: "unterminated-quote" }
}
function bashHeredocDelimiter(input: string, start: number, tabs: boolean) {
function bashHeredocDelimiter(input: string, start: number, tabs: boolean, dialect: Dialect) {
let delimiter = ""
let dashDelimiter: string | undefined
let quoted = false
@@ -1726,8 +1746,8 @@ function bashHeredocDelimiter(input: string, start: number, tabs: boolean) {
if (!quote && char === "$" && input.startsWith("\\\n", index + 1)) return
// Dash retains $ in $'...' heredoc delimiters while Bash and Zsh strip $.
if (!quote && input.startsWith("$'", index)) {
const literal = bashAnsiQuote(input, index + 1, false)
if (!literal || literal.end !== input.indexOf("'", index + 2)) return
const literal = bashAnsiQuote(input, index + 1, dialect, false)
if ("reason" in literal || literal.end !== input.indexOf("'", index + 2)) return
dashDelimiter = `${dashDelimiter ?? delimiter}$${input.slice(index + 2, literal.end)}`
delimiter += literal.value
quoted = true
@@ -169,6 +169,45 @@ describe("ordinary Bash and Zsh syntax", () => {
})
})
// Expected values are what Bash 3.2 and 5.2 and Zsh 5.9 print for these words.
test.each([
["bash", "$'\\033\\[?1049'", "\x1b\\[?1049"],
["bash", "$'\\q\\8\\-\\xg\\ug\\Ug'", "\\q\\8\\-\\xg\\ug\\Ug"],
["bash", "$'a\\\nb'", "a\\\nb"],
["zsh", "$'\\033\\[?1049'", "\x1b[?1049"],
["zsh", "$'\\q\\8\\-\\c'", "q8-c"],
["zsh", "$'a\\\nb'", "a\nb"],
] as const)("decodes unknown ANSI-C escapes like %s: %s", (dialect, word, value) => {
expect(ShellScan.scan(`printf '%s' ${word} | od -c`, dialect)).toMatchObject({
kind: "scanned",
commands: [{ words: ["printf", "%s", value] }, { words: ["od", "-c"] }],
})
})
test("decodes unknown escapes in ANSI-C heredoc delimiters like each dialect", () => {
const source = "cat <<$'E\\[F'\nE[F\nE\\[F\nprintf done"
expect(ShellScan.scan(source, "bash")).toMatchObject({
kind: "scanned",
commands: [{ words: ["cat"] }, { words: ["printf", "done"] }],
})
expect(ShellScan.scan(source, "zsh")).toMatchObject({
kind: "scanned",
commands: [{ words: ["cat"] }, { words: ["E[F"] }, { words: ["printf", "done"] }],
})
})
test.each([
["posix", "$'\\['"],
["bash", "$'\\cA'"],
["bash", "$'a\\0b'"],
["zsh", "$'\\C-a'"],
["zsh", "$'\\M-a'"],
["zsh", "$'\\xg'"],
["zsh", "$'\\u'"],
] as const)("rejects ANSI-C escapes whose value differs between shells in %s: %s", (dialect, word) => {
expect(ShellScan.scan(`printf '%s' ${word}`, dialect)).toEqual({ kind: "opaque", reason: "unsupported-escape" })
})
test.each([
["coproc job { scan_probe; }", ["scan_probe"]],
["case x in @(one|$(scan_probe))) ;; esac", ["scan_probe"]],
@@ -501,6 +501,8 @@ const fixtures = [
"$'scan_probe\\Uz'",
"$'scan_probe\\C-@x'",
"$'scan_probe\\C@x'",
"$'scan\\_probe'",
": <<$'E\\[F'\nE[F\nscan_probe\nE\\[F",
"a=(1); set -A 'a[$(scan_probe)]' 1",
"a=(1); set +A 'a[$(scan_probe)]' 1",
"unsetopt noglobsubst; x='*(e:scan_probe:)'; echo $x",