fix: fail explicit host ssh cwd

This commit is contained in:
Codex
2026-06-29 06:59:34 +00:00
parent 4d300d8888
commit 3cea6d96b7
3 changed files with 27 additions and 3 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@unidesk/provider-gateway",
"version": "0.2.31",
"version": "0.2.32",
"private": true,
"type": "module",
"scripts": {
@@ -6,6 +6,10 @@ port="${HOST_SSH_PORT}"
user="${HOST_SSH_USER}"
key="${HOST_SSH_KEY}"
fallback_cwd="${HOST_REMOTE_CWD}"
explicit_cwd=0
if [ "${UNIDESK_REQUESTED_CWD+x}" = x ]; then
explicit_cwd=1
fi
requested_cwd="${UNIDESK_REQUESTED_CWD:-$fallback_cwd}"
login_shell="${HOST_LOGIN_SHELL}"
@@ -16,7 +20,12 @@ quote() {
q_requested=$(quote "$requested_cwd")
q_fallback=$(quote "$fallback_cwd")
q_shell=$(quote "$login_shell")
remote_cmd="cd $q_requested 2>/dev/null || cd $q_fallback 2>/dev/null || cd; export UNIDESK_BRIDGE=host-ssh; exec $q_shell -l"
if [ "$explicit_cwd" = 1 ]; then
q_error=$(quote "UNIDESK_SSH_CWD_FAILED {\"code\":\"ssh-cwd-failed\",\"failureKind\":\"cwd-failed\",\"cwd\":\"$requested_cwd\",\"message\":\"requested host ssh cwd does not exist or is not accessible\"}")
remote_cmd="cd $q_requested 2>/dev/null || { printf '%s\n' $q_error >&2; exit 1; }; export UNIDESK_BRIDGE=host-ssh; exec $q_shell -l"
else
remote_cmd="cd $q_requested 2>/dev/null || cd $q_fallback 2>/dev/null || cd; export UNIDESK_BRIDGE=host-ssh; exec $q_shell -l"
fi
tty_arg="-T"
if [ -t 0 ] && [ -t 1 ]; then
tty_arg="-tt"
+16 -1
View File
@@ -1757,15 +1757,30 @@ async function runHostSsh(payload: Record<string, JsonValue>): Promise<JsonValue
};
}
function hostSshCwdFailureLine(cwd: string): string {
return `UNIDESK_SSH_CWD_FAILED ${JSON.stringify({
code: "ssh-cwd-failed",
failureKind: "cwd-failed",
cwd,
message: "requested host ssh cwd does not exist or is not accessible",
})}`;
}
function hostSshEnterCwdScript(requestedCwd: string, fallbackCwd: string, explicitCwd: boolean): string {
if (!explicitCwd) return `cd ${shellQuote(requestedCwd)} 2>/dev/null || cd ${shellQuote(fallbackCwd)} 2>/dev/null || cd`;
return `cd ${shellQuote(requestedCwd)} 2>/dev/null || { printf '%s\\n' ${shellQuote(hostSshCwdFailureLine(requestedCwd))} >&2; exit 1; }`;
}
function hostSshRemoteScript(command: string | null, cwd: string | null, allocateTty: boolean, cols?: number, rows?: number): string {
const fallbackCwd = config.hostRemoteCwd ?? `/home/${config.hostSshUser ?? "root"}`;
const explicitCwd = cwd !== null;
const requestedCwd = cwd ?? fallbackCwd;
if (command !== null && command.length > 0) assertHostSshCommandAllowed(command, requestedCwd);
const loginShell = config.hostLoginShell ?? "/bin/bash";
const resize = allocateTty && Number.isFinite(cols) && Number.isFinite(rows)
? `stty rows ${Math.max(8, Math.min(120, Math.floor(rows ?? 30)))} cols ${Math.max(20, Math.min(300, Math.floor(cols ?? 100)))} 2>/dev/null || true`
: "true";
const enterCwd = `cd ${shellQuote(requestedCwd)} 2>/dev/null || cd ${shellQuote(fallbackCwd)} 2>/dev/null || cd`;
const enterCwd = hostSshEnterCwdScript(requestedCwd, fallbackCwd, explicitCwd);
const exports = `export UNIDESK_BRIDGE=host.ssh; export UNIDESK_PROVIDER_ID=${shellQuote(config.providerId)}`;
const execPart = command === null || command.length === 0
? `exec ${shellQuote(loginShell)} -l`