Add tests for Allowinsecureredirect parameter in Web cmdlets (#18939)

This commit is contained in:
CarloToso
2023-01-13 17:37:55 +05:00
committed by GitHub
parent ca4b6162f4
commit f018beedff
2 changed files with 82 additions and 1 deletions
@@ -514,6 +514,26 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
}
It "Validate Invoke-WebRequest redirect with -Query destination Http" {
$httpUri = Get-WebListenerUrl -Test 'Get'
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @{destination = $httpUri}
$command = "Invoke-WebRequest -Uri '$uri'"
$result = ExecuteWebCommand -command $command
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.headers.Host | Should -Match $httpUri.Authority
}
It "Validate Invoke-WebRequest redirect with -Query destination Https" {
$httpsUri = Get-WebListenerUrl -Test 'Get' -Https
$uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpsUri}
$command = "Invoke-WebRequest -Uri '$uri' -SkipCertificateCheck"
$result = ExecuteWebCommand -command $command
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.headers.Host | Should -Match $httpsUri.Authority
}
It "Invoke-WebRequest supports request that returns page containing UTF-8 data." {
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8'
$command = "Invoke-WebRequest -Uri '$uri'"
@@ -937,6 +957,25 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$response.Error.Exception.Response.StatusCode | Should -Be $StatusCode
$response.Error.Exception.Response.Headers.Location | Should -BeNullOrEmpty
}
It "Validate Invoke-WebRequest Https to Http redirect with -AllowInsecureRedirect" {
$httpUri = Get-WebListenerUrl -Test 'Get'
$uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpUri}
$command = "Invoke-WebRequest -Uri '$uri' -SkipCertificateCheck -AllowInsecureRedirect"
$result = ExecuteWebCommand -command $command
$jsonContent = $result.Output.Content | ConvertFrom-Json
$jsonContent.headers.Host | Should -Match $httpUri.Authority
}
It "Validate Invoke-WebRequest Https to Http redirect without -AllowInsecureRedirect" {
$httpUri = Get-WebListenerUrl -Test 'Get'
$uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpUri}
$command = "Invoke-WebRequest -Uri '$uri' -SkipCertificateCheck"
$result = ExecuteWebCommand -command $command
$result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"
}
}
@@ -2170,6 +2209,24 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
}
It "Validate Invoke-RestMethod redirect with -Query destination Http" {
$httpUri = Get-WebListenerUrl -Test 'Get'
$uri = Get-WebListenerUrl -Test 'Redirect' -Query @{destination = $httpUri}
$command = "Invoke-RestMethod -Uri '$uri'"
$result = ExecuteWebCommand -command $command
$result.Output.Headers.Host | Should -Be $httpUri.Authority
}
It "Validate Invoke-RestMethod redirect with -Query destination Https" {
$httpsUri = Get-WebListenerUrl -Test 'Get' -Https
$uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpsUri}
$command = "Invoke-RestMethod -Uri '$uri' -SkipCertificateCheck"
$result = ExecuteWebCommand -command $command
$result.Output.Headers.Host | Should -Be $httpsUri.Authority
}
It "Invoke-RestMethod supports request that returns page containing UTF-8 data." {
$uri = Get-WebListenerUrl -Test 'Encoding' -TestValue 'Utf8'
$command = "Invoke-RestMethod -Uri '$uri'"
@@ -2595,6 +2652,24 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$response.Error.Exception.Response.Headers.Location | Should -BeNullOrEmpty
}
It "Validate Invoke-RestMethod Https to Http redirect with -AllowInsecureRedirect" {
$httpUri = Get-WebListenerUrl -Test 'Get'
$uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpUri}
$command = "Invoke-RestMethod -Uri '$uri' -SkipCertificateCheck -AllowInsecureRedirect"
$result = ExecuteWebCommand -command $command
$result.Output.Headers.Host | Should -Be $httpUri.Authority
}
It "Validate Invoke-RestMethod Https to Http redirect without -AllowInsecureRedirect" {
$httpUri = Get-WebListenerUrl -Test 'Get'
$uri = Get-WebListenerUrl -Test 'Redirect' -Https -Query @{destination = $httpUri}
$command = "Invoke-RestMethod -Uri '$uri' -SkipCertificateCheck"
$result = ExecuteWebCommand -command $command
$result.Error.FullyQualifiedErrorId | Should -Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
}
#endregion Redirect tests
Context "Invoke-RestMethod SkipHeaderVerification Tests" {
@@ -21,9 +21,10 @@ namespace mvc.Controllers
public IActionResult Index(int count)
{
string url = Regex.Replace(input: Request.GetDisplayUrl(), pattern: "\\/Redirect.*", replacement: string.Empty, options: RegexOptions.IgnoreCase);
var destinationIsPresent = Request.Query.TryGetValue("destination", out StringValues destination);
if (count <= 1)
{
url = $"{url}/Get/";
url = destinationIsPresent ? destination.FirstOrDefault() : $"{url}/Get/";
}
else
{
@@ -44,6 +45,11 @@ namespace mvc.Controllers
url = new Uri($"{url}?type={type.FirstOrDefault()}").PathAndQuery;
Response.Redirect(url, false);
}
else if (destinationIsPresent)
{
Response.StatusCode = 302;
Response.Headers.Add("Location", url);
}
else
{
Response.Redirect(url, false);