-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(ssh): survive csh/tcsh login shells when running multiline relay commands #8714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xianjianlf2
wants to merge
1
commit into
stablyai:main
Choose a base branch
from
xianjianlf2:fix/ssh-csh-login-shell-multiline-8701
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { spawnSync } from 'node:child_process' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { wrapRemoteCommandForPosixShell } from './ssh-connection-utils' | ||
|
|
||
| // Why: sshd hands the wrapped command string to the user's login shell with | ||
| // `-c`, so running `<shell> -c <wrapped>` locally reproduces the remote parse | ||
| // exactly — including the csh/tcsh multiline re-parse failure from #8701. | ||
| const LOGIN_SHELLS = ['/bin/sh', '/bin/bash', '/bin/zsh', '/bin/dash', '/bin/csh', '/bin/tcsh'] | ||
| const availableShells = | ||
| process.platform === 'win32' | ||
| ? [] | ||
| : LOGIN_SHELLS.filter((shell) => spawnSync(shell, ['-c', 'exit 0']).status === 0) | ||
|
|
||
| function runViaLoginShell( | ||
| loginShell: string, | ||
| wrapped: string, | ||
| input?: string | ||
| ): { stdout: string; status: number | null } { | ||
| const result = spawnSync(loginShell, ['-c', wrapped], { | ||
| encoding: 'utf8', | ||
| ...(input !== undefined ? { input } : {}), | ||
| timeout: 5000 | ||
| }) | ||
| expect(result.error).toBeUndefined() | ||
| return { stdout: result.stdout, status: result.status } | ||
| } | ||
|
|
||
| describe('wrapRemoteCommandForPosixShell', () => { | ||
| it('keeps single-line commands in the plain /bin/sh -c form', () => { | ||
| expect(wrapRemoteCommandForPosixShell('echo "${SHELL:-/bin/sh}"')).toBe( | ||
| `exec /bin/sh -c 'echo "\${SHELL:-/bin/sh}"'` | ||
| ) | ||
| }) | ||
|
|
||
| it('emits a single-line wrapper for multiline scripts', () => { | ||
| const wrapped = wrapRemoteCommandForPosixShell('echo one\necho two\n') | ||
| expect(wrapped).not.toContain('\n') | ||
| expect(wrapped.startsWith('exec /bin/sh -c ')).toBe(true) | ||
| }) | ||
|
|
||
| describe.each(availableShells)('under a %s login shell', (loginShell) => { | ||
| it('runs a multiline script with quotes, backslashes, and expansions intact', () => { | ||
| const script = [ | ||
| 'echo START', | ||
| `name='it'\\''s %s here'`, | ||
| 'for cand in "$HOME" /nonexistent', | ||
| 'do', | ||
| ' [ -e "$cand" ] && echo "found: $cand \\\\ $name"', | ||
| 'done', | ||
| 'echo END' | ||
| ].join('\n') | ||
|
|
||
| const { stdout, status } = runViaLoginShell( | ||
| loginShell, | ||
| wrapRemoteCommandForPosixShell(script) | ||
| ) | ||
| expect(status).toBe(0) | ||
| expect(stdout).toContain('START') | ||
| expect(stdout).toContain(`found: ${process.env.HOME} \\ it's %s here`) | ||
| expect(stdout).toContain('END') | ||
| }) | ||
|
|
||
| it('propagates the script exit code', () => { | ||
| const { status } = runViaLoginShell( | ||
| loginShell, | ||
| wrapRemoteCommandForPosixShell('echo one\nexit 42\n') | ||
| ) | ||
| expect(status).toBe(42) | ||
| }) | ||
|
|
||
| it('leaves stdin available to the script', () => { | ||
| const { stdout, status } = runViaLoginShell( | ||
| loginShell, | ||
| wrapRemoteCommandForPosixShell('read line\necho "got: $line"\n'), | ||
| 'stream-me\n' | ||
| ) | ||
| expect(status).toBe(0) | ||
| expect(stdout).toContain('got: stream-me') | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use Node path utilities instead of assuming path separators.
As per coding guidelines, use Node path utilities for file paths and never assume
/or\. Since this function operates on remote POSIX shell paths,path.posix.basename()is the proper utility and correctly handles edge cases (like trailing slashes) that manual splitting might miss.Note: Please ensure
pathis imported at the top of the file (e.g.,import path from 'node:path') if it isn't already.♻️ Proposed refactor
📝 Committable suggestion
Source: Coding guidelines