GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix bug with managing redirection and keepAuthorization in Web cmdlets by CarloToso · Pull Request #18902 · PowerShell/PowerShell

Conversation

@CarloToso

@CarloToso CarloToso commented Jan 7, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

PR Summary

The current behaviour of if (keepAuthorization && IsRedirectCode(response.StatusCode) && response.Headers.Location is not null) is wrong, I tested it by setting keepAuthorization = true --> this sets handleRetirect = true --> and then sets handler.AllowAutoRedirect = false. Without sessionRedirect (we can choose a better name) it ignores the MaximumRedirection parameter.

Test: maunally set keepAuthorization = true before the if, then
Invoke-WebRequest "http://mockbin.org/redirect/308?to=https://mockbin.org/redirect/302?to=https://mockbin.org/status/200" -MaximumRedirection 0 -SkipHttpErrorCheck --> expected 308, result 200

Proposed fix -->

bool sessionRedirect = WebSession.MaximumRedirection > 0 || WebSession.MaximumRedirection == -1;
if (keepAuthorization  && sessionRedirect && IsRedirectCode(response.StatusCode) && response.Headers.Location is not null)

PR Context

Fix Bug, merge before #18546 and #18894

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@CarloToso
fix bug with managing redirection
4ad575b
@CarloToso
CarloToso requested a review from Paul Higinbotham (PaulHigin) as a code owner January 7, 2023 15:30
@pull-request-quantifier-deprecated pull-request-quantifier-deprecated Bot added the Extra Small label Jan 7, 2023
@ghost ghost assigned Aditya Patwardhan (adityapatwardhan) Jan 7, 2023
@iSazonov
Ilya (iSazonov) requested a review from Aditya Patwardhan (adityapatwardhan) January 7, 2023 17:15
@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jan 7, 2023
@iSazonov

Ilya (iSazonov) commented Jan 7, 2023

Copy link
Copy Markdown
Collaborator

CarloToso Can you enhance our helper test tool - WebListener - to cover the scenario? Currently it supports redirection but not take into account authorization header (I guess we could use -Authentication Bear?). WebListener could return the auth token as-is and we could add tests to check that the header is removed by default and isn't removed with PreserveAuthorizationOnRedirect.

I see only reason we do custom redirection processing is that we want to keep the auth header. So I suggest rename keepAuthorization and handleRedirect parameters to keepAuthorizationOnRedirect. This simplify understanding the code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CarloToso

CarloToso commented Jan 7, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) we also need to handle redirection for #18546 and #18894, I can change keepAuthorization to keepAuthorizationOnRedirect but i think handleRedirect should stay as it is.

I don't know much about the helper tool test but I can look into it

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Jan 7, 2023

Copy link
Copy Markdown
Collaborator

but i think handleRedirect should stay as it is.

It is the same as keepAuthorization - we assign keepAuthorization to handleRedirect - this mislead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

xtqqczze
xtqqczze reviewed Jan 7, 2023
View reviewed changes
Comment thread ...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

CarloToso and others added 5 commits January 8, 2023 00:48
@CarloToso @xtqqczze
Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/Web…
a1671df
…Cmdlet/Common/WebRequestPSCmdlet.Common.cs

Co-authored-by: xtqqczze <[email protected]>
@CarloToso
keepAuthorization --> keepAuthorizationOnRedirect
8510729
@CarloToso
remove useless IsPresent
e378ef2
@CarloToso
missed two keepAuthorizationOnRedirect
2a9f147
@CarloToso
bool handleRedirect = keepAuthorizationOnRedirect
f5b6d1d
@CarloToso

CarloToso commented Jan 8, 2023

Copy link
Copy Markdown
Contributor Author

If we change it handleRedirect to keepAuthorization, it will be misleading after we add -PreserveHTTPMethodOnRedirect or -AllowInsicureRedirect. We could add something like this bool handleRedirect = keepAuthorizationOnRedirect || AllowInsicureRedirect || PreserveHTTPMethodOnRedirect

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CarloToso
better handleRedirect if logic
e48bd39
@iSazonov

Ilya (iSazonov) commented Jan 8, 2023

Copy link
Copy Markdown
Collaborator

If we change it handleRedirect to keepAuthorization, it will be misleading after we add -PreserveHTTPMethodOnRedirect or -AllowInsicureRedirect. We could add something like this bool handleRedirect = keepAuthorizationOnRedirect || AllowInsicureRedirect || PreserveHTTPMethodOnRedirect

I don't see a problem here. As I said only reason we do manual redirect processing is that we want to keep auth header. So variable names should reflect to our intention. And comments could be updated too.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iSazonov
Ilya (iSazonov) reviewed Jan 8, 2023
View reviewed changes
Comment thread ...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs Outdated

// Resume requires OutFile.
if (Resume.IsPresent && OutFile is null)
if (Resume && OutFile is null)

@iSazonov Ilya (iSazonov) Jan 8, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reduces readability. 😕

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@CarloToso CarloToso Jan 8, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
CarloToso added 2 commits January 8, 2023 13:12
@CarloToso
revert Resume.IsPresent
1345784
@CarloToso
keepAuthorizationOnRedirect --> handleRedirect
3ed7bd7
@CarloToso CarloToso mentioned this pull request Jan 8, 2023
Add PreserveHttpMethodOnRedirect switch to Web cmdlets #18894
Merged
6 of 22 tasks
@iSazonov Ilya (iSazonov) changed the title fix bug with managing redirection Fix bug with managing redirection Jan 9, 2023
@CarloToso

CarloToso commented Jan 10, 2023

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) It looks like there already are some tests that cover -PreserveAuthorizationOnRedirect

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Jan 11, 2023

Copy link
Copy Markdown
Collaborator

Ilya (@iSazonov) It looks like there already are some tests that cover -PreserveAuthorizationOnRedirect

Can you point these tests? I see there is Authorization controller but it doesn't cover redirection scenario.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CarloToso

CarloToso commented Jan 11, 2023

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) I found the -PreserveAuthorizationOnRedirect parameter in WebCmdlet.Tests.ps1 and I think it covers this case

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Jan 11, 2023

Copy link
Copy Markdown
Collaborator

If the test is passed before the PR it is a broken test.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CarloToso
add test
c2d3399
@CarloToso

CarloToso commented Jan 11, 2023

Copy link
Copy Markdown
Contributor Author

I think the tests are not broken, they just never checked for -MaximumRedirection + -PreserveAuthorizationOnRedirect

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iSazonov
Ilya (iSazonov) reviewed Jan 12, 2023
View reviewed changes

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please confirm that the new tests fail before the PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

xtqqczze
xtqqczze suggested changes Jan 12, 2023
View reviewed changes
Comment thread ...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jan 12, 2023
@CarloToso
remove comment
7f3fd07
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jan 12, 2023
@iSazonov

Ilya (iSazonov) commented Jan 12, 2023

Copy link
Copy Markdown
Collaborator

CarloToso Please resolve merge conflicts.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CarloToso
Merge branch 'master' into Fix-handleRedirect-with-MaximumRedirection
bcfcfc1
@iSazonov

Ilya (iSazonov) commented Jan 12, 2023

Copy link
Copy Markdown
Collaborator

CarloToso Please resolve merge conflict again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CarloToso
Merge branch 'master' into Fix-handleRedirect-with-MaximumRedirection
536de9d
iSazonov
Ilya (iSazonov) reviewed Jan 12, 2023
View reviewed changes
Comment thread ...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs Outdated
Comment on lines +1419 to +1423
bool keepAuthorizationOnRedirect = WebSession is not null
&& WebSession.Headers is not null
&& PreserveAuthorizationOnRedirect.IsPresent
&& WebSession.Headers.ContainsKey(HttpKnownHeaderNames.Authorization);

@iSazonov Ilya (iSazonov) Jan 12, 2023

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems PreserveAuthorizationOnRedirect.IsPresent should be on first place.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
CarloToso added 2 commits January 12, 2023 18:12
@CarloToso
reorder keepAuthorizationOnRedirect
2771457
@CarloToso
fix error
7a8daeb
@iSazonov
Ilya (iSazonov) self-requested a review January 12, 2023 17:40
iSazonov
Ilya (iSazonov) requested changes Jan 12, 2023
View reviewed changes
Comment thread ...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Comment on lines +1278 to +1281
if (handleRedirect
&& WebSession.MaximumRedirection is not 0
&& IsRedirectCode(response.StatusCode)
&& response.Headers.Location is not null)

@iSazonov Ilya (iSazonov) Jan 12, 2023

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is AllowInsecureRedirect removed?

We merged #18546 without tests. Time to add tests for AllowInsecureRedirect

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@CarloToso CarloToso Jan 13, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some tests #18939, AllowInsecureRedirect is in handleRedirect after this PR (line 1422)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jan 12, 2023
@CarloToso CarloToso mentioned this pull request Jan 13, 2023
Add tests for Allowinsecureredirect parameter in Web cmdlets #18939
Merged
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jan 13, 2023
@iSazonov

Ilya (iSazonov) commented Jan 13, 2023

Copy link
Copy Markdown
Collaborator

/rebase

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Jan 13, 2023

Copy link
Copy Markdown
Collaborator

CarloToso Please rebase to get tests.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CarloToso
Merge branch 'PowerShell:master' into Fix-handleRedirect-with-Maximum…
289f5e8
…Redirection
@pull-request-quantifier-deprecated

pull-request-quantifier-deprecated Bot commented Jan 13, 2023

Copy link
Copy Markdown

This PR has 47 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +28 -19
Percentile : 18.8%

Total files changed: 2

Change summary by file extension:
.cs : +16 -17
.ps1 : +12 -2

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iSazonov
Ilya (iSazonov) approved these changes Jan 13, 2023
View reviewed changes
@iSazonov Ilya (iSazonov) assigned Ilya (iSazonov) and unassigned Aditya Patwardhan (adityapatwardhan) Jan 13, 2023
@iSazonov Ilya (iSazonov) changed the title Fix bug with managing redirection Fix bug with managing redirection and keepAuthorization in Web cmdlets Jan 13, 2023
@iSazonov
Ilya (iSazonov) merged commit 282fc5f into PowerShell:master Jan 13, 2023
@CarloToso
CarloToso deleted the Fix-handleRedirect-with-MaximumRedirection branch January 13, 2023 14:16
@ghost

Deleted user (ghost) commented Mar 14, 2023

Copy link
Copy Markdown

🎉v7.4.0-preview.2 has been released which incorporates this pull request.:tada:

Handy links:

  • Release Notes
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@CarloToso
Fix bug with managing redirection and keepAuthorization in Web cmdlets (
1a32319
PowerShell#18902)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iSazonov Ilya (iSazonov) iSazonov approved these changes
@PaulHigin Paul Higinbotham (PaulHigin) Awaiting requested review from PaulHigin
@adityapatwardhan Aditya Patwardhan (adityapatwardhan) Awaiting requested review from adityapatwardhan
+1 more reviewer
@xtqqczze xtqqczze xtqqczze requested changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@iSazonov Ilya (iSazonov)

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@CarloToso @iSazonov @xtqqczze @adityapatwardhan
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.