Skip to content

Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects from HTTPS #2896

Closed

Description

@aburlak

Steps to reproduce

  1. Install Windows Server Nano (10.0.14393)
  2. Connect to the server using PS Remoting, then use PowerShell Core (the built-in version) to download a file, e.g. try to download .NET Core 1.1 ZIP package:

Invoke-Webrequest -Uri 'https://go.microsoft.com/fwlink/?LinkID=835028' -OutFile C:\netcore11.zip -MaximumRedirection 10

Expected behavior

File is downloaded, HTTP 302 Redirect returned by go.microsoft.com is handled properly.

Actual behavior

The following error occurs:
invoke-webrequest : Response status code does not indicate success: 302 (Moved Temporarily).
And there is no workaround, because the thrown exception is a HttpRequestException thrown by EnsureSuccessStatusCode(), which loses all header information (so, unable even to access the Location header).

Issue is still present in the master branch:
https://github.com/PowerShell/PowerShell/blob/master/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs#L352
EnsureSuccessStatusCode() should not be used, as it doesn't support redirects, and it also obfuscates the exception, making it impossible to access the returned headers/body even if the exception is caught.

Environment data

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
BuildVersion                   10.0.14393.1000
SerializationVersion           1.1.0.1
PSVersion                      5.1.14393.1000
PSEdition                      Core
WSManStackVersion              3.0
CLRVersion

Activity

  1. aburlak commented on Dec 16, 2016

    @aburlak
    Author

    After some debugging, it looks like the root cause is that .NET Core's HttpClientHandler silently refuses to follow HTTPS-to-HTTP redirects.

    However, there should be some way to at least look at the underlying response headers - currently, only a generic string description of the error is provided in the caught Exception (in desktop PowerShell - it is possible to retrieve the headers).

  2. added this to the milestone on Feb 28, 2017
  3. modified the milestones: , 6.0.0-beta1 on Mar 15, 2017
  4. SteveL-MSFT commented on Mar 15, 2017

    @SteveL-MSFT
    Member

    Perhaps we should add a -AllowRedirectToHTTPfromHTTPS type switch or just output a warning message

  5. changed the title [-]Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects (Windows Server Nano)[/-] [+]Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects[/+] on Mar 15, 2017
  6. SteveL-MSFT commented on Mar 15, 2017

    @SteveL-MSFT
    Member

    This repros with alpha.17 on Ubuntu16 so not Nano specific

  7. changed the title [-]Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects[/-] [+]Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects from HTTPS[/+] on Mar 15, 2017
  8. added
    Committee-ReviewedPS-Committee has reviewed this and made a decision
    and removed
    Review - CommitteeThe PR/Issue needs a review from the PowerShell Committee
    on Mar 15, 2017
  9. SteveL-MSFT commented on Mar 15, 2017

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee suggests -allowInsecureRedirect

  10. 34 remaining items

  11. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Jul 2, 2020
  12. tsteven4 commented on Jan 4, 2021

    @tsteven4

    In the mean time:
    C:\Windows\System32\curl -L -o outfile "https://example.com/somefile"

  13. mkht commented on Feb 7, 2021

    @mkht
    Contributor

    This issue was a major obstacle for me when I migrated from PS5 to PS7.
    I hope it will be addressed as soon as possible.

  14. added and removed
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Jul 12, 2021
  15. ghost added
    In-PRIndicates that a PR is out for the issue
    on Nov 13, 2022
  16. ghost removed
    In-PRIndicates that a PR is out for the issue
    on Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-Committee has reviewed this and made a decisionHacktoberfest-AcceptedAccepted to participate in HacktoberfestIssue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions