Verify cached pack zips against the release SHA256SUMS file.
This commit is contained in:
1 parent
428760fef6
commit
ad18b93b7a
3 files changed
+130
-12
No files matched your search
@@ -466,24 +466,63 @@ public sealed class MainForm : Form
|
||||
"cache");
|
||||
PathUtils.EnsureDirectory(cacheDir);
|
||||
|
||||
var sumsAsset = _gitea.FindChecksumAsset(_latestRelease)
|
||||
?? throw new InvalidOperationException(
|
||||
$"Release {_latestRelease.TagName} has no SHA256SUMS asset.");
|
||||
|
||||
Log($"Fetching {sumsAsset.Name}...");
|
||||
var sumsText = await _gitea.DownloadAssetTextAsync(sumsAsset, token);
|
||||
var expectedHash = Sha256Sums.FindHash(sumsText, asset.Name)
|
||||
?? throw new InvalidOperationException(
|
||||
$"{sumsAsset.Name} has no entry for {asset.Name}.");
|
||||
|
||||
var zipPath = Path.Combine(cacheDir, asset.Name);
|
||||
if (!File.Exists(zipPath))
|
||||
var cacheMatches = false;
|
||||
if (File.Exists(zipPath))
|
||||
{
|
||||
Log($"Checking cached {asset.Name}...");
|
||||
var cachedHash = await Sha256Sums.HashFileAsync(zipPath, token);
|
||||
cacheMatches = Sha256Sums.Matches(cachedHash, expectedHash);
|
||||
if (cacheMatches)
|
||||
Log("Cached pack matches SHA256SUMS.");
|
||||
else
|
||||
{
|
||||
Log("Cached pack hash differs from SHA256SUMS. Downloading again.");
|
||||
PathUtils.DeletePath(zipPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (!cacheMatches)
|
||||
{
|
||||
Log($"Downloading {asset.Name} ({asset.Size / (1024 * 1024)} MB)...");
|
||||
await _gitea.DownloadAssetAsync(asset, zipPath, new Progress<(long, long)>(p =>
|
||||
try
|
||||
{
|
||||
var (downloaded, total) = p;
|
||||
if (total > 0)
|
||||
await _gitea.DownloadAssetAsync(asset, zipPath, new Progress<(long, long)>(p =>
|
||||
{
|
||||
var pct = (int)(downloaded * 100 / total);
|
||||
_progressBar.Value = Math.Clamp(pct, 0, 100);
|
||||
var (downloaded, total) = p;
|
||||
if (total > 0)
|
||||
{
|
||||
var pct = (int)(downloaded * 100 / total);
|
||||
_progressBar.Value = Math.Clamp(pct, 0, 100);
|
||||
}
|
||||
}), token);
|
||||
|
||||
Log("Verifying download...");
|
||||
var downloadedHash = await Sha256Sums.HashFileAsync(zipPath, token);
|
||||
if (!Sha256Sums.Matches(downloadedHash, expectedHash))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"SHA256 mismatch for {asset.Name}. Expected {expectedHash}, got {downloadedHash}.");
|
||||
}
|
||||
}), token);
|
||||
Log("Download complete.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log($"Using cached pack: {zipPath}");
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (File.Exists(zipPath))
|
||||
PathUtils.DeletePath(zipPath);
|
||||
throw;
|
||||
}
|
||||
|
||||
Log("Download verified.");
|
||||
}
|
||||
|
||||
var extractRoot = Path.Combine(cacheDir, "extract", _latestRelease.TagName);
|
||||
|
||||
@@ -54,6 +54,13 @@ public sealed class GiteaClient
|
||||
return destinationPath;
|
||||
}
|
||||
|
||||
public async Task<string> DownloadAssetTextAsync(ReleaseAsset asset, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await _http.GetAsync(asset.DownloadUrl, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public ReleaseAsset? FindPackAsset(ReleaseInfo release, OmniNxVariant variant)
|
||||
{
|
||||
var prefix = VariantHelper.GetZipAssetPrefix(variant);
|
||||
@@ -62,6 +69,13 @@ public sealed class GiteaClient
|
||||
a.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
public ReleaseAsset? FindChecksumAsset(ReleaseInfo release)
|
||||
{
|
||||
return release.Assets.LastOrDefault(a =>
|
||||
a.Name.StartsWith("SHA256", StringComparison.OrdinalIgnoreCase) &&
|
||||
!a.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private static ReleaseInfo MapRelease(GiteaReleaseDto dto) => new()
|
||||
{
|
||||
TagName = dto.TagName ?? string.Empty,
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace OmniNX.DesktopInstaller.Services;
|
||||
|
||||
public static class Sha256Sums
|
||||
{
|
||||
public static string? FindHash(string sumsText, string fileName)
|
||||
{
|
||||
foreach (var rawLine in sumsText.Split('\n'))
|
||||
{
|
||||
var line = rawLine.Trim();
|
||||
if (line.Length == 0 || line[0] == '#')
|
||||
continue;
|
||||
|
||||
var splitAt = line.IndexOf(' ');
|
||||
if (splitAt <= 0)
|
||||
continue;
|
||||
|
||||
var hash = line[..splitAt].Trim();
|
||||
var name = line[(splitAt + 1)..].Trim();
|
||||
if (name.StartsWith('*'))
|
||||
name = name[1..];
|
||||
|
||||
if (!IsSha256Hex(hash))
|
||||
continue;
|
||||
|
||||
if (name.Equals(fileName, StringComparison.OrdinalIgnoreCase))
|
||||
return hash;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static async Task<string> HashFileAsync(string path, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var stream = new FileStream(
|
||||
path,
|
||||
FileMode.Open,
|
||||
FileAccess.Read,
|
||||
FileShare.Read,
|
||||
bufferSize: 1024 * 1024,
|
||||
FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
|
||||
var hash = await SHA256.HashDataAsync(stream, cancellationToken);
|
||||
return Convert.ToHexString(hash);
|
||||
}
|
||||
|
||||
public static bool Matches(string actualHex, string expectedHex) =>
|
||||
string.Equals(actualHex.Trim(), expectedHex.Trim(), StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
private static bool IsSha256Hex(string value)
|
||||
{
|
||||
if (value.Length != 64)
|
||||
return false;
|
||||
|
||||
foreach (var c in value)
|
||||
{
|
||||
var isHex = c is >= '0' and <= '9' or >= 'a' and <= 'f' or >= 'A' and <= 'F';
|
||||
if (!isHex)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user