From 6a65c5fc04d0ded8dc8946396304a84f0336922b Mon Sep 17 00:00:00 2001 From: AkariiinMKII <6019344+AkariiinMKII@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:21:13 +0800 Subject: [PATCH 1/4] feat(autoupdate): Add base64 decoding for hash values fetched in json mode --- lib/autoupdate.ps1 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/autoupdate.ps1 b/lib/autoupdate.ps1 index 5cef1622a0..84f79d90b8 100644 --- a/lib/autoupdate.ps1 +++ b/lib/autoupdate.ps1 @@ -129,6 +129,19 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $ if (!$hash) { $hash = json_path_legacy $json $jsonpath $substitutions } + + # 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 + } + } + } + return format_hash $hash } From d7a9421267fb59b2307201974580887b8baba5be Mon Sep 17 00:00:00 2001 From: AkariiinMKII <6019344+AkariiinMKII@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:26:30 +0800 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fecb9eef35..1f6ddd8952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/master...develop) + +### Features + +- **autoupdate**: Add base64 decoding for hash values fetched in json mode ([#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 From 93c815a1ba0d505afd2105e7aca9138c591b32b0 Mon Sep 17 00:00:00 2001 From: AkariiinMKII <6019344+AkariiinMKII@users.noreply.github.com> Date: Sat, 18 Jan 2025 20:20:11 +0800 Subject: [PATCH 3/4] move base64 converter to format_hash --- lib/autoupdate.ps1 | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/lib/autoupdate.ps1 b/lib/autoupdate.ps1 index 84f79d90b8..2dccc0d3be 100644 --- a/lib/autoupdate.ps1 +++ b/lib/autoupdate.ps1 @@ -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 @@ -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})" @@ -130,18 +133,6 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $ $hash = json_path_legacy $json $jsonpath $substitutions } - # 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 - } - } - } - return format_hash $hash } @@ -199,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() From ff15e8893584f7b7145669d6e18cb429b4acf1f6 Mon Sep 17 00:00:00 2001 From: AkariiinMKII <6019344+AkariiinMKII@users.noreply.github.com> Date: Sat, 18 Jan 2025 20:41:12 +0800 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f6ddd8952..b2e431a222 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- **autoupdate**: Add base64 decoding for hash values fetched in json mode ([#6271](https://github.com/ScoopInstaller/Scoop/issues/6271)) +- **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