Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(install): Limit HTTP redirects #5758

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- **scoop-reset:** Don't abort when multiple apps are passed and an app is running ([#5687](https://github.com/ScoopInstaller/Scoop/issues/5687))
- **core:** Do not call `scoop` externally from inside the code ([#5695](https://github.com/ScoopInstaller/Scoop/issues/5695))
- **scoop-checkup:** Don't throw 7zip error when external 7zip is used ([#5703](https://github.com/ScoopInstaller/Scoop/issues/5703))
- **install:** Limit HTTP redirects ([#5757](https://github.com/ScoopInstaller/Scoop/issues/5757))

### Performance Improvements

Expand Down
10 changes: 8 additions & 2 deletions lib/install.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$MaxRedirectCount = 20

function nightly_version($quiet = $false) {
if (!$quiet) {
warn "This is a nightly version. Downloaded files won't be verified."
Expand Down Expand Up @@ -355,7 +357,7 @@ function Invoke-CachedAria2Download ($app, $version, $manifest, $architecture, $
}

# download with filesize and progress indicator
function Invoke-Download ($url, $to, $cookies, $progress) {
function Invoke-Download ($url, $to, $cookies, $progress, $redirectCount = 0) {
$reqUrl = ($url -split '#')[0]
$wreq = [Net.WebRequest]::Create($reqUrl)
if ($wreq -is [Net.HttpWebRequest]) {
Expand Down Expand Up @@ -396,6 +398,10 @@ function Invoke-Download ($url, $to, $cookies, $progress) {
throw $exc
}

if ($redirectCount++ -ge $MaxRedirectCount) {
throw "Exceeded maximum redirect limit. Aborting."
}

# Get the new location of the file
if ((-not $redirectRes.Headers) -or ($redirectRes.Headers -notcontains 'Location')) {
throw $exc
Expand All @@ -410,7 +416,7 @@ function Invoke-Download ($url, $to, $cookies, $progress) {
$newUrl = "$newUrl#/$postfix"
}

Invoke-Download $newUrl $to $cookies $progress
Invoke-Download $newUrl $to $cookies $progress $redirectCount
return
}

Expand Down