Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Web Cmdlets Fail to Redirect to Relative Paths when Authorization Header Supplied but -PreserveAuthorizationOnRedirect Not Present #5967

Copy link
Copy link
@markekraus

Description

@markekraus
Issue body actions

When a relative path redirect is sent in the Location response header and the Authorization header is supplied, Invoke-WebRequest and Invoke-RestMethod will fail to redirect unless the -PreserveAuthorizationOnRedirect parameter is supplied. This does not fail if the Location: supplies an absolute URL.

The problem is here:

using (HttpRequestMessage redirectRequest = GetRequest(response.Headers.Location, stripAuthorization:true))

We are taking the Location header and supplying it as is. When the Location header contains a relative path (/get for example instead of https://httpbin.org/get), this causes a System.UriFormatException on the following line

We need to detect an relative URI and construct an absulute one if neccesary.

This is a regression from Windows PowerShell 5.1 introduced in #3885.

Steps to reproduce

$uri = ' https://httpbin.org/relative-redirect/6'
$headers = @{Authorization = "Bearer foo"}
Invoke-WebRequest -Uri $uri -Headers $headers | Select-Object -ExpandProperty Content

Expected behavior

{
  "args": {},
  "headers": {
    "Connection": "close",
    "Host": "httpbin.org",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.16299; en-US) PowerShell/6.0.0"
  },
  "origin": "209.205.125.102",
  "url": "https://httpbin.org/get"
}

Actual behavior

Invoke-WebRequest : Invalid URI: The hostname could not be parsed.
At line:1 char:1
+ Invoke-WebRequest -Uri $uri -Headers $headers | Select-Object -Expand ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], UriFormatException
+ FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Environment data

Name                           Value
----                           -----
PSVersion                      6.0.0
PSEdition                      Core
GitCommitId                    v6.0.0
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.The issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility modulecmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.