Skip to content

ServerCertificateValidationCallback has no effect on Invoke-WebRequest #1753

@snobu

Description

@snobu

As stated in Example 2 here,

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }

has no effect on Invoke-WebRequest or Invoke-RestMethod, establishing the TLS session will still fail if the remote presents a self-signed certificate.

PS> [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
PS> Invoke-RestMethod -URI https://selfsigned.badssl.com

Invoke-RestMethod : The underlying connection was closed:
An unexpected error occurred on a send.

At line:1 char:1
+ invoke-restmethod -uri https://selfsigned.badssl.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest)
[Invoke-RestMethod],WebException    + FullyQualifiedErrorId : WebCmdletWebResponseException,
Microsoft.PowerShell.Commands.InvokeRestMethodCommand
PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.15063.138
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.138
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Looks like it only works when calling straight into the assembly:

$apiReq = [System.Net.HttpWebRequest]::CreateHttp($url)
$apiRes = $apiReq.GetResponse()

The TLS handshake is successful now.

This is not kosher, definitely needs to be addressed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions