diff --git a/docs/specs/cli-config-sources.md b/docs/specs/cli-config-sources.md index 705baa3b05d..9fc0d05c584 100644 --- a/docs/specs/cli-config-sources.md +++ b/docs/specs/cli-config-sources.md @@ -145,7 +145,7 @@ When the CLI host supports interactive input, fetch with inherited terminal inpu Unattended configuration-source Git operations (including opted-in JSON listing, non-interactive `new`/`init`, and advisory checks) override `core.askPass` only for the child process and disable `GIT_ASKPASS`, `SSH_ASKPASS`, terminal prompts and Git Credential Manager interaction. Standard input receives EOF. This also applies when `--non-interactive`, `ASPIRE_NON_INTERACTIVE`, or CI disables host input. Credential helpers remain enabled for cached credentials; custom credential helpers must themselves run without prompting. Interactive Git acquisition and captured template-repository acquisition retain their existing authentication behavior. -Unattended SSH resolves the selected executable through Git's effective child `PATH`, including Git for Windows' bundled executable directories, rather than substituting an OpenSSH client found on the CLI's own `PATH`. It verifies that executable and supplies `BatchMode=yes`, preserving configured identity files, known hosts and agent authentication. Unknown host keys and unavailable credentials fail rather than prompt. Bare OpenSSH executable selections through `GIT_SSH`, `GIT_SSH_COMMAND` or `core.sshCommand` are supported, but custom command strings with arguments, shell expansions or wrappers fail with an actionable diagnostic instead of being silently overwritten or receiving potentially incompatible OpenSSH options. Move identity/host options into OpenSSH configuration or use interactive acquisition for those commands. Custom credential helpers, SSH agents and proxy commands are not sandboxed and must already be unattended. Git URL rewrites are resolved before selecting this SSH policy, so HTTPS selectors rewritten to SSH receive the same protection. No user Git configuration is modified. +Unattended SSH resolves the selected executable through Git's effective child `PATH`, including Git for Windows' bundled executable directories, rather than substituting an OpenSSH client found on the CLI's own `PATH`. It verifies that executable and supplies `BatchMode=yes`, preserving configured identity files, known hosts and agent authentication. The version probe allows slow process startup within a 30-second limit and honors the enclosing operation's cancellation budget. Unknown host keys and unavailable credentials fail rather than prompt. Bare OpenSSH executable selections through `GIT_SSH`, `GIT_SSH_COMMAND` or `core.sshCommand` are supported, but custom command strings with arguments, shell expansions or wrappers fail with an actionable diagnostic instead of being silently overwritten or receiving potentially incompatible OpenSSH options. Move identity/host options into OpenSSH configuration or use interactive acquisition for those commands. Custom credential helpers, SSH agents and proxy commands are not sandboxed and must already be unattended. Git URL rewrites are resolved before selecting this SSH policy, so HTTPS selectors rewritten to SSH receive the same protection. No user Git configuration is modified. If a non-interactive fetch blocks waiting for a custom authentication helper, the 30-second timeout terminates its process tree and reports the timeout. Capture output without truncation, including prompts without a terminating newline, and display the last 64 Ki characters of the combined fetch output on stderr so the user can diagnose and correct authentication. Local query results remain complete. Do not display captured configuration-file contents or copy these diagnostics into exception messages, logs, or telemetry. User cancellation and shutdown cancellation do not display captured output. Advisory timeout output is deferred with the warning until command exit and does not change the foreground command's exit code. diff --git a/src/Aspire.Cli/Git/GitCommandRunner.cs b/src/Aspire.Cli/Git/GitCommandRunner.cs index 5654f45701f..713996e8c25 100644 --- a/src/Aspire.Cli/Git/GitCommandRunner.cs +++ b/src/Aspire.Cli/Git/GitCommandRunner.cs @@ -269,8 +269,17 @@ internal static string GetSshExecutable(string command) probe.FileName = executable; probe.ArgumentList.Clear(); probe.ArgumentList.Add("-V"); + + return await VerifyOpenSshAsync(probe, cancellationToken) ? executable : null; + } + + internal async Task VerifyOpenSshAsync(ProcessStartInfo probe, CancellationToken cancellationToken) + { + var stopwatch = Stopwatch.StartNew(); + // Allow slow OpenSSH initialization on busy Windows runners without exceeding + // the caller's acquisition deadline or leaving an unbounded probe. var result = await ProcessCaptureRunner.RunAsync( - probe, TimeSpan.FromSeconds(5), + probe, TimeSpan.FromSeconds(30), async (process, token) => { process.StandardInput.Close(); @@ -280,9 +289,11 @@ internal static string GetSshExecutable(string command) return (await stdout + await stderr).Trim(); }, () => null, NullLogger.Instance, cancellationToken); + logger.LogDebug("OpenSSH verification completed after {Elapsed}; exit code {ExitCode}, failure {Failure}, cancelled {Cancelled}.", + stopwatch.Elapsed, result.ExitCode, result.FailureKind, result.Cancelled); cancellationToken.ThrowIfCancellationRequested(); return !result.Cancelled && result.FailureKind is null && result.ExitCode == 0 - && result.Capture?.StartsWith("OpenSSH_", StringComparison.Ordinal) is true ? executable : null; + && result.Capture?.StartsWith("OpenSSH_", StringComparison.Ordinal) is true; } internal static string NormalizeSshPath(string path, string workingDirectory) diff --git a/tests/Aspire.Cli.Tests/Git/GitCommandRunnerTests.Authentication.cs b/tests/Aspire.Cli.Tests/Git/GitCommandRunnerTests.Authentication.cs index c0fb84324f3..930069420e9 100644 --- a/tests/Aspire.Cli.Tests/Git/GitCommandRunnerTests.Authentication.cs +++ b/tests/Aspire.Cli.Tests/Git/GitCommandRunnerTests.Authentication.cs @@ -232,6 +232,42 @@ public async Task ConfigureUnattendedSsh_VerifiesOpenSshAndAppliesBatchModeAfter Assert.Equal(configuration, await File.ReadAllTextAsync(config)); } + [Fact] + public async Task VerifyOpenSshAsync_AllowsSlowStartup() + { + var probe = CreateShellStartInfo( + "sleep 6; printf 'OpenSSH_test' >&2", + "Start-Sleep -Seconds 6; [Console]::Error.Write('OpenSSH_test')"); + probe.RedirectStandardInput = true; + + Assert.True(await CreateRunner().VerifyOpenSshAsync(probe, TestContext.Current.CancellationToken)); + } + + [Theory] + [InlineData("OpenSSH_test", 0, true)] + [InlineData("OpenSSH_test", 1, false)] + [InlineData("not OpenSSH", 0, false)] + public async Task VerifyOpenSshAsync_RequiresSuccessfulOpenSshBanner(string banner, int exitCode, bool expected) + { + var probe = CreateShellStartInfo( + $"printf '%s' '{banner}' >&2; exit {exitCode}", + $"[Console]::Error.Write('{banner}'); exit {exitCode}"); + probe.RedirectStandardInput = true; + + Assert.Equal(expected, await CreateRunner().VerifyOpenSshAsync(probe, TestContext.Current.CancellationToken)); + } + + [Fact] + public async Task VerifyOpenSshAsync_HonorsCancellation() + { + var probe = CreateShellStartInfo("sleep 60", "Start-Sleep -Seconds 60"); + probe.RedirectStandardInput = true; + using var cancellation = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken); + cancellation.CancelAfter(TimeSpan.FromMilliseconds(100)); + + await Assert.ThrowsAnyAsync(() => CreateRunner().VerifyOpenSshAsync(probe, cancellation.Token)); + } + [Fact] [RequiresTools(["git", "ssh"])] public async Task ConfigureUnattendedSsh_RelativePathEntriesUseGitDirectoryBeforeAbsoluteFallback()