From aa7dc2515c9e3743a7df410992ece8ef0b970826 Mon Sep 17 00:00:00 2001 From: davinci Date: Sat, 26 Oct 2019 17:51:23 -0700 Subject: [PATCH 01/12] Adds Mask Input Parameter to Read-Host --- .../commands/utility/ReadConsoleCmdlet.cs | 30 ++++- .../resources/ReadHostStrings.resx | 123 ++++++++++++++++++ .../host/msh/CommandLineParameterParser.cs | 9 ++ .../host/msh/ConsoleHostUserInterface.cs | 34 +++++ .../hostifaces/InternalHostUserInterface.cs | 39 ++++++ .../engine/hostifaces/MshHostUserInterface.cs | 25 ++++ .../server/ServerRemoteHostUserInterface.cs | 8 ++ .../Read-Host.Tests.ps1 | 12 ++ .../Modules/HelpersHostCS/HelpersHostCS.psm1 | 5 + 9 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.PowerShell.Commands.Utility/resources/ReadHostStrings.resx diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs index 49af92fffd4..1a4d44e524b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs @@ -55,9 +55,8 @@ public sealed class ReadHostCommand : PSCmdlet } /// - /// Set to no echo the input as is is typed. + /// Set to no echo the input as is is typed. Returns a secure string /// - [Parameter] public SwitchParameter @@ -73,6 +72,18 @@ public sealed class ReadHostCommand : PSCmdlet _safe = value; } } + + /// + /// Set to no echo the input as is is typed. Returns a regular string + /// + [Parameter] + public + SwitchParameter + MaskInput + { + get; + set; + } #endregion Parameters #region Cmdlet Overrides @@ -144,11 +155,26 @@ protected override void BeginProcessing() } else { + if (this.MyInvocation.BoundParameters.ContainsKey(nameof(AsSecureString)) && this.MyInvocation.BoundParameters.ContainsKey(nameof(MaskInput))) + { + InvalidOperationException exception = new InvalidOperationException(ReadHostStrings.CannotSpecifyAsSecureStringAndMaskInput); + var errorRecord = new ErrorRecord( + exception, + "CannotSpecifyAsSecureStringAndMaskInput", + ErrorCategory.InvalidOperation, + targetObject: null); + + ThrowTerminatingError(errorRecord); + } object result; if (AsSecureString) { result = Host.UI.ReadLineAsSecureString(); } + else if (MaskInput) + { + result = Host.UI.ReadLineMaskedAsString(); + } else { result = Host.UI.ReadLine(); diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/ReadHostStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/ReadHostStrings.resx new file mode 100644 index 00000000000..b473afe170f --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/ReadHostStrings.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + You must specify either the -AsSecureString or -MaskInput parameters, but not both. + + diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 02cb00c52ce..bdef4367b5b 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -96,6 +96,15 @@ public override string ReadLine() throw new PSNotImplementedException(); } + /// + /// ReadLineMaskedAsString. + /// + /// + public override string ReadLineMaskedAsString() + { + throw new PSNotImplementedException(); + } + /// /// ReadLineAsSecureString. /// diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 3862df694e9..ff9d6589182 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -174,6 +174,40 @@ public override string ReadLine() return ReadLine(false, string.Empty, out unused, true, true); } + /// + /// See base class + /// + /// + /// + /// If obtaining a handle to the active screen buffer failed + /// OR + /// Win32's setting input buffer mode to disregard window and mouse input failed + /// OR + /// Win32's ReadConsole failed + /// + /// + /// If Ctrl-C is entered by user + /// + public override string ReadLineMaskedAsString() + { + HandleThrowOnReadAndPrompt(); + + const char printToken = '*'; // This is not localizable + + // we lock here so that multiple threads won't interleave the various reads and writes here. + + object result = null; + lock (_instanceLock) + { + result = ReadLineSafe(false, printToken); + } + + StringBuilder resultSb = result as StringBuilder; + System.Management.Automation.Diagnostics.Assert(resultSb != null, "ReadLineMaskedAsString did not return a stringBuilder"); + + return resultSb.ToString(); + } + /// /// See base class. /// diff --git a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs index acfd0b5a1ed..68f0cd2e62d 100644 --- a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs @@ -120,6 +120,45 @@ public override return result; } + /// + /// See base class. + /// + /// + /// if the UI property of the external host is null, possibly because the PSHostUserInterface is not + /// implemented by the external host + /// + public override + string + ReadLineMaskedAsString() + { + if (_externalUI == null) + { + ThrowNotInteractive(); + } + + string result = null; + + try + { + result = _externalUI.ReadLineMaskedAsString(); + } + catch (PipelineStoppedException) + { + // PipelineStoppedException is thrown by host when it wants + // to stop the pipeline. + LocalPipeline lpl = (LocalPipeline)((RunspaceBase)_parent.Context.CurrentRunspace).GetCurrentlyRunningPipeline(); + if (lpl == null) + { + throw; + } + + lpl.Stopper.Stop(); + } + + return result; + + } + /// /// See base class. /// diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs index f553b520bed..0371aa87ba0 100644 --- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs @@ -59,6 +59,31 @@ public abstract System.Management.Automation.Host.PSHostRawUserInterface RawUI /// /// public abstract string ReadLine(); + + /// + /// Same as ReadLine except that the input is not echoed to the user while it is collected + /// or is echoed in some obfuscated way, such as showing a dot for each character. + /// + /// + /// The characters typed by the user. + /// + /// + /// Note that credentials (a user name and password) should be gathered with + /// + /// + /// + /// + /// + /// + /// + /// + + public virtual string ReadLineMaskedAsString() + { + // Default implementation of the function to maintain backwards compatibility of the base class. + throw new PSNotImplementedException(); + } + /// /// Same as ReadLine, except that the result is a SecureString, and that the input is not echoed to the user while it is /// collected (or is echoed in some obfuscated way, such as showing a dot for each character). diff --git a/src/System.Management.Automation/engine/remoting/server/ServerRemoteHostUserInterface.cs b/src/System.Management.Automation/engine/remoting/server/ServerRemoteHostUserInterface.cs index 694876f8c32..3c797203744 100644 --- a/src/System.Management.Automation/engine/remoting/server/ServerRemoteHostUserInterface.cs +++ b/src/System.Management.Automation/engine/remoting/server/ServerRemoteHostUserInterface.cs @@ -197,6 +197,14 @@ public override void WriteWarningLine(string message) _serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteWarningLine, new object[] { message }); } + /// + /// Read line as string masked. + /// + public override string ReadLineMaskedAsString() + { + throw new PSNotImplementedException(); + } + /// /// Read line as secure string. /// diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 index 702b6b0596b..7116ea13622 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 @@ -38,6 +38,18 @@ Describe "Read-Host Test" -tag "CI" { [pscredential]::New("foo",$result).GetNetworkCredential().Password | Should -BeExactly TEST } + It "Read-Host returns a string when using -MaskInput parameter" { + $result = $ps.AddScript("Read-Host -MaskInput").Invoke() + $result | Should -Be $th.UI.ReadLineData + } + + It "Read-Host throws an error when both -AsSecureString parameter and -MaskInput parameter are used" { + # Contrary to the rest of the tests this does not need to be invoked through a runspace since it is going to throw an error. + $errorId = "CannotSpecifyAsSecureStringAndMaskInput,Microsoft.PowerShell.Commands.ReadHostCommand" + {Read-Host -MaskInput -AsSecureString}| Should -Throw -ErrorId $errorId + } + + It "Read-Host doesn't enter command prompt mode" { $result = "!1" | pwsh -NoProfile -c "Read-host -Prompt 'foo'" if ($IsWindows) { diff --git a/test/tools/Modules/HelpersHostCS/HelpersHostCS.psm1 b/test/tools/Modules/HelpersHostCS/HelpersHostCS.psm1 index 45e4db5b844..240a4a3a09b 100755 --- a/test/tools/Modules/HelpersHostCS/HelpersHostCS.psm1 +++ b/test/tools/Modules/HelpersHostCS/HelpersHostCS.psm1 @@ -164,6 +164,11 @@ namespace TestHost return ReadLineData; } + public override string ReadLineMaskedAsString() + { + return ReadLineData; + } + public override SecureString ReadLineAsSecureString() { SecureString ss = new SecureString(); From 7f21ad7e82a2ff5f2e632ba984285c873eb0331b Mon Sep 17 00:00:00 2001 From: davinci Date: Sun, 27 Oct 2019 13:26:42 -0700 Subject: [PATCH 02/12] ParameterSetNames + Code Styling --- .../commands/utility/ReadConsoleCmdlet.cs | 21 +-- .../resources/ReadHostStrings.resx | 123 ------------------ .../host/msh/CommandLineParameterParser.cs | 6 +- .../hostifaces/InternalHostUserInterface.cs | 3 +- .../server/ServerRemoteHostUserInterface.cs | 3 + .../Read-Host.Tests.ps1 | 2 +- 6 files changed, 14 insertions(+), 144 deletions(-) delete mode 100644 src/Microsoft.PowerShell.Commands.Utility/resources/ReadHostStrings.resx diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs index 1a4d44e524b..035c7339904 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs @@ -17,7 +17,7 @@ namespace Microsoft.PowerShell.Commands /// Retrieves input from the host virtual console and writes it to the pipeline output. /// - [Cmdlet(VerbsCommunications.Read, "Host", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113371")] + [Cmdlet(VerbsCommunications.Read, "Host", DefaultParameterSetName = "AsString", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113371")] [OutputType(typeof(string), typeof(SecureString))] public sealed class ReadHostCommand : PSCmdlet { @@ -55,9 +55,9 @@ public sealed class ReadHostCommand : PSCmdlet } /// - /// Set to no echo the input as is is typed. Returns a secure string + /// Set to no echo the input as is is typed. Returns a secure string. /// - [Parameter] + [Parameter(ParameterSetName = "AsSecureString")] public SwitchParameter AsSecureString @@ -74,9 +74,9 @@ public sealed class ReadHostCommand : PSCmdlet } /// - /// Set to no echo the input as is is typed. Returns a regular string + /// Set to no echo the input as is is typed. Returns a regular string. /// - [Parameter] + [Parameter(ParameterSetName = "AsString")] public SwitchParameter MaskInput @@ -155,17 +155,6 @@ protected override void BeginProcessing() } else { - if (this.MyInvocation.BoundParameters.ContainsKey(nameof(AsSecureString)) && this.MyInvocation.BoundParameters.ContainsKey(nameof(MaskInput))) - { - InvalidOperationException exception = new InvalidOperationException(ReadHostStrings.CannotSpecifyAsSecureStringAndMaskInput); - var errorRecord = new ErrorRecord( - exception, - "CannotSpecifyAsSecureStringAndMaskInput", - ErrorCategory.InvalidOperation, - targetObject: null); - - ThrowTerminatingError(errorRecord); - } object result; if (AsSecureString) { diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/ReadHostStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/ReadHostStrings.resx deleted file mode 100644 index b473afe170f..00000000000 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/ReadHostStrings.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - You must specify either the -AsSecureString or -MaskInput parameters, but not both. - - diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index bdef4367b5b..ebdae9b428c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -97,9 +97,11 @@ public override string ReadLine() } /// - /// ReadLineMaskedAsString. + /// Null implementation of ReadLineMaskedAsString. /// - /// + /// + /// It throws an exception + /// public override string ReadLineMaskedAsString() { throw new PSNotImplementedException(); diff --git a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs index 68f0cd2e62d..fe886293dca 100644 --- a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs @@ -125,7 +125,7 @@ public override /// /// /// if the UI property of the external host is null, possibly because the PSHostUserInterface is not - /// implemented by the external host + /// implemented by the external host. /// public override string @@ -156,7 +156,6 @@ public override } return result; - } /// diff --git a/src/System.Management.Automation/engine/remoting/server/ServerRemoteHostUserInterface.cs b/src/System.Management.Automation/engine/remoting/server/ServerRemoteHostUserInterface.cs index 3c797203744..18b2aa8d250 100644 --- a/src/System.Management.Automation/engine/remoting/server/ServerRemoteHostUserInterface.cs +++ b/src/System.Management.Automation/engine/remoting/server/ServerRemoteHostUserInterface.cs @@ -200,6 +200,9 @@ public override void WriteWarningLine(string message) /// /// Read line as string masked. /// + /// + /// Not implemented. It throws an exception. + /// public override string ReadLineMaskedAsString() { throw new PSNotImplementedException(); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 index 7116ea13622..1ddb86d7992 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 @@ -45,7 +45,7 @@ Describe "Read-Host Test" -tag "CI" { It "Read-Host throws an error when both -AsSecureString parameter and -MaskInput parameter are used" { # Contrary to the rest of the tests this does not need to be invoked through a runspace since it is going to throw an error. - $errorId = "CannotSpecifyAsSecureStringAndMaskInput,Microsoft.PowerShell.Commands.ReadHostCommand" + $errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.ReadHostCommand" {Read-Host -MaskInput -AsSecureString}| Should -Throw -ErrorId $errorId } From 66548de7008662db8c2d4d2c9a3cea06a63fdf1a Mon Sep 17 00:00:00 2001 From: davinci Date: Sun, 27 Oct 2019 14:31:36 -0700 Subject: [PATCH 03/12] Documentation and styling --- .../commands/utility/ReadConsoleCmdlet.cs | 4 ++-- .../host/msh/CommandLineParameterParser.cs | 4 ++-- .../engine/hostifaces/InternalHostUserInterface.cs | 2 +- .../engine/hostifaces/MshHostUserInterface.cs | 1 - 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs index 035c7339904..819c8c7d4b5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs @@ -55,7 +55,7 @@ public sealed class ReadHostCommand : PSCmdlet } /// - /// Set to no echo the input as is is typed. Returns a secure string. + /// Set to no echo the input as is is typed. If set then the cmdlet returns a secure string. /// [Parameter(ParameterSetName = "AsSecureString")] public @@ -74,7 +74,7 @@ public sealed class ReadHostCommand : PSCmdlet } /// - /// Set to no echo the input as is is typed. Returns a regular string. + /// Gets or Sets whether the console will echo the input as is is typed. If set then the cmdlet returns a regular string. /// [Parameter(ParameterSetName = "AsString")] public diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index ebdae9b428c..9b01e66256f 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -100,8 +100,8 @@ public override string ReadLine() /// Null implementation of ReadLineMaskedAsString. /// /// - /// It throws an exception - /// + /// It throws an exception. + /// public override string ReadLineMaskedAsString() { throw new PSNotImplementedException(); diff --git a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs index fe886293dca..b14452ac431 100644 --- a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs @@ -124,7 +124,7 @@ public override /// See base class. /// /// - /// if the UI property of the external host is null, possibly because the PSHostUserInterface is not + /// If the UI property of the external host is null, possibly because the PSHostUserInterface is not /// implemented by the external host. /// public override diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs index 0371aa87ba0..649d7a07078 100644 --- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs @@ -77,7 +77,6 @@ public abstract System.Management.Automation.Host.PSHostRawUserInterface RawUI /// /// /// - public virtual string ReadLineMaskedAsString() { // Default implementation of the function to maintain backwards compatibility of the base class. From 3fdcc5cb28823a4ffe38f702323fee76b270f293 Mon Sep 17 00:00:00 2001 From: Sotiris Nanopoulos Date: Tue, 29 Oct 2019 12:55:23 -0700 Subject: [PATCH 04/12] PR Comments --- .../host/msh/ConsoleHostUserInterface.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index ff9d6589182..8249b4c8416 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -29,6 +29,12 @@ namespace Microsoft.PowerShell [SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")] internal partial class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInterface { + + /// + /// This is the char that is echoed to the console when the input is masked. This not localizable + /// + private const char printToken = '*'; + /// /// Command completion implementation object. /// @@ -192,8 +198,6 @@ public override string ReadLineMaskedAsString() { HandleThrowOnReadAndPrompt(); - const char printToken = '*'; // This is not localizable - // we lock here so that multiple threads won't interleave the various reads and writes here. object result = null; @@ -227,8 +231,6 @@ public override SecureString ReadLineAsSecureString() { HandleThrowOnReadAndPrompt(); - const char printToken = '*'; // This is not localizable - // we lock here so that multiple threads won't interleave the various reads and writes here. object result = null; From e01f3c2834c2f2a67ac51db510736ebb35005fd8 Mon Sep 17 00:00:00 2001 From: Sotiris Nanopoulos Date: Wed, 30 Oct 2019 09:04:30 -0700 Subject: [PATCH 05/12] added underscore infront of private variable --- .../host/msh/ConsoleHostUserInterface.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 8249b4c8416..bfb10b118e1 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -33,7 +33,7 @@ internal partial class ConsoleHostUserInterface : System.Management.Automation.H /// /// This is the char that is echoed to the console when the input is masked. This not localizable /// - private const char printToken = '*'; + private const char _printToken = '*'; /// /// Command completion implementation object. From 32b24adac86aedd599ad13e44e0846651fdea42c Mon Sep 17 00:00:00 2001 From: Sotiris Nanopoulos Date: Wed, 30 Oct 2019 09:05:32 -0700 Subject: [PATCH 06/12] added underscore infront of private variable --- .../host/msh/ConsoleHostUserInterface.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index bfb10b118e1..67eb2cb07c1 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -203,7 +203,7 @@ public override string ReadLineMaskedAsString() object result = null; lock (_instanceLock) { - result = ReadLineSafe(false, printToken); + result = ReadLineSafe(false, _printToken); } StringBuilder resultSb = result as StringBuilder; @@ -236,7 +236,7 @@ public override SecureString ReadLineAsSecureString() object result = null; lock (_instanceLock) { - result = ReadLineSafe(true, printToken); + result = ReadLineSafe(true, _printToken); } SecureString secureResult = result as SecureString; From 338694dc2a14b5ffa6f24b9bdcd49aea942563e5 Mon Sep 17 00:00:00 2001 From: Sotiris Nanopoulos Date: Wed, 30 Oct 2019 11:53:00 -0700 Subject: [PATCH 07/12] Capitalize first character of const member variable --- .../host/msh/ConsoleHostUserInterface.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 67eb2cb07c1..8a71388d8a8 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -33,7 +33,7 @@ internal partial class ConsoleHostUserInterface : System.Management.Automation.H /// /// This is the char that is echoed to the console when the input is masked. This not localizable /// - private const char _printToken = '*'; + private const char PrintToken = '*'; /// /// Command completion implementation object. @@ -203,7 +203,7 @@ public override string ReadLineMaskedAsString() object result = null; lock (_instanceLock) { - result = ReadLineSafe(false, _printToken); + result = ReadLineSafe(false, PrintToken); } StringBuilder resultSb = result as StringBuilder; @@ -236,7 +236,7 @@ public override SecureString ReadLineAsSecureString() object result = null; lock (_instanceLock) { - result = ReadLineSafe(true, _printToken); + result = ReadLineSafe(true, PrintToken); } SecureString secureResult = result as SecureString; From 9616a67429c6ac52f938b1f22050d6833e3fd119 Mon Sep 17 00:00:00 2001 From: Sotiris Nanopoulos Date: Thu, 31 Oct 2019 17:49:49 -0700 Subject: [PATCH 08/12] Addressing comments by iSazonov --- .../commands/utility/ReadConsoleCmdlet.cs | 4 ++-- .../host/msh/ConsoleHostUserInterface.cs | 15 ++++++++------- .../hostifaces/InternalHostUserInterface.cs | 2 +- .../Read-Host.Tests.ps1 | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs index 819c8c7d4b5..8489b8b9570 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ReadConsoleCmdlet.cs @@ -55,7 +55,7 @@ public sealed class ReadHostCommand : PSCmdlet } /// - /// Set to no echo the input as is is typed. If set then the cmdlet returns a secure string. + /// Gets or sets to no echo the input as is is typed. If set then the cmdlet returns a secure string. /// [Parameter(ParameterSetName = "AsSecureString")] public @@ -74,7 +74,7 @@ public sealed class ReadHostCommand : PSCmdlet } /// - /// Gets or Sets whether the console will echo the input as is is typed. If set then the cmdlet returns a regular string. + /// Gets or sets whether the console will echo the input as is is typed. If set then the cmdlet returns a regular string. /// [Parameter(ParameterSetName = "AsString")] public diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 8a71388d8a8..a2ddfe533ea 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -31,7 +31,7 @@ internal partial class ConsoleHostUserInterface : System.Management.Automation.H { /// - /// This is the char that is echoed to the console when the input is masked. This not localizable + /// This is the char that is echoed to the console when the input is masked. This not localizable. /// private const char PrintToken = '*'; @@ -181,25 +181,26 @@ public override string ReadLine() } /// - /// See base class + /// See base class. /// - /// + /// + /// The characters typed by the user. + /// /// /// If obtaining a handle to the active screen buffer failed /// OR - /// Win32's setting input buffer mode to disregard window and mouse input failed + /// Win32's setting input buffer mode to disregard window and mouse input failed. /// OR - /// Win32's ReadConsole failed + /// Win32's ReadConsole failed. /// /// - /// If Ctrl-C is entered by user + /// If Ctrl-C is entered by user. /// public override string ReadLineMaskedAsString() { HandleThrowOnReadAndPrompt(); // we lock here so that multiple threads won't interleave the various reads and writes here. - object result = null; lock (_instanceLock) { diff --git a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs index b14452ac431..c1800075fda 100644 --- a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs @@ -88,7 +88,7 @@ public override bool SupportsVirtualTerminal /// /// /// if the UI property of the external host is null, possibly because the PSHostUserInterface is not - /// implemented by the external host + /// implemented by the external host /// public override string diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 index 1ddb86d7992..6146fb6e4e9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 @@ -46,7 +46,7 @@ Describe "Read-Host Test" -tag "CI" { It "Read-Host throws an error when both -AsSecureString parameter and -MaskInput parameter are used" { # Contrary to the rest of the tests this does not need to be invoked through a runspace since it is going to throw an error. $errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.ReadHostCommand" - {Read-Host -MaskInput -AsSecureString}| Should -Throw -ErrorId $errorId + {Read-Host -MaskInput -AsSecureString} | Should -Throw -ErrorId $errorId } From 5ab178a6892baf7c1eb81072353b835cf28ae729 Mon Sep 17 00:00:00 2001 From: Sotiris Nanopoulos Date: Thu, 31 Oct 2019 18:06:53 -0700 Subject: [PATCH 09/12] More styling :) --- .../engine/hostifaces/InternalHostUserInterface.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs index c1800075fda..ae73fd39364 100644 --- a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs @@ -123,6 +123,9 @@ public override /// /// See base class. /// + /// + /// The characters typed by the user. + /// /// /// If the UI property of the external host is null, possibly because the PSHostUserInterface is not /// implemented by the external host. From 58df8f45d0d60274d5c238560bdacb286de5d23f Mon Sep 17 00:00:00 2001 From: Sotiris Nanopoulos Date: Thu, 31 Oct 2019 18:12:32 -0700 Subject: [PATCH 10/12] Use internal Dbg --- .../host/msh/ConsoleHostUserInterface.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index a2ddfe533ea..dc4aea815d9 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -208,7 +208,7 @@ public override string ReadLineMaskedAsString() } StringBuilder resultSb = result as StringBuilder; - System.Management.Automation.Diagnostics.Assert(resultSb != null, "ReadLineMaskedAsString did not return a stringBuilder"); + Dbg.Assert(resultSb != null, "ReadLineMaskedAsString did not return a stringBuilder"); return resultSb.ToString(); } From 198b33a4a8ee1e69fdb23b1a242458cafdb5fb7a Mon Sep 17 00:00:00 2001 From: davinci Date: Thu, 31 Oct 2019 21:22:15 -0700 Subject: [PATCH 11/12] styling --- .../engine/hostifaces/InternalHostUserInterface.cs | 6 +++--- .../Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs index ae73fd39364..5b58fce543f 100644 --- a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs @@ -88,7 +88,7 @@ public override bool SupportsVirtualTerminal /// /// /// if the UI property of the external host is null, possibly because the PSHostUserInterface is not - /// implemented by the external host + /// implemented by the external host. /// public override string @@ -128,7 +128,7 @@ public override /// /// /// If the UI property of the external host is null, possibly because the PSHostUserInterface is not - /// implemented by the external host. + /// implemented by the external host. /// public override string @@ -166,7 +166,7 @@ public override /// /// /// if the UI property of the external host is null, possibly because the PSHostUserInterface is not - /// implemented by the external host + /// implemented by the external host. /// public override diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 index 6146fb6e4e9..20e992ddc9e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 @@ -49,7 +49,6 @@ Describe "Read-Host Test" -tag "CI" { {Read-Host -MaskInput -AsSecureString} | Should -Throw -ErrorId $errorId } - It "Read-Host doesn't enter command prompt mode" { $result = "!1" | pwsh -NoProfile -c "Read-host -Prompt 'foo'" if ($IsWindows) { From 54db27ddaa8f4a9b5fc3c1e3ab0dcfbde99b8e8d Mon Sep 17 00:00:00 2001 From: davinci Date: Sat, 7 Dec 2019 12:32:30 -0800 Subject: [PATCH 12/12] Retry CI