Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public abstract partial class WebRequestPSCmdlet : PSCmdlet
[ValidateNotNull]
public virtual X509Certificate Certificate { get; set; }

/// <summary>
/// gets or sets the SkipCertificateCheck property
/// </summary>
[Parameter]
public virtual SwitchParameter SkipCertificateCheck { get; set; }

#endregion

#region Headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ internal virtual HttpClient GetHttpClient()
handler.ClientCertificates = WebSession.Certificates;
}*/

if (SkipCertificateCheck)
{
handler.ServerCertificateCustomValidationCallback = delegate { return true; };
}

if (WebSession.MaximumRedirection > -1)
{
if (WebSession.MaximumRedirection == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
$jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell"
}

It "Validate Invoke-WebRequest -SkipCertificateCheck" {

# validate that exception is thrown for URI with expired certificate
$command = "Invoke-WebRequest -Uri 'https://expired.badssl.com'"
$result = ExecuteWebCommand -command $command
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand"

# validate that no exception is thrown for URI with expired certificate when using -SkipCertificateCheck option
$command = "Invoke-WebRequest -Uri 'https://expired.badssl.com' -SkipCertificateCheck"
$result = ExecuteWebCommand -command $command
$result.Error | Should BeNullOrEmpty
}

}

Describe "Invoke-RestMethod tests" -Tags "Feature" {
Expand Down Expand Up @@ -593,4 +606,19 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
$jsonContent.headers.Host | Should Match "httpbin.org"
$jsonContent.headers.'User-Agent' | Should Match "WindowsPowerShell"
}

It "Validate Invoke-RestMethod -SkipCertificateCheck" {

# HTTP method HEAD must be used to not retrieve an unparsable HTTP body
# validate that exception is thrown for URI with expired certificate
$command = "Invoke-RestMethod -Uri 'https://expired.badssl.com' -Method HEAD"
$result = ExecuteWebCommand -command $command
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"

# validate that no exception is thrown for URI with expired certificate when using -SkipCertificateCheck option
$command = "Invoke-RestMethod -Uri 'https://expired.badssl.com' -SkipCertificateCheck -Method HEAD"
$result = ExecuteWebCommand -command $command
$result.Error | Should BeNullOrEmpty
}

}