From bd4e5598cea4756cf2eecb1bc1c2cb53443d7b32 Mon Sep 17 00:00:00 2001 From: Paul Higinbotham Date: Thu, 28 Oct 2021 10:39:55 -0700 Subject: [PATCH] Fix for SSH remoting when banner is enabled on SSHD endpoint (#16205) --- .../ExperimentalFeature.cs | 4 +++ .../fanin/OutOfProcTransportManager.cs | 34 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs b/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs index cb0be3193a..5695d54806 100644 --- a/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs +++ b/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs @@ -23,6 +23,7 @@ namespace System.Management.Automation internal const string EngineSource = "PSEngine"; internal const string PSNativeCommandArgumentPassingFeatureName = "PSNativeCommandArgumentPassing"; internal const string PSNativeCommandErrorActionPreferenceFeatureName = "PSNativeCommandErrorActionPreference"; + internal const string PSRemotingSSHTransportErrorHandling = "PSRemotingSSHTransportErrorHandling"; internal const string PSCleanBlockFeatureName = "PSCleanBlock"; #endregion @@ -127,6 +128,9 @@ namespace System.Management.Automation new ExperimentalFeature( name: PSNativeCommandErrorActionPreferenceFeatureName, description: "Native commands with non-zero exit codes issue errors according to $ErrorActionPreference when $PSNativeCommandUseErrorActionPreference is $true"), + new ExperimentalFeature( + name: PSRemotingSSHTransportErrorHandling, + description: "Removes the SSH remoting transport stdErr stream message handling as terminating errors, and instead just writes error messages to console."), new ExperimentalFeature( name: PSCleanBlockFeatureName, description: "Add support of a 'Clean' block to functions and script cmdlets for easy resource cleanup"), diff --git a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs index 75f7094185..038ea9620e 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs @@ -1762,7 +1762,39 @@ namespace System.Management.Automation.Remoting.Client while (true) { - string error = ReadError(reader); + string error; + + if (ExperimentalFeature.IsEnabled(ExperimentalFeature.PSRemotingSSHTransportErrorHandling)) + { + // Blocking read from StdError stream + error = reader.ReadLine(); + + if (error == null) + { + // Stream is closed unexpectedly. + throw new PSInvalidOperationException(RemotingErrorIdStrings.SSHAbruptlyTerminated); + } + + if (error.Length == 0) + { + // Ignore + continue; + } + + try + { + // Messages in error stream from ssh are unreliable, and may just be warnings or + // banner text. + // So just report the messages but don't act on them. + System.Console.WriteLine(error); + } + catch (IOException) + { } + + continue; + } + + error = ReadError(reader); if (error.Length == 0) {