From 935e164a6dbd95cb937988f61d30ca8efd5686af Mon Sep 17 00:00:00 2001 From: Mark Kraus Date: Mon, 6 May 2019 17:15:04 -0700 Subject: [PATCH] Add Support for Null Usernames in Web Cmdlet Basic Auth (#9536) --- .../Common/WebRequestPSCmdlet.Common.cs | 3 ++- .../WebCmdlets.Tests.ps1 | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 1d0649050..d082020bb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -805,7 +805,8 @@ namespace Microsoft.PowerShell.Commands private string GetBasicAuthorizationHeader() { - string unencoded = string.Format("{0}:{1}", Credential.UserName, Credential.GetNetworkCredential().Password); + var password = new NetworkCredential(null, Credential.Password).Password; + string unencoded = string.Format("{0}:{1}", Credential.UserName, password); byte[] bytes = Encoding.UTF8.GetBytes(unencoded); return string.Format("Basic {0}", Convert.ToBase64String(bytes)); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 9f69785cd..304732528 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1455,6 +1455,20 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $result.Headers.Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" } + It "Verifies Invoke-WebRequest -Authentication Basic with null username" { + $credential = [pscredential]::new([PSCustomObject]@{UserName = $null;Password=$token.psobject.BaseObject}) + $params = @{ + Uri = $httpsUri + Authentication = "Basic" + Credential = $credential + SkipCertificateCheck = $true + } + $Response = Invoke-WebRequest @params + $result = $response.Content | ConvertFrom-Json + + $result.Headers.Authorization | Should -BeExactly "Basic OnRlc3RwYXNzd29yZA==" + } + It "Verifies Invoke-WebRequest -Authentication " -TestCases $testCases { param($Authentication) $params = @{ @@ -2779,6 +2793,19 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $result.Headers.Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" } + It "Verifies Invoke-RestMethod -Authentication Basic with null username" { + $credential = [pscredential]::new([PSCustomObject]@{UserName = $null;Password=$token.psobject.BaseObject}) + $params = @{ + Uri = $httpsUri + Authentication = "Basic" + Credential = $credential + SkipCertificateCheck = $true + } + $Response = Invoke-RestMethod @params + + $Response.Headers.Authorization | Should -BeExactly "Basic OnRlc3RwYXNzd29yZA==" + } + It "Verifies Invoke-RestMethod -Authentication " -TestCases $testCases { param($Authentication) $params = @{