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

feat(autoupdate): Try decoding invalid hash value with base64 in format_hash #6272

Open
wants to merge 4 commits 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/master...develop)

### Features

- **autoupdate**: Try decoding base64 encoded hash values in `format_hash` ([#6271](https://github.com/ScoopInstaller/Scoop/issues/6271))

## [v0.5.3](https://github.com/ScoopInstaller/Scoop/compare/v0.5.2...v0.5.3) - 2024-12-31

### Bug Fixes
Expand Down
31 changes: 17 additions & 14 deletions lib/autoupdate.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# Must included with 'json.ps1'

function format_hash([String] $hash) {

# convert base64 encoded hash values to hex string
if ($hash -match '^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$') {
$base64 = $matches[0]
if (!($hash -match '^[a-fA-F0-9]+$') -and $hash.Length -notin @(32, 40, 64, 128)) {
try {
$hash = ([System.Convert]::FromBase64String($base64) | ForEach-Object { $_.ToString('x2') }) -join ''
} catch {
$hash = $hash
}
}
}

debug $hash

$hash = $hash.toLower()
switch ($hash.Length) {
32 { $hash = "md5:$hash" } # md5
Expand Down Expand Up @@ -74,18 +89,6 @@ function find_hash_in_textfile([String] $url, [Hashtable] $substitutions, [Strin
$hash = $matches[1] -replace '\s', ''
}

# convert base64 encoded hash values
if ($hash -match '^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$') {
$base64 = $matches[0]
if (!($hash -match '^[a-fA-F0-9]+$') -and $hash.Length -notin @(32, 40, 64, 128)) {
try {
$hash = ([System.Convert]::FromBase64String($base64) | ForEach-Object { $_.ToString('x2') }) -join ''
} catch {
$hash = $hash
}
}
}

# find hash with filename in $hashfile
if ($hash.Length -eq 0) {
$filenameRegex = "([a-fA-F0-9]{32,128})[\x20\t]+.*`$basename(?:\s|$)|`$basename[\x20\t]+.*?([a-fA-F0-9]{32,128})"
Expand Down Expand Up @@ -129,6 +132,7 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $
if (!$hash) {
$hash = json_path_legacy $json $jsonpath $substitutions
}

return format_hash $hash
}

Expand Down Expand Up @@ -186,8 +190,7 @@ function find_hash_in_headers([String] $url) {
$res = $req.GetResponse()
if (([int]$res.StatusCode -ge 300) -and ([int]$res.StatusCode -lt 400)) {
if ($res.Headers['Digest'] -match 'SHA-256=([^,]+)' -or $res.Headers['Digest'] -match 'SHA=([^,]+)' -or $res.Headers['Digest'] -match 'MD5=([^,]+)') {
$hash = ([System.Convert]::FromBase64String($matches[1]) | ForEach-Object { $_.ToString('x2') }) -join ''
debug $hash
$hash = $matches[1]
}
}
$res.Close()
Expand Down