fix(sftp): reject arithmetic insertion contexts and continuation-prompt cd

This commit is contained in:
陈大猫 committed 2026-11-25 03:15:19 +00:00
1 parent 5be716942a
commit d5eacb8584
4 files changed
+479 -20

No files matched your search

+5
View File
@@ -79,6 +79,7 @@ import {
} from "../domain/sftpLocatePathInTerminal";
import {
canSendSftpPathToTerminal,
isTerminalDraftContextSafeForCd,
isTerminalDraftContextSafeForPathInsertion,
resolveCdSftpPathInTerminalAction,
resolveInsertSftpPathInTerminalAction,
@@ -1940,6 +1941,10 @@ const SftpSidePanelInteractiveBody: React.FC<SftpSidePanelInteractiveBodyProps>
if (isTerminalSensitiveInputActive(action.sessionId)) return;
// Only submit cd at an idle shell prompt -- never append into typed input or a TUI.
if (!isTerminalReadyForCommandInjection(action.sessionId)) return;
// Reject continuation prompts (e.g. PS2 `>`, `dquote>`) where the shell is
// still reading an unterminated quote or compound command.
const draftContext = getTerminalPromptDraftContext(action.sessionId);
if (!draftContext || !isTerminalDraftContextSafeForCd(draftContext)) return;
terminalBackend.writeToSession(action.sessionId, action.data, { automated: true });
scheduleDeferredTerminalFocus(onRequestTerminalFocus);
}, [buildLiveTerminalPathContext, onRequestTerminalFocus, terminalBackend]);
@@ -253,7 +253,7 @@ test("SftpSidePanel guards terminal path insertion and cd writes and SftpPaneVie
);
assert.match(
sidePanelSource,
/const handleCdInTerminal = useCallback\([\s\S]*?isTerminalSensitiveInputActive\(action\.sessionId\)[\s\S]*?isTerminalReadyForCommandInjection\(action\.sessionId\)[\s\S]*?terminalBackend\.writeToSession\(action\.sessionId, action\.data, \{ automated: true \}\)[\s\S]*?scheduleDeferredTerminalFocus\(onRequestTerminalFocus\)/,
/const handleCdInTerminal = useCallback\([\s\S]*?isTerminalSensitiveInputActive\(action\.sessionId\)[\s\S]*?isTerminalReadyForCommandInjection\(action\.sessionId\)[\s\S]*?getTerminalPromptDraftContext\(action\.sessionId\)[\s\S]*?isTerminalDraftContextSafeForCd\(draftContext\)[\s\S]*?terminalBackend\.writeToSession\(action\.sessionId, action\.data, \{ automated: true \}\)[\s\S]*?scheduleDeferredTerminalFocus\(onRequestTerminalFocus\)/,
);
assert.match(
sidePanelSource,
+101
View File
@@ -8,6 +8,7 @@ import {
hasUnsafeTerminalControlChars,
isShellDraftSafeForPathInsertion,
isTerminalContinuationPrompt,
isTerminalDraftContextSafeForCd,
isTerminalDraftContextSafeForPathInsertion,
quoteSftpPathForShell,
resolveCdSftpPathInTerminalAction,
@@ -431,3 +432,103 @@ test("isTerminalDraftContextSafeForPathInsertion rejects continuation prompts an
true,
);
});
test("isShellDraftSafeForPathInsertion rejects POSIX arithmetic, parameter expansion, array subscripts, and let contexts", () => {
// Arithmetic expansion $(( ... )) and arithmetic command (( ... ))
assert.equal(isShellDraftSafeForPathInsertion("echo $((", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo $(( 1 + ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion('echo "$((', "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo $(( (1 + 2) + ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("(( ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("for (( i=0; ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo $(( $(echo ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo $(( 'a))' + ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo $(( 1 + 2 )) ", "posix"), true);
assert.equal(isShellDraftSafeForPathInsertion("echo $(( (1 + 2) + 3 )) ", "posix"), true);
assert.equal(isShellDraftSafeForPathInsertion('echo "$((1 + 2))" ', "posix"), true);
// Legacy $[...] arithmetic
assert.equal(isShellDraftSafeForPathInsertion("echo $[", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion('echo "$[', "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo $[1 + 2] ", "posix"), true);
// Parameter expansions and array subscripts
assert.equal(isShellDraftSafeForPathInsertion("echo ${a[", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion('echo "${a[', "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo ${var:-", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("a[", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("unset arr[", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("declare -i x[", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("cp ${DIR}/", "posix"), true);
assert.equal(isShellDraftSafeForPathInsertion('echo "${a[0]}" ', "posix"), true);
assert.equal(isShellDraftSafeForPathInsertion("a[0]=", "posix"), true);
// `let` arithmetic builtin and `[[` arithmetic comparison operators
assert.equal(isShellDraftSafeForPathInsertion("let ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo ok; let ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("echo $(let ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion('echo "foo; let " ', "posix"), true);
assert.equal(isShellDraftSafeForPathInsertion("[[ 1 -eq ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("[[ $x -gt ", "posix"), false);
assert.equal(isShellDraftSafeForPathInsertion("[[ -f ", "posix"), true);
assert.equal(isShellDraftSafeForPathInsertion("[[ 1 -eq 1 ]] && ls ", "posix"), true);
});
test("isTerminalDraftContextSafeForCd rejects continuation prompts and non-empty drafts", () => {
assert.equal(
isTerminalDraftContextSafeForCd({
promptText: "user@host:~$ ",
userInput: "",
pendingTypedInput: "",
}),
true,
);
assert.equal(
isTerminalDraftContextSafeForCd({
promptText: "root@host:/# ",
userInput: "",
pendingTypedInput: "",
}),
true,
);
// Continuation prompts must be rejected even when draft input is empty
for (const promptText of [
"dquote> ",
"quote> ",
"> ",
">> ",
"heredoc> ",
"dquote cmdsubst> ",
"pipe dquote> ",
"More? ",
]) {
assert.equal(
isTerminalDraftContextSafeForCd({
promptText,
userInput: "",
pendingTypedInput: "",
}),
false,
`expected continuation prompt ${JSON.stringify(promptText)} to block auto-cd`,
);
}
// Non-empty draft input or un-echoed typed input must block auto-cd
assert.equal(
isTerminalDraftContextSafeForCd({
promptText: "user@host:~$ ",
userInput: "ls ",
pendingTypedInput: "",
}),
false,
);
assert.equal(
isTerminalDraftContextSafeForCd({
promptText: "user@host:~$ ",
userInput: "",
pendingTypedInput: "rm -rf",
}),
false,
);
});
+372 -19
View File
@@ -25,8 +25,12 @@ const WINDOWS_UNC_PATH_RE = /^\\\\[^\\]+\\[^\\]+/;
const POWERSHELL_SINGLE_QUOTE_RE = /['\u2018\u2019\u201A\u201B]/g;
const POWERSHELL_SINGLE_QUOTE_CHARS = new Set(["'", "\u2018", "\u2019", "\u201A", "\u201B"]);
const POWERSHELL_DOUBLE_QUOTE_CHARS = new Set(['"', "\u201C", "\u201D", "\u201E"]);
const CONTINUATION_PROMPT_RE =
/^(?:(?:[a-z]*quote|heredoc|cmdsubst|pipe|for|if|while|until|case|select|then|else|elif|cursh|subsh|brace|math|array|param)?>|>>?)\s*$/i;
const CONTINUATION_PROMPT_WORD =
"(?:[a-z]*quote|heredoc|cmdsubst|pipe|for|if|while|until|case|select|then|else|elif|cursh|subsh|brace|math|array|param|function|repeat|foreach|eval|and|or)";
const CONTINUATION_PROMPT_RE = new RegExp(
`^(?:(?:${CONTINUATION_PROMPT_WORD}(?:\\s+${CONTINUATION_PROMPT_WORD})*)?>|>>?|More\\?)\\s*$`,
"i",
);
/**
* True when a string contains control bytes (C0, DEL, C1) or Unicode line
@@ -204,20 +208,36 @@ export function canSendSftpPathToTerminal(
|| protocol === "local";
}
const POSIX_IDENTIFIER_CHAR_RE = /[A-Za-z0-9_]/;
const POSIX_LET_COMMAND_RE = /^\s*let(?:\s+|$)/;
const POSIX_DOUBLE_BRACKET_ARITH_OP_RE = /(?:^|\s)-(?:eq|ne|lt|le|gt|ge)\s+\S*$/;
function isPosixDraftSafeForPathInsertion(draft: string): boolean {
type Context = "single" | "ansiSingle" | "double" | "backtick" | "cmdsubst";
const stack: Context[] = [];
type Frame =
| { kind: "single" }
| { kind: "ansiSingle" }
| { kind: "double" }
| { kind: "backtick" }
| { kind: "cmdsubst"; parenDepth: number; segmentStart: number }
| { kind: "arith"; parenDepth: number }
| { kind: "arithBracket"; bracketDepth: number }
| { kind: "param" }
| { kind: "subscript"; bracketDepth: number }
| { kind: "doubleBracket"; startIdx: number };
const stack: Frame[] = [];
let rootSegmentStart = 0;
for (let i = 0; i < draft.length; i += 1) {
const ch = draft[i];
const top = stack[stack.length - 1];
if (top === "single") {
if (top?.kind === "single") {
if (ch === "'") stack.pop();
continue;
}
if (top === "ansiSingle") {
if (top?.kind === "ansiSingle") {
if (ch === "\\") {
if (i + 1 >= draft.length) return false;
i += 1;
@@ -227,26 +247,41 @@ function isPosixDraftSafeForPathInsertion(draft: string): boolean {
continue;
}
if (top === "double") {
if (top?.kind === "double") {
if (ch === "\\") {
if (i + 1 >= draft.length) return false;
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "(" && draft[i + 2] === "(") {
stack.push({ kind: "arith", parenDepth: 0 });
i += 2;
continue;
}
if (ch === "$" && draft[i + 1] === "(") {
stack.push("cmdsubst");
stack.push({ kind: "cmdsubst", parenDepth: 0, segmentStart: i + 2 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "[") {
stack.push({ kind: "arithBracket", bracketDepth: 0 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "{") {
stack.push({ kind: "param" });
i += 1;
continue;
}
if (ch === "`") {
stack.push("backtick");
stack.push({ kind: "backtick" });
continue;
}
if (ch === '"') stack.pop();
continue;
}
if (top === "backtick") {
if (top?.kind === "backtick") {
if (ch === "\\") {
if (i + 1 >= draft.length) return false;
i += 1;
@@ -256,7 +291,210 @@ function isPosixDraftSafeForPathInsertion(draft: string): boolean {
continue;
}
// Outside quotes (top === undefined or top === "cmdsubst")
if (top?.kind === "arith") {
if (ch === "\\") {
if (i + 1 >= draft.length) return false;
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "(" && draft[i + 2] === "(") {
stack.push({ kind: "arith", parenDepth: 0 });
i += 2;
continue;
}
if (ch === "$" && draft[i + 1] === "(") {
stack.push({ kind: "cmdsubst", parenDepth: 0, segmentStart: i + 2 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "[") {
stack.push({ kind: "arithBracket", bracketDepth: 0 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "{") {
stack.push({ kind: "param" });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "'") {
stack.push({ kind: "ansiSingle" });
i += 1;
continue;
}
if (ch === "'") {
stack.push({ kind: "single" });
continue;
}
if (ch === '"') {
stack.push({ kind: "double" });
continue;
}
if (ch === "`") {
stack.push({ kind: "backtick" });
continue;
}
if (ch === "(") {
top.parenDepth += 1;
continue;
}
if (ch === ")") {
if (top.parenDepth > 0) {
top.parenDepth -= 1;
continue;
}
if (draft[i + 1] === ")") {
stack.pop();
i += 1;
}
}
continue;
}
if (top?.kind === "arithBracket") {
if (ch === "\\") {
if (i + 1 >= draft.length) return false;
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "'") {
stack.push({ kind: "ansiSingle" });
i += 1;
continue;
}
if (ch === "'") {
stack.push({ kind: "single" });
continue;
}
if (ch === '"') {
stack.push({ kind: "double" });
continue;
}
if (ch === "`") {
stack.push({ kind: "backtick" });
continue;
}
if (ch === "[") {
top.bracketDepth += 1;
continue;
}
if (ch === "]") {
if (top.bracketDepth > 0) {
top.bracketDepth -= 1;
} else {
stack.pop();
}
}
continue;
}
if (top?.kind === "param") {
if (ch === "\\") {
if (i + 1 >= draft.length) return false;
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "'") {
stack.push({ kind: "ansiSingle" });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "(" && draft[i + 2] === "(") {
stack.push({ kind: "arith", parenDepth: 0 });
i += 2;
continue;
}
if (ch === "$" && draft[i + 1] === "(") {
stack.push({ kind: "cmdsubst", parenDepth: 0, segmentStart: i + 2 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "[") {
stack.push({ kind: "arithBracket", bracketDepth: 0 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "{") {
stack.push({ kind: "param" });
i += 1;
continue;
}
if (ch === "'") {
stack.push({ kind: "single" });
continue;
}
if (ch === '"') {
stack.push({ kind: "double" });
continue;
}
if (ch === "`") {
stack.push({ kind: "backtick" });
continue;
}
if (ch === "}") {
stack.pop();
}
continue;
}
if (top?.kind === "subscript") {
if (ch === "\\") {
if (i + 1 >= draft.length) return false;
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "'") {
stack.push({ kind: "ansiSingle" });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "(" && draft[i + 2] === "(") {
stack.push({ kind: "arith", parenDepth: 0 });
i += 2;
continue;
}
if (ch === "$" && draft[i + 1] === "(") {
stack.push({ kind: "cmdsubst", parenDepth: 0, segmentStart: i + 2 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "[") {
stack.push({ kind: "arithBracket", bracketDepth: 0 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "{") {
stack.push({ kind: "param" });
i += 1;
continue;
}
if (ch === "'") {
stack.push({ kind: "single" });
continue;
}
if (ch === '"') {
stack.push({ kind: "double" });
continue;
}
if (ch === "`") {
stack.push({ kind: "backtick" });
continue;
}
if (ch === "[") {
top.bracketDepth += 1;
continue;
}
if (ch === "]") {
if (top.bracketDepth > 0) {
top.bracketDepth -= 1;
} else {
stack.pop();
}
}
continue;
}
// Outside quotes (top === undefined, "cmdsubst", or "doubleBracket")
if (ch === "\\") {
if (i + 1 >= draft.length) return false;
i += 1;
@@ -268,34 +506,130 @@ function isPosixDraftSafeForPathInsertion(draft: string): boolean {
return false;
}
if (ch === "$" && draft[i + 1] === "'") {
stack.push("ansiSingle");
stack.push({ kind: "ansiSingle" });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "(" && draft[i + 2] === "(") {
stack.push({ kind: "arith", parenDepth: 0 });
i += 2;
continue;
}
if (ch === "$" && draft[i + 1] === "(") {
stack.push("cmdsubst");
stack.push({ kind: "cmdsubst", parenDepth: 0, segmentStart: i + 2 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "[") {
stack.push({ kind: "arithBracket", bracketDepth: 0 });
i += 1;
continue;
}
if (ch === "$" && draft[i + 1] === "{") {
stack.push({ kind: "param" });
i += 1;
continue;
}
if (ch === "(" && draft[i + 1] === "(") {
stack.push({ kind: "arith", parenDepth: 0 });
i += 1;
continue;
}
if (
ch === "["
&& draft[i + 1] === "["
&& (i === 0 || /[\s;|&(\n]/.test(draft[i - 1]))
&& (i + 2 >= draft.length || /\s/.test(draft[i + 2]))
) {
stack.push({ kind: "doubleBracket", startIdx: i + 2 });
i += 1;
continue;
}
if (
top?.kind === "doubleBracket"
&& ch === "]"
&& draft[i + 1] === "]"
) {
stack.pop();
i += 1;
continue;
}
if (ch === "[" && i > 0 && POSIX_IDENTIFIER_CHAR_RE.test(draft[i - 1])) {
// Array element subscript (e.g. `a[`, `unset arr[`, `declare a[`) is
// evaluated in arithmetic context by Bash/Zsh where single quotes do not
// suppress `$(...)` expansion.
stack.push({ kind: "subscript", bracketDepth: 0 });
continue;
}
if (ch === "'") {
stack.push("single");
stack.push({ kind: "single" });
continue;
}
if (ch === '"') {
stack.push("double");
stack.push({ kind: "double" });
continue;
}
if (ch === "`") {
stack.push("backtick");
stack.push({ kind: "backtick" });
continue;
}
if (ch === ")" && top === "cmdsubst") {
stack.pop();
if (ch === "(" && top?.kind === "cmdsubst") {
top.parenDepth += 1;
top.segmentStart = i + 1;
continue;
}
if (ch === ")" && top?.kind === "cmdsubst") {
if (top.parenDepth > 0) {
top.parenDepth -= 1;
} else {
stack.pop();
}
continue;
}
if (ch === ";" || ch === "|" || ch === "&" || ch === "\n" || ch === "(") {
if (top?.kind === "cmdsubst") {
top.segmentStart = i + 1;
} else if (top === undefined) {
rootSegmentStart = i + 1;
}
}
}
// Reject if any enclosing frame is an arithmetic, parameter-expansion, or
// array-subscript context (even when nested inside `$(...)`, whose output
// would still be re-evaluated by the enclosing arithmetic expression).
if (
stack.some(
(frame) =>
frame.kind === "arith"
|| frame.kind === "arithBracket"
|| frame.kind === "param"
|| frame.kind === "subscript",
)
) {
return false;
}
const top = stack[stack.length - 1];
return top === undefined || top === "cmdsubst";
if (top?.kind === "doubleBracket") {
const tail = draft.slice(top.startIdx);
if (POSIX_DOUBLE_BRACKET_ARITH_OP_RE.test(tail)) {
return false;
}
return true;
}
if (top !== undefined && top.kind !== "cmdsubst") {
return false;
}
// Bash/Zsh `let` evaluates all arguments as arithmetic expressions.
const segmentStart = top?.kind === "cmdsubst" ? top.segmentStart : rootSegmentStart;
const activeSegment = draft.slice(segmentStart);
if (POSIX_LET_COMMAND_RE.test(activeSegment)) {
return false;
}
return true;
}
function isFishDraftSafeForPathInsertion(draft: string): boolean {
@@ -467,6 +801,25 @@ export function isTerminalDraftContextSafeForPathInsertion(options: {
);
}
/**
* Validate the live terminal prompt + draft text before automatically
* submitting a `cd` command. Rejects secondary/continuation prompts (such as
* PS2 `>` or `dquote>`) where the shell is still reading an unterminated quote
* or compound command, as well as any non-empty draft input.
*/
export function isTerminalDraftContextSafeForCd(options: {
promptText?: string;
userInput: string;
pendingTypedInput?: string;
}): boolean {
if (options.promptText && isTerminalContinuationPrompt(options.promptText)) {
return false;
}
if (options.userInput.trim().length > 0) return false;
if ((options.pendingTypedInput ?? "").trim().length > 0) return false;
return true;
}
/** Build the PTY write payload for inserting a quoted path into the paired terminal. */
export function resolveInsertSftpPathInTerminalAction(
options: SftpTerminalPathContext & { path?: string | null },