Skip to content

InvokeWeb-Request -WebSession -AllowInsecureRedirect -MaximumRedirection together wrong number of redirections #19183

Closed

Description

@CarloToso

Prerequisites

Steps to reproduce

I think the behaviour when we handle the redirects* and use -MaximumRedirection and -WebSession is incorrect.
Each time we handle a redirection the WebSession.MaximumRedirection parameter is reduced by one.
In the example below it goes from 3 to 0.
When we use the web session I would expect the -MaximumRedirection to be 3 in each follow up request, instead WebSession.MaximumRedirection is 0 and stays 0

Test:

Invoke-WebRequest "http://mockbin.org/redirect/300?to=https://mockbin.org/redirect/301?to=http://mockbin.org/redirect/302?to=https://mockbin.org/redirect/303?to=http://mockbin.org/redirect/308?to=https://mockbin.org/status/200" -MaximumRedirection 3 -SessionVariable session -AllowInsecureRedirect
[int]($error[0].Exception.statuscode) #--> 303
$session.MaximumRedirection #--> 0 (i think it should be 3)
Invoke-WebRequest "http://mockbin.org/redirect/300?to=https://mockbin.org/redirect/301?to=http://mockbin.org/redirect/302?to=https://mockbin.org/redirect/303?to=http://mockbin.org/redirect/308?to=https://mockbin.org/status/200" -WebSession $session -AllowInsecureRedirect 
# I expect -MaximumRedirection == 3
[int]($error[0].Exception.statuscode) #--> 300
$session.MaximumRedirection #--> 0

If this issue is confirmed, I will fix it in my next PR

  • We currently handle the redirects in 3 cases -AllowInsecureRedirect (https to http redirect); -PreserveAuthorizationOnRedirect; -PreserveHttpMethodOnRedirect in the other cases HttpClient handles the redirects

Expected behavior

After setting -MaximumRedirection 3 it should remain 3 in the following requests

Actual behavior

It changes

Error details

No response

Environment data

Latest Build
(every build since https://github.com/PowerShell/PowerShell/commit/ca4b6162f49aff1b89fa624a2e059794c740b6cb)

Visuals

No response

Activity

  1. added
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Feb 20, 2023
  2. changed the title [-]InvokeWeb-Request -WebSession -AllowInsecureRedirect -MaximumRedirection together variable number of redirections[/-] [+]InvokeWeb-Request -WebSession -AllowInsecureRedirect -MaximumRedirection together wrong number of redirections[/+] on Feb 20, 2023
  3. iSazonov commented on Feb 21, 2023

    @iSazonov
    Collaborator

    I'd check other WebSession properties too.

  4. CarloToso commented on Feb 22, 2023

    @CarloToso
    ContributorAuthor

    WebSession.MaximumRedirection is the only WebSession property that changes after being set.

    // If explicit count was provided, reduce it for this redirection.
    if (WebSession.MaximumRedirection > 0)
    {
    WebSession.MaximumRedirection--;
    }

  5. ghost added
    In-PRIndicates that a PR is out for the issue
    on Feb 28, 2023
  6. ghost removed
    In-PRIndicates that a PR is out for the issue
    on Mar 1, 2023
  7. ghost removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Mar 1, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions