diff --git a/src/shared/__tests__/parse-command.spec.ts b/src/shared/__tests__/parse-command.spec.ts index fa41d31c6c..6a44ee5b6f 100644 --- a/src/shared/__tests__/parse-command.spec.ts +++ b/src/shared/__tests__/parse-command.spec.ts @@ -290,6 +290,39 @@ describe("parseCommand", () => { expect(result[1]).toBe("echo done") }) + it("does not absorb a control operator into the delimiter on the opener line", () => { + // A heredoc delimiter is a single shell word; a control operator + // (`;`, `|`, `&&`) on the same line terminates the word and must not + // be absorbed into the delimiter. Otherwise the terminator line is + // never found and the whole (valid) command is rejected as an + // unterminated heredoc. The whole heredoc is kept as one opaque token + // (matching the other heredoc cases), but must not produce a parse error. + const inputs = [ + "sh -c bash << EOF; echo done\necho hello\nEOF", + "sh -c bash << EOF | grep hi\necho hello\nEOF", + "sh -c bash << EOF && cat f\necho hello\nEOF", + ] + for (const input of inputs) { + const { commands: result, parseError } = parseCommand(input) + expect(parseError).toBeNull() + expect(result).toEqual([input]) + } + }) + + it("treats a CRLF heredoc as one command without an unterminated error", () => { + // With CRLF line endings the delimiter word ends at `\r` (just like + // the body scanner strips `\r` before comparing the terminator line). + // Without treating `\r` as a terminator, the delimiter would become + // `EOF\r`, never match the `EOF` terminator, and be rejected as an + // unterminated heredoc. The whole heredoc is kept as one opaque token + // (the trailing `\r\n` after the terminator is consumed, matching the + // LF case where the trailing newline is left as a separator). + const input = "sh -c bash << EOF\r\necho hello\r\nEOF\r\n" + const { commands: result, parseError } = parseCommand(input) + expect(parseError).toBeNull() + expect(result).toEqual(["sh -c bash << EOF\r\necho hello\r\nEOF"]) + }) + it("treats a heredoc with a missing terminator as one opaque token", () => { // An unterminated heredoc is a syntax error; the whole input must be // returned as a single token so no body line can be auto-approved alone. diff --git a/src/shared/parse-command.ts b/src/shared/parse-command.ts index 6b2fb4d267..2ee63a8d5a 100644 --- a/src/shared/parse-command.ts +++ b/src/shared/parse-command.ts @@ -142,6 +142,27 @@ interface ScanResult { * Returns the bare delimiter word (for terminator line matching) and the index * of the first character after the delimiter token. */ +// POSIX control operators that terminate a word. A heredoc delimiter is a +// single word, so it must stop at these just like the shell's tokenizer does. +// This prevents a trailing control operator (e.g. `;`, `|`, `&&`) from being +// absorbed into the delimiter (see the "match POSIX shell tokenization" rule +// in scanTopLevelQuotes). +function isHeredocDelimiterTerminator(char: string): boolean { + return ( + char === "\n" || + char === "\r" || + char === " " || + char === "\t" || + char === ";" || + char === "|" || + char === "&" || + char === ">" || + char === "<" || + char === "(" || + char === ")" + ) +} + function parseHeredocDelimiter(command: string, start: number): { delimiter: string; endIndex: number } { let i = start let delimiter = "" @@ -160,11 +181,11 @@ function parseHeredocDelimiter(command: string, start: number): { delimiter: str if (command[i] === '"') i++ // consume closing " } else if (command[i] === "\\") { i++ // skip backslash - while (i < command.length && command[i] !== "\n" && command[i] !== " " && command[i] !== "\t") { + while (i < command.length && !isHeredocDelimiterTerminator(command[i])) { delimiter += command[i++] } } else { - while (i < command.length && command[i] !== "\n" && command[i] !== " " && command[i] !== "\t") { + while (i < command.length && !isHeredocDelimiterTerminator(command[i])) { delimiter += command[i++] } }