Verify cached pack zips against the release SHA256SUMS file.
This commit is contained in:
1 parent
428760fef6
commit
ad18b93b7a
3 files changed
+123
-5
No files matched your search
@@ -466,10 +466,37 @@ public sealed class MainForm : Form
|
|||||||
"cache");
|
"cache");
|
||||||
PathUtils.EnsureDirectory(cacheDir);
|
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);
|
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)...");
|
Log($"Downloading {asset.Name} ({asset.Size / (1024 * 1024)} MB)...");
|
||||||
|
try
|
||||||
|
{
|
||||||
await _gitea.DownloadAssetAsync(asset, zipPath, new Progress<(long, long)>(p =>
|
await _gitea.DownloadAssetAsync(asset, zipPath, new Progress<(long, long)>(p =>
|
||||||
{
|
{
|
||||||
var (downloaded, total) = p;
|
var (downloaded, total) = p;
|
||||||
@@ -479,11 +506,23 @@ public sealed class MainForm : Form
|
|||||||
_progressBar.Value = Math.Clamp(pct, 0, 100);
|
_progressBar.Value = Math.Clamp(pct, 0, 100);
|
||||||
}
|
}
|
||||||
}), token);
|
}), token);
|
||||||
Log("Download complete.");
|
|
||||||
}
|
Log("Verifying download...");
|
||||||
else
|
var downloadedHash = await Sha256Sums.HashFileAsync(zipPath, token);
|
||||||
|
if (!Sha256Sums.Matches(downloadedHash, expectedHash))
|
||||||
{
|
{
|
||||||
Log($"Using cached pack: {zipPath}");
|
throw new InvalidOperationException(
|
||||||
|
$"SHA256 mismatch for {asset.Name}. Expected {expectedHash}, got {downloadedHash}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
if (File.Exists(zipPath))
|
||||||
|
PathUtils.DeletePath(zipPath);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log("Download verified.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var extractRoot = Path.Combine(cacheDir, "extract", _latestRelease.TagName);
|
var extractRoot = Path.Combine(cacheDir, "extract", _latestRelease.TagName);
|
||||||
|
|||||||
@@ -54,6 +54,13 @@ public sealed class GiteaClient
|
|||||||
return destinationPath;
|
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)
|
public ReleaseAsset? FindPackAsset(ReleaseInfo release, OmniNxVariant variant)
|
||||||
{
|
{
|
||||||
var prefix = VariantHelper.GetZipAssetPrefix(variant);
|
var prefix = VariantHelper.GetZipAssetPrefix(variant);
|
||||||
@@ -62,6 +69,13 @@ public sealed class GiteaClient
|
|||||||
a.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase));
|
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()
|
private static ReleaseInfo MapRelease(GiteaReleaseDto dto) => new()
|
||||||
{
|
{
|
||||||
TagName = dto.TagName ?? string.Empty,
|
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