diff --git a/src/components/provider-gateway/package.json b/src/components/provider-gateway/package.json index bafc518c..3aea6591 100644 --- a/src/components/provider-gateway/package.json +++ b/src/components/provider-gateway/package.json @@ -1,6 +1,6 @@ { "name": "@unidesk/provider-gateway", - "version": "0.2.31", + "version": "0.2.32", "private": true, "type": "module", "scripts": { diff --git a/src/components/provider-gateway/scripts/host-ssh-shell.sh b/src/components/provider-gateway/scripts/host-ssh-shell.sh index 3a12d410..dca34883 100755 --- a/src/components/provider-gateway/scripts/host-ssh-shell.sh +++ b/src/components/provider-gateway/scripts/host-ssh-shell.sh @@ -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" diff --git a/src/components/provider-gateway/src/index.ts b/src/components/provider-gateway/src/index.ts index 04389c7e..014dcdfe 100644 --- a/src/components/provider-gateway/src/index.ts +++ b/src/components/provider-gateway/src/index.ts @@ -1757,15 +1757,30 @@ async function runHostSsh(payload: Record): Promise/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`