-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Closed
xianjianlf2
wants to merge
1
commit into
stablyai:main
from
xianjianlf2:fix/ssh-csh-login-shell-multiline-8701
Closed
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.
Uh oh!
There was an error while loading. Please reload this page.