From 6571d1b5efe54f3adc909dc06795104281fe2f1d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 22 Feb 2023 02:16:28 +0100 Subject: [PATCH 01/24] fix bug --- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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 567a98ebfb9..a74e6a95e7a 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 @@ -107,6 +107,11 @@ public abstract class WebRequestPSCmdlet : PSCmdlet /// internal int _maximumFollowRelLink = int.MaxValue; + /// + /// Maximum number of Redirects to follow, caches WebSession.MaximumRedirection. + /// + internal int _maximumRedirection; + /// /// Parse Rel Links. /// @@ -549,6 +554,8 @@ protected override void ProcessRecord() WriteVerbose(reqVerboseMsg); + _maximumRedirection = WebSession.MaximumRedirection; + using HttpResponseMessage response = GetResponse(client, request, handleRedirect); string contentType = ContentHelper.GetContentType(response); @@ -633,7 +640,7 @@ protected override void ProcessRecord() // Errors with redirection counts of greater than 0 are handled automatically by .NET, but are // impossible to detect programmatically when we hit this limit. By handling this ourselves // (and still writing out the result), users can debug actual HTTP redirect problems. - if (WebSession.MaximumRedirection == 0 && IsRedirectCode(response.StatusCode)) + if (_maximumRedirection == 0 && IsRedirectCode(response.StatusCode)) { ErrorRecord er = new(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); @@ -1231,7 +1238,7 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM response = client.SendAsync(currentRequest, HttpCompletionOption.ResponseHeadersRead, _cancelToken.Token).GetAwaiter().GetResult(); if (handleRedirect - && WebSession.MaximumRedirection is not 0 + && _maximumRedirection is not 0 && IsRedirectCode(response.StatusCode) && response.Headers.Location is not null) { @@ -1239,9 +1246,9 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM _cancelToken = null; // If explicit count was provided, reduce it for this redirection. - if (WebSession.MaximumRedirection > 0) + if (_maximumRedirection > 0) { - WebSession.MaximumRedirection--; + _maximumRedirection--; } // For selected redirects, GET must be used with the redirected Location. From f61c070b0d4e1b57637b532011e95691d2e85494 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 26 Feb 2023 02:26:22 +0100 Subject: [PATCH 02/24] add tests --- .../WebCmdlets.Tests.ps1 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 2da345fcd3a..6c033182216 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -969,6 +969,14 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Content.Headers."Authorization" | Should -BeExactly "test" } + It "Validates Invoke-WebRequest with -WebSession, -PreserveAuthorizationOnRedirect and -MaximumRedirection doesn't change Websession.MaximumRedirection on multiple redirects: " -TestCases $redirectTests { + param($redirectType) + $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 3 -Query @{type = $redirectType} + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session + + $session.MaximumRedirection | Should -BeExactly 2 + } + It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { param($redirectType) $uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType} @@ -2706,6 +2714,14 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Content.Headers."Authorization" | Should -BeExactly "test" } + It "Validates Invoke-RestMethod with -WebSession, -PreserveAuthorizationOnRedirect and -MaximumRedirection doesn't change Websession.MaximumRedirection on multiple redirects: " -TestCases $redirectTests { + param($redirectType) + $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 3 -Query @{type = $redirectType} + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session + + $session.MaximumRedirection | Should -BeExactly 2 + } + It "Validates Invoke-RestMethod strips the authorization header on various redirects: " -TestCases $redirectTests { param($redirectType) $uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType} From 30e44cf81f74e258365197b96ab993dcf624ec51 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 26 Feb 2023 02:48:27 +0100 Subject: [PATCH 03/24] fix tests --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 6c033182216..3035b40c535 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -969,9 +969,9 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Content.Headers."Authorization" | Should -BeExactly "test" } - It "Validates Invoke-WebRequest with -WebSession, -PreserveAuthorizationOnRedirect and -MaximumRedirection doesn't change Websession.MaximumRedirection on multiple redirects: " -TestCases $redirectTests { + It "Validates Invoke-WebRequest with -WebSession, -PreserveAuthorizationOnRedirect and -MaximumRedirection doesn't change session.MaximumRedirection on multiple redirects: " -TestCases $redirectTests { param($redirectType) - $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 3 -Query @{type = $redirectType} + $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session $session.MaximumRedirection | Should -BeExactly 2 @@ -2714,9 +2714,9 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Content.Headers."Authorization" | Should -BeExactly "test" } - It "Validates Invoke-RestMethod with -WebSession, -PreserveAuthorizationOnRedirect and -MaximumRedirection doesn't change Websession.MaximumRedirection on multiple redirects: " -TestCases $redirectTests { + It "Validates Invoke-RestMethod with -WebSession, -PreserveAuthorizationOnRedirect and -MaximumRedirection doesn't change session.MaximumRedirection on multiple redirects: " -TestCases $redirectTests { param($redirectType) - $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 3 -Query @{type = $redirectType} + $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session $session.MaximumRedirection | Should -BeExactly 2 From 7337f24589df23489e2497b76c76ff3ee0a396c6 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 27 Feb 2023 10:26:42 +0100 Subject: [PATCH 04/24] add more tests --- .../WebCmdlets.Tests.ps1 | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 3035b40c535..e6a67b7ef3f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -969,12 +969,17 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Content.Headers."Authorization" | Should -BeExactly "test" } - It "Validates Invoke-WebRequest with -WebSession, -PreserveAuthorizationOnRedirect and -MaximumRedirection doesn't change session.MaximumRedirection on multiple redirects: " -TestCases $redirectTests { + It "Validates Invoke-WebRequest with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { param($redirectType) $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication $session.MaximumRedirection | Should -BeExactly 2 + $session2.MaximumRetryCount | Should -BeExactly 2 + $session2.RetryIntervalInSeconds | Should -BeExactly 2 + $session3.UseDefaultCredentials | Should -BeExactly $true } It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { @@ -2714,12 +2719,17 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Content.Headers."Authorization" | Should -BeExactly "test" } - It "Validates Invoke-RestMethod with -WebSession, -PreserveAuthorizationOnRedirect and -MaximumRedirection doesn't change session.MaximumRedirection on multiple redirects: " -TestCases $redirectTests { + It "Validates Invoke-RestMethod with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { param($redirectType) $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication $session.MaximumRedirection | Should -BeExactly 2 + $session2.MaximumRetryCount | Should -BeExactly 2 + $session2.RetryIntervalInSeconds | Should -BeExactly 2 + $session3.UseDefaultCredentials | Should -BeExactly $true } It "Validates Invoke-RestMethod strips the authorization header on various redirects: " -TestCases $redirectTests { From 7add56c17c88d7c17fdcecd6d546ac73c772ce6c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 27 Feb 2023 10:51:15 +0100 Subject: [PATCH 05/24] add session proxy test --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index e6a67b7ef3f..1a599bd4ec1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -617,11 +617,12 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { param($proxy_address, $name, $protocol) # use external url, but with proxy the external url should not actually be called - $command = "Invoke-WebRequest -Uri ${protocol}://httpbin.org -Proxy '${protocol}://${proxy_address}' -SkipCertificateCheck" + $command = "Invoke-WebRequest -Uri ${protocol}://httpbin.org -Proxy '${protocol}://${proxy_address}' -SkipCertificateCheck -SessionVariable session" $result = ExecuteWebCommand -command $command $command = "Invoke-WebRequest -Uri '${protocol}://${proxy_address}' -NoProxy" $expectedResult = ExecuteWebCommand -command $command $result.Output.Content | Should -BeExactly $expectedResult.Output.Content + $session.Proxy.GetProxy("${protocol}://httpbin.org").AbsoluteUri | Should -BeExactly "${protocol}://${proxy_address}" } # Perform the following operation for Invoke-WebRequest @@ -2393,11 +2394,12 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { param($proxy_address, $name, $protocol) # use external url, but with proxy the external url should not actually be called - $command = "Invoke-RestMethod -Uri ${protocol}://httpbin.org -Proxy '${protocol}://${proxy_address}'" + $command = "Invoke-RestMethod -Uri ${protocol}://httpbin.org -Proxy '${protocol}://${proxy_address}' -SessionVariable session" $result = ExecuteWebCommand -command $command $command = "Invoke-RestMethod -Uri '${protocol}://${proxy_address}' -NoProxy" $expectedResult = ExecuteWebCommand -command $command $result.Output | Should -BeExactly $expectedResult.Output + $session.Proxy.GetProxy("${protocol}://httpbin.org").AbsoluteUri | Should -BeExactly "${protocol}://${proxy_address}" } # Perform the following operation for Invoke-RestMethod From 5b1d2f461f3080d3324f4beb49d0a8a8c51d4c12 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 27 Feb 2023 23:39:39 +0100 Subject: [PATCH 06/24] revert --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 1a599bd4ec1..e88427e6315 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -617,12 +617,11 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { param($proxy_address, $name, $protocol) # use external url, but with proxy the external url should not actually be called - $command = "Invoke-WebRequest -Uri ${protocol}://httpbin.org -Proxy '${protocol}://${proxy_address}' -SkipCertificateCheck -SessionVariable session" + $command = "Invoke-WebRequest -Uri ${protocol}://httpbin.org -Proxy '${protocol}://${proxy_address}'" $result = ExecuteWebCommand -command $command $command = "Invoke-WebRequest -Uri '${protocol}://${proxy_address}' -NoProxy" $expectedResult = ExecuteWebCommand -command $command $result.Output.Content | Should -BeExactly $expectedResult.Output.Content - $session.Proxy.GetProxy("${protocol}://httpbin.org").AbsoluteUri | Should -BeExactly "${protocol}://${proxy_address}" } # Perform the following operation for Invoke-WebRequest @@ -2394,12 +2393,11 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { param($proxy_address, $name, $protocol) # use external url, but with proxy the external url should not actually be called - $command = "Invoke-RestMethod -Uri ${protocol}://httpbin.org -Proxy '${protocol}://${proxy_address}' -SessionVariable session" + $command = "Invoke-RestMethod -Uri ${protocol}://httpbin.org -Proxy '${protocol}://${proxy_address}'" $result = ExecuteWebCommand -command $command $command = "Invoke-RestMethod -Uri '${protocol}://${proxy_address}' -NoProxy" $expectedResult = ExecuteWebCommand -command $command $result.Output | Should -BeExactly $expectedResult.Output - $session.Proxy.GetProxy("${protocol}://httpbin.org").AbsoluteUri | Should -BeExactly "${protocol}://${proxy_address}" } # Perform the following operation for Invoke-RestMethod From 8c5b36568cef7da5ed1fad379550489f78225121 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 11:20:40 +0100 Subject: [PATCH 07/24] add session.Credentials test --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index e88427e6315..241e143f65d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -971,15 +971,20 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { It "Validates Invoke-WebRequest with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { param($redirectType) + $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force + $credential = [pscredential]::new("testuser", $token) $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication $session.MaximumRedirection | Should -BeExactly 2 $session2.MaximumRetryCount | Should -BeExactly 2 $session2.RetryIntervalInSeconds | Should -BeExactly 2 $session3.UseDefaultCredentials | Should -BeExactly $true + $session4.Credentials.UserName | Should -BeExactly $credential.UserName + $session4.Credentials.Password | Should -BeExactly $credential.Password } It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { @@ -2721,15 +2726,20 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { It "Validates Invoke-RestMethod with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { param($redirectType) + $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force + $credential = [pscredential]::new("testuser", $token) $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication $session.MaximumRedirection | Should -BeExactly 2 $session2.MaximumRetryCount | Should -BeExactly 2 $session2.RetryIntervalInSeconds | Should -BeExactly 2 $session3.UseDefaultCredentials | Should -BeExactly $true + $session4.Credentials.UserName | Should -BeExactly $credential.UserName + $session4.Credentials.Password | Should -BeExactly $credential.Password } It "Validates Invoke-RestMethod strips the authorization header on various redirects: " -TestCases $redirectTests { From e52ce622607c056228e5289bb6321f23d750251d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 11:49:38 +0100 Subject: [PATCH 08/24] fix test --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 241e143f65d..c4b59b10340 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -984,7 +984,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session2.RetryIntervalInSeconds | Should -BeExactly 2 $session3.UseDefaultCredentials | Should -BeExactly $true $session4.Credentials.UserName | Should -BeExactly $credential.UserName - $session4.Credentials.Password | Should -BeExactly $credential.Password + $session4.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password } It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { @@ -2739,7 +2739,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session2.RetryIntervalInSeconds | Should -BeExactly 2 $session3.UseDefaultCredentials | Should -BeExactly $true $session4.Credentials.UserName | Should -BeExactly $credential.UserName - $session4.Credentials.Password | Should -BeExactly $credential.Password + $session4.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password } It "Validates Invoke-RestMethod strips the authorization header on various redirects: " -TestCases $redirectTests { From ef4f5fef8a4cc3c23ca32bd333f94a95fb5605c1 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 11:53:49 +0100 Subject: [PATCH 09/24] codefactor --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index c4b59b10340..cf405d42062 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -971,6 +971,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { It "Validates Invoke-WebRequest with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { param($redirectType) + #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force $credential = [pscredential]::new("testuser", $token) $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} @@ -2726,6 +2727,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { It "Validates Invoke-RestMethod with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { param($redirectType) + #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force $credential = [pscredential]::new("testuser", $token) $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} From de55c0368c2d87b4f826e217285c2b770b232eda Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 12:16:15 +0100 Subject: [PATCH 10/24] add session.certificates test; fix headers --- .../WebCmdlets.Tests.ps1 | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index cf405d42062..125cfb75ee4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -974,11 +974,14 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force $credential = [pscredential]::new("testuser", $token) + $certificate = Get-WebListenerClientCertificate + $headers = @{"Authorization" = "test"} $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session -Headers $headers + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 -Headers $headers + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication -Headers $headers + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication -Headers $headers + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -Certificate $certificate -SessionVariable session5 -SkipCertificateCheck -Headers $headers $session.MaximumRedirection | Should -BeExactly 2 $session2.MaximumRetryCount | Should -BeExactly 2 @@ -986,7 +989,18 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session3.UseDefaultCredentials | Should -BeExactly $true $session4.Credentials.UserName | Should -BeExactly $credential.UserName $session4.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password + $session5.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint + } + + It "Verifies Invoke-RestMethod Certificate Authentication Successful with -Certificate" { + $uri = Get-WebListenerUrl -Https -Test 'Cert' + $certificate = Get-WebListenerClientCertificate + $result = Invoke-RestMethod -Uri $uri -Certificate $certificate -SkipCertificateCheck + + $result.Status | Should -Be 'OK' + $result.Thumbprint | Should -Be $certificate.Thumbprint } + } It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { param($redirectType) @@ -2730,11 +2744,14 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force $credential = [pscredential]::new("testuser", $token) + $certificate = Get-WebListenerClientCertificate + $headers = @{"Authorization" = "test"} $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session -Headers $headers + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 -Headers $headers + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication -Headers $headers + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication -Headers $headers + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Certificate $certificate -SessionVariable session5 -SkipCertificateCheck -Headers $headers $session.MaximumRedirection | Should -BeExactly 2 $session2.MaximumRetryCount | Should -BeExactly 2 @@ -2742,6 +2759,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session3.UseDefaultCredentials | Should -BeExactly $true $session4.Credentials.UserName | Should -BeExactly $credential.UserName $session4.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password + $session5.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint } It "Validates Invoke-RestMethod strips the authorization header on various redirects: " -TestCases $redirectTests { From 3f9f4a85b6933f5f76a49b8e88861760eedd9210 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 12:31:07 +0100 Subject: [PATCH 11/24] remove incorrectly pasted code --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 125cfb75ee4..a5eb7b8ddb3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -992,16 +992,6 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session5.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint } - It "Verifies Invoke-RestMethod Certificate Authentication Successful with -Certificate" { - $uri = Get-WebListenerUrl -Https -Test 'Cert' - $certificate = Get-WebListenerClientCertificate - $result = Invoke-RestMethod -Uri $uri -Certificate $certificate -SkipCertificateCheck - - $result.Status | Should -Be 'OK' - $result.Thumbprint | Should -Be $certificate.Thumbprint - } - } - It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { param($redirectType) $uri = Get-WebListenerUrl -Test 'Redirect' -Query @{type = $redirectType} From b438e17d069884e1d3c2acb1772ba71701322bb1 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 13:14:18 +0100 Subject: [PATCH 12/24] add test session.proxy --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index a5eb7b8ddb3..9a5f555de1c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -976,12 +976,14 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $credential = [pscredential]::new("testuser", $token) $certificate = Get-WebListenerClientCertificate $headers = @{"Authorization" = "test"} + $proxy = "http://127.0.0.1:8080" $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session -Headers $headers $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 -Headers $headers $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication -Headers $headers $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication -Headers $headers $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -Certificate $certificate -SessionVariable session5 -SkipCertificateCheck -Headers $headers + try { $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -Proxy $proxy -SessionVariable session6 -Headers $headers } catch {} $session.MaximumRedirection | Should -BeExactly 2 $session2.MaximumRetryCount | Should -BeExactly 2 @@ -990,6 +992,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session4.Credentials.UserName | Should -BeExactly $credential.UserName $session4.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password $session5.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint + $session6.Proxy.GetProxy($uri) | Should -BeExactly [uri]$proxy } It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { @@ -2736,12 +2739,14 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $credential = [pscredential]::new("testuser", $token) $certificate = Get-WebListenerClientCertificate $headers = @{"Authorization" = "test"} + $proxy = "http://127.0.0.1:8080" $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session -Headers $headers $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 -Headers $headers $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication -Headers $headers $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication -Headers $headers $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Certificate $certificate -SessionVariable session5 -SkipCertificateCheck -Headers $headers + try { $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Proxy $proxy -SessionVariable session6 -Headers $headers } catch {} $session.MaximumRedirection | Should -BeExactly 2 $session2.MaximumRetryCount | Should -BeExactly 2 @@ -2750,6 +2755,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session4.Credentials.UserName | Should -BeExactly $credential.UserName $session4.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password $session5.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint + $session6.Proxy.GetProxy($uri) | Should -BeExactly [uri]$proxy } It "Validates Invoke-RestMethod strips the authorization header on various redirects: " -TestCases $redirectTests { From 0e6d76425c6a7fdf32eb559f13e8a73001aa9414 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 18:52:55 +0100 Subject: [PATCH 13/24] [WebRequestSession]::New(); modify tests; try fix proxy test --- .../WebCmdlets.Tests.ps1 | 90 +++++++++++++------ 1 file changed, 64 insertions(+), 26 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 9a5f555de1c..fbc0283439e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -971,6 +971,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { It "Validates Invoke-WebRequest with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { param($redirectType) + #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force $credential = [pscredential]::new("testuser", $token) @@ -978,21 +979,39 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $headers = @{"Authorization" = "test"} $proxy = "http://127.0.0.1:8080" $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session -Headers $headers - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 -Headers $headers - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication -Headers $headers - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication -Headers $headers - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -Certificate $certificate -SessionVariable session5 -SkipCertificateCheck -Headers $headers - try { $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -Proxy $proxy -SessionVariable session6 -Headers $headers } catch {} + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.MaximumRedirection = 2 + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers $session.MaximumRedirection | Should -BeExactly 2 - $session2.MaximumRetryCount | Should -BeExactly 2 - $session2.RetryIntervalInSeconds | Should -BeExactly 2 - $session3.UseDefaultCredentials | Should -BeExactly $true - $session4.Credentials.UserName | Should -BeExactly $credential.UserName - $session4.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password - $session5.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint - $session6.Proxy.GetProxy($uri) | Should -BeExactly [uri]$proxy + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.MaximumRetryCount = 2 + $session.RetryIntervalInSeconds = 2 + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers + $session.MaximumRetryCount | Should -BeExactly 2 + $session.RetryIntervalInSeconds | Should -BeExactly 2 + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.UseDefaultCredentials = $true + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -AllowUnencryptedAuthentication -Headers $headers + $session.UseDefaultCredentials | Should -BeExactly $true + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.Credentials = $credential + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -AllowUnencryptedAuthentication -Headers $headers + $session.Credentials.UserName | Should -BeExactly $credential.UserName + $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.Certificates = $certificate + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers + $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.Proxy = $proxy + try { $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers } catch {} + $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy } It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { @@ -2734,6 +2753,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { It "Validates Invoke-RestMethod with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { param($redirectType) + #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] $token = "testpassword" | ConvertTo-SecureString -AsPlainText -Force $credential = [pscredential]::new("testuser", $token) @@ -2741,21 +2761,39 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $headers = @{"Authorization" = "test"} $proxy = "http://127.0.0.1:8080" $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRedirection 2 -SessionVariable session -Headers $headers - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -MaximumRetryCount 2 -RetryIntervalSec 2 -SessionVariable session2 -Headers $headers - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -UseDefaultCredentials -SessionVariable session3 -AllowUnencryptedAuthentication -Headers $headers - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Credential $credential -SessionVariable session4 -AllowUnencryptedAuthentication -Headers $headers - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Certificate $certificate -SessionVariable session5 -SkipCertificateCheck -Headers $headers - try { $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -Proxy $proxy -SessionVariable session6 -Headers $headers } catch {} + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.MaximumRedirection = 2 + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers $session.MaximumRedirection | Should -BeExactly 2 - $session2.MaximumRetryCount | Should -BeExactly 2 - $session2.RetryIntervalInSeconds | Should -BeExactly 2 - $session3.UseDefaultCredentials | Should -BeExactly $true - $session4.Credentials.UserName | Should -BeExactly $credential.UserName - $session4.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password - $session5.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint - $session6.Proxy.GetProxy($uri) | Should -BeExactly [uri]$proxy + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.MaximumRetryCount = 2 + $session.RetryIntervalInSeconds = 2 + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers + $session.MaximumRetryCount | Should -BeExactly 2 + $session.RetryIntervalInSeconds | Should -BeExactly 2 + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.UseDefaultCredentials = $true + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -AllowUnencryptedAuthentication -Headers $headers + $session.UseDefaultCredentials | Should -BeExactly $true + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.Credentials = $credential + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -AllowUnencryptedAuthentication -Headers $headers + $session.Credentials.UserName | Should -BeExactly $credential.UserName + $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.Certificates = $certificate + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers + $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session.Proxy = $proxy + try { $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers } catch {} + $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy } It "Validates Invoke-RestMethod strips the authorization header on various redirects: " -TestCases $redirectTests { From 143c6b4f61c5983357f05a5449cfd868ae772c86 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 19:23:32 +0100 Subject: [PATCH 14/24] certificates --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index fbc0283439e..ca62e53c532 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1004,7 +1004,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() - $session.Certificates = $certificate + $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::New([X509Certificate]$certificate) $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint @@ -2786,7 +2786,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() - $session.Certificates = $certificate + $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::New([X509Certificate]$certificate) $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint From ceaf473237c5f6eb45ce8d53a69f1f4ce479ca80 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 28 Feb 2023 19:49:52 +0100 Subject: [PATCH 15/24] try fix proxy --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index ca62e53c532..502f4fc09e1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1009,7 +1009,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() - $session.Proxy = $proxy + $session.Proxy = [System.Net.WebProxy]::New($proxy) try { $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers } catch {} $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy } @@ -2791,7 +2791,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() - $session.Proxy = $proxy + $session.Proxy = [System.Net.WebProxy]::New($proxy) try { $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers } catch {} $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy } From 37de41304d280c21bdf87f64b95558c038c5686c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 10:16:05 +0100 Subject: [PATCH 16/24] Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs Co-authored-by: Ilya --- .../utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 2 +- 1 file changed, 1 insertion(+), 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 9b40547155c..c0b237fb256 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 @@ -108,7 +108,7 @@ public abstract class WebRequestPSCmdlet : PSCmdlet internal int _maximumFollowRelLink = int.MaxValue; /// - /// Maximum number of Redirects to follow, caches WebSession.MaximumRedirection. + /// Maximum number of Redirects to follow. /// internal int _maximumRedirection; From ed4ca1439badf5d1772747075dd57fd7bb159739 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 10:23:17 +0100 Subject: [PATCH 17/24] merge tests --- .../WebCmdlets.Tests.ps1 | 48 +++++-------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 502f4fc09e1..eb6fa04b483 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -982,35 +982,23 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() $session.MaximumRedirection = 2 - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers - $session.MaximumRedirection | Should -BeExactly 2 - - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() $session.MaximumRetryCount = 2 $session.RetryIntervalInSeconds = 2 - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers - $session.MaximumRetryCount | Should -BeExactly 2 - $session.RetryIntervalInSeconds | Should -BeExactly 2 - - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() $session.UseDefaultCredentials = $true $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -AllowUnencryptedAuthentication -Headers $headers + $session.MaximumRedirection | Should -BeExactly 2 + $session.MaximumRetryCount | Should -BeExactly 2 + $session.RetryIntervalInSeconds | Should -BeExactly 2 $session.UseDefaultCredentials | Should -BeExactly $true $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() $session.Credentials = $credential - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -AllowUnencryptedAuthentication -Headers $headers + $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::New([X509Certificate]$certificate) + $session.Proxy = [System.Net.WebProxy]::New($proxy) + try { $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers } catch {} $session.Credentials.UserName | Should -BeExactly $credential.UserName $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password - - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() - $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::New([X509Certificate]$certificate) - $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint - - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() - $session.Proxy = [System.Net.WebProxy]::New($proxy) - try { $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers } catch {} $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy } @@ -2764,35 +2752,23 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() $session.MaximumRedirection = 2 - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers - $session.MaximumRedirection | Should -BeExactly 2 - - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() $session.MaximumRetryCount = 2 $session.RetryIntervalInSeconds = 2 - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers - $session.MaximumRetryCount | Should -BeExactly 2 - $session.RetryIntervalInSeconds | Should -BeExactly 2 - - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() $session.UseDefaultCredentials = $true $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -AllowUnencryptedAuthentication -Headers $headers + $session.MaximumRedirection | Should -BeExactly 2 + $session.MaximumRetryCount | Should -BeExactly 2 + $session.RetryIntervalInSeconds | Should -BeExactly 2 $session.UseDefaultCredentials | Should -BeExactly $true $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() $session.Credentials = $credential - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -AllowUnencryptedAuthentication -Headers $headers + $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::New([X509Certificate]$certificate) + $session.Proxy = [System.Net.WebProxy]::New($proxy) + try { $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers } catch {} $session.Credentials.UserName | Should -BeExactly $credential.UserName $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password - - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() - $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::New([X509Certificate]$certificate) - $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint - - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() - $session.Proxy = [System.Net.WebProxy]::New($proxy) - try { $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers } catch {} $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy } From 02d9d9e68bb77da224f20f696f93d5e1eb966e6d Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 10:24:45 +0100 Subject: [PATCH 18/24] title --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index eb6fa04b483..f0724bad8f8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -969,7 +969,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Content.Headers."Authorization" | Should -BeExactly "test" } - It "Validates Invoke-WebRequest with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { + It "Validates Invoke-WebRequest with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session variable on multiple redirects: " -TestCases $redirectTests { param($redirectType) #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] @@ -2739,7 +2739,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $response.Content.Headers."Authorization" | Should -BeExactly "test" } - It "Validates Invoke-RestMethod with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session on multiple redirects: " -TestCases $redirectTests { + It "Validates Invoke-RestMethod with -WebSession and -PreserveAuthorizationOnRedirect doesn't change session variable on multiple redirects: " -TestCases $redirectTests { param($redirectType) #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")] From e55285038a22eff32155baec00dea5d8c7a6a6de Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 13:17:17 +0100 Subject: [PATCH 19/24] N -> n --- .../WebCmdlets.Tests.ps1 | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index f0724bad8f8..d141fa78156 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -980,7 +980,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $proxy = "http://127.0.0.1:8080" $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.MaximumRedirection = 2 $session.MaximumRetryCount = 2 $session.RetryIntervalInSeconds = 2 @@ -991,10 +991,10 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session.RetryIntervalInSeconds | Should -BeExactly 2 $session.UseDefaultCredentials | Should -BeExactly $true - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.Credentials = $credential - $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::New([X509Certificate]$certificate) - $session.Proxy = [System.Net.WebProxy]::New($proxy) + $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::new([X509Certificate]$certificate) + $session.Proxy = [System.Net.WebProxy]::new($proxy) try { $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers } catch {} $session.Credentials.UserName | Should -BeExactly $credential.UserName $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password @@ -2750,7 +2750,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $proxy = "http://127.0.0.1:8080" $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.MaximumRedirection = 2 $session.MaximumRetryCount = 2 $session.RetryIntervalInSeconds = 2 @@ -2761,10 +2761,10 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session.RetryIntervalInSeconds | Should -BeExactly 2 $session.UseDefaultCredentials | Should -BeExactly $true - $session = [Microsoft.PowerShell.Commands.WebRequestSession]::New() + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.Credentials = $credential - $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::New([X509Certificate]$certificate) - $session.Proxy = [System.Net.WebProxy]::New($proxy) + $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::new([X509Certificate]$certificate) + $session.Proxy = [System.Net.WebProxy]::new($proxy) try { $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers } catch {} $session.Credentials.UserName | Should -BeExactly $credential.UserName $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password From ded4f009cf7b4e0b2a81032a8999869d1e0b4022 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 16:41:11 +0100 Subject: [PATCH 20/24] remove try-catch --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index d141fa78156..b208e8ae34b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -995,7 +995,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session.Credentials = $credential $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::new([X509Certificate]$certificate) $session.Proxy = [System.Net.WebProxy]::new($proxy) - try { $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers } catch {} + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers $session.Credentials.UserName | Should -BeExactly $credential.UserName $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint From 3a5cdf0154887c7e07eb45970a5a6c0aaef24e5a Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 17:26:07 +0100 Subject: [PATCH 21/24] follow suggestion --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index b208e8ae34b..8ba5a00e6ab 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -994,11 +994,14 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.Credentials = $credential $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::new([X509Certificate]$certificate) - $session.Proxy = [System.Net.WebProxy]::new($proxy) $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers $session.Credentials.UserName | Should -BeExactly $credential.UserName $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() + $session.Proxy = [System.Net.WebProxy]::new($proxy) + $null = Invoke-WebRequest -Uri http://httpbin.org -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy } From a3180e38bdb2531ec71526cefa3061cbb8421859 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 17:54:31 +0100 Subject: [PATCH 22/24] change proxy --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 8ba5a00e6ab..159c0e38588 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -977,7 +977,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $credential = [pscredential]::new("testuser", $token) $certificate = Get-WebListenerClientCertificate $headers = @{"Authorization" = "test"} - $proxy = "http://127.0.0.1:8080" + $proxy = (Get-WebListenerUrl).Authority $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() @@ -2750,7 +2750,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $credential = [pscredential]::new("testuser", $token) $certificate = Get-WebListenerClientCertificate $headers = @{"Authorization" = "test"} - $proxy = "http://127.0.0.1:8080" + $proxy = (Get-WebListenerUrl).Authority $uri = Get-WebListenerUrl -Test 'Redirect' -TestValue 2 -Query @{type = $redirectType} $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() From f6a9e1c9494b2970d6c56fb3cf7f307e4a6338ea Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 18:24:20 +0100 Subject: [PATCH 23/24] fix tests --- .../Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 159c0e38588..0936623769a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1002,7 +1002,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.Proxy = [System.Net.WebProxy]::new($proxy) $null = Invoke-WebRequest -Uri http://httpbin.org -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers - $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy + $session.Proxy.GetProxy($uri).Authority | Should -BeExactly $proxy } It "Validates Invoke-WebRequest strips the authorization header on various redirects: " -TestCases $redirectTests { @@ -2767,12 +2767,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.Credentials = $credential $session.Certificates = [System.Security.Cryptography.X509Certificates.X509CertificateCollection]::new([X509Certificate]$certificate) - $session.Proxy = [System.Net.WebProxy]::new($proxy) - try { $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers } catch {} + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -SkipCertificateCheck -Headers $headers $session.Credentials.UserName | Should -BeExactly $credential.UserName $session.Credentials.Password | Should -BeExactly $credential.GetNetworkCredential().Password $session.Certificates.Thumbprint | Should -BeExactly $certificate.Thumbprint - $session.Proxy.GetProxy($uri).OriginalString | Should -BeExactly $proxy + + $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() + $session.Proxy = [System.Net.WebProxy]::new($proxy) + $null = Invoke-RestMethod -Uri http://httpbin.org -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers + $session.Proxy.GetProxy($uri).Authority | Should -BeExactly $proxy } It "Validates Invoke-RestMethod strips the authorization header on various redirects: " -TestCases $redirectTests { From 4b3ee4718f20301378ed31fbc760edc7819f2cc2 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 1 Mar 2023 19:29:23 +0100 Subject: [PATCH 24/24] use $uri --- .../Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 0936623769a..d78f57141c6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -1001,7 +1001,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.Proxy = [System.Net.WebProxy]::new($proxy) - $null = Invoke-WebRequest -Uri http://httpbin.org -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers + $null = Invoke-WebRequest -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers $session.Proxy.GetProxy($uri).Authority | Should -BeExactly $proxy } @@ -2774,7 +2774,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $session.Proxy = [System.Net.WebProxy]::new($proxy) - $null = Invoke-RestMethod -Uri http://httpbin.org -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers + $null = Invoke-RestMethod -Uri $uri -PreserveAuthorizationOnRedirect -WebSession $session -Headers $headers $session.Proxy.GetProxy($uri).Authority | Should -BeExactly $proxy }