From 022e0b0aa2f6d6b7fd8b9e2f312d3782e196e758 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sun, 27 Dec 2020 15:34:31 -0800 Subject: [PATCH 1/4] Fix PromptForCredential() to add `targetName` as domain --- .../host/msh/ConsoleHostUserInterfaceSecurity.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs index 67b4fc6a531..ecbccae1724 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs @@ -109,6 +109,11 @@ public override PSCredential PromptForCredential( WriteLineToConsole(); + if (!string.IsNullOrEmpty(targetName)) + { + userName = StringUtil.Format("{0}\\{1}", targetName, userName); + } + cred = new PSCredential(userName, password); return cred; From 9058f64d49b53f64cb0c4cde214aa800f221e135 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sun, 27 Dec 2020 17:50:07 -0800 Subject: [PATCH 2/4] add test --- .../msh/ConsoleHostUserInterfaceSecurity.cs | 25 ++++++++++++------- .../engine/Utils.cs | 1 + test/powershell/Host/HostUtilities.Tests.ps1 | 20 +++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs index ecbccae1724..7f5291094d3 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs @@ -97,17 +97,24 @@ public override PSCredential PromptForCredential( passwordPrompt = StringUtil.Format(ConsoleHostUserInterfaceSecurityResources.PromptForCredential_Password, userName ); - // - // now, prompt for the password - // - WriteToConsole(passwordPrompt, true); - password = ReadLineAsSecureString(); - if (password == null) + if (!InternalTestHooks.NoPromptForPassword) { - return null; - } + // + // now, prompt for the password + // + WriteToConsole(passwordPrompt, true); + password = ReadLineAsSecureString(); + if (password == null) + { + return null; + } - WriteLineToConsole(); + WriteLineToConsole(); + } + else + { + password = new SecureString(); + } if (!string.IsNullOrEmpty(targetName)) { diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index 6a4a863d23c..74184e9e847 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -2072,6 +2072,7 @@ public static class InternalTestHooks internal static bool BypassOnlineHelpRetrieval; internal static bool ForcePromptForChoiceDefaultOption; internal static bool BypassOutputRedirectionCheck; + internal static bool NoPromptForPassword; // Stop/Restart/Rename Computer tests internal static bool TestStopComputer; diff --git a/test/powershell/Host/HostUtilities.Tests.ps1 b/test/powershell/Host/HostUtilities.Tests.ps1 index 6c791b5c446..a886891080b 100644 --- a/test/powershell/Host/HostUtilities.Tests.ps1 +++ b/test/powershell/Host/HostUtilities.Tests.ps1 @@ -58,3 +58,23 @@ Describe "InvokeOnRunspace method on remote runspace" -tags "Feature","RequireAd $results[0] | Should -Be "Hello!" } } + +Describe 'PromptForCredential' { + BeforeAll { + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $true) + } + + AfterAll { + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $false) + } + + It 'Should accept no targetname' { + $out = $Host.UI.PromptForCredential('caption','message','myUser',$null) + $out.UserName | Should -BeExactly 'myUser' + } + + It 'Should accept targetname as domain' { + $out = $Host.UI.PromptForCredential('caption','message','myUser','myDomain') + $out.UserName | Should -BeExactly 'myDomain\myUser' + } +} From af685c12657698dee876602caadadbf1ba9fe6e5 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 5 Mar 2021 17:32:20 -0800 Subject: [PATCH 3/4] Update src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs Co-authored-by: Aditya Patwardhan --- .../host/msh/ConsoleHostUserInterfaceSecurity.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs index 7f5291094d3..b3071fece7b 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs @@ -102,7 +102,7 @@ public override PSCredential PromptForCredential( // // now, prompt for the password // - WriteToConsole(passwordPrompt, true); + WriteToConsole(passwordPrompt, transcribeResult:true); password = ReadLineAsSecureString(); if (password == null) { From 7015cde7a41bf2ba030c8fb639ba14a1eb68badd Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 5 Mar 2021 17:48:54 -0800 Subject: [PATCH 4/4] address Codefactor --- .../host/msh/ConsoleHostUserInterfaceSecurity.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs index b3071fece7b..1e934553f18 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs @@ -99,10 +99,7 @@ public override PSCredential PromptForCredential( if (!InternalTestHooks.NoPromptForPassword) { - // - // now, prompt for the password - // - WriteToConsole(passwordPrompt, transcribeResult:true); + WriteToConsole(passwordPrompt, transcribeResult: true); password = ReadLineAsSecureString(); if (password == null) {