Files
ThreadPilot/Services/UpdateDownloadService.cs
T
Prime Build 024673dcbe Add safe in-app updater (#28)
## Summary

Adds a safe in-app updater for ThreadPilot. This PR does not bump the app version and does not prepare a release; version and release metadata will be handled separately for v1.4.0.

## Type of Change

- [x] Feature
- [x] Security hardening
- [x] Documentation
- [x] Added/updated tests

## Update flow

- Checks for updates on demand from Settings.
- Performs background startup checks only when enabled and the last check is older than the configured interval.
- Defaults to a 7-day interval.
- Excludes prereleases by default.
- Notifies users when an update is available.
- Requires explicit user action before download/install.

## Security and safety

- Uses release metadata from the official `PrimeBuild-pc/ThreadPilot` repository only.
- Selects safe ThreadPilot installer assets from GitHub HTTPS release assets.
- Downloads into a ThreadPilot-owned temp update directory.
- Verifies SHA256 checksums when `SHA256SUMS.txt` is available.
- Rejects checksum mismatches.
- Performs best-effort Authenticode verification and rejects explicitly invalid signatures.
- Starts the installer elevated without shell command construction.
- Prevents concurrent install attempts.

## User data preservation

- Update flow does not delete AppData, settings, profiles, CPU masks, rules, custom/imported power plans, or logs.
- Only updater temp files are cleaned.
- Full uninstall behavior remains separate.

## Validation

- `dotnet restore ThreadPilot_1.sln`: passed
- `dotnet build ThreadPilot_1.sln`: passed, 0 warnings, 0 errors
- `dotnet test ThreadPilot_1.sln`: passed, 537 tests

## Release note

This PR intentionally leaves `ThreadPilot.csproj` at `1.3.1`. The `1.4.0` version bump, changelog, packaging metadata, and tag should happen in a separate release-prep branch after this PR is merged.
2026-06-07 06:06:53 +02:00

104 lines
4.5 KiB
C#

/*
* ThreadPilot - secure update installer download and verification.
*/
namespace ThreadPilot.Services
{
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
public sealed class UpdateDownloadService : IUpdateDownloadService
{
private readonly IUpdateDownloadClient downloadClient;
private readonly IUpdateTempDirectoryProvider tempDirectoryProvider;
private readonly IUpdateSignatureVerifier signatureVerifier;
private readonly ILogger<UpdateDownloadService> logger;
public UpdateDownloadService(
IUpdateDownloadClient downloadClient,
IUpdateTempDirectoryProvider tempDirectoryProvider,
IUpdateSignatureVerifier signatureVerifier,
ILogger<UpdateDownloadService> logger)
{
this.downloadClient = downloadClient ?? throw new ArgumentNullException(nameof(downloadClient));
this.tempDirectoryProvider = tempDirectoryProvider ?? throw new ArgumentNullException(nameof(tempDirectoryProvider));
this.signatureVerifier = signatureVerifier ?? throw new ArgumentNullException(nameof(signatureVerifier));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<UpdateDownloadResult> DownloadInstallerAsync(UpdateReleaseInfo release, CancellationToken cancellationToken = default)
{
if (!UpdateAssetSelector.TrySelectInstaller(release, out var installerAsset))
{
throw new InvalidOperationException("Release does not contain a ThreadPilot installer asset.");
}
var tempDirectory = this.tempDirectoryProvider.CreateUpdateTempDirectory(release.Version);
try
{
var installerPath = Path.Combine(tempDirectory, installerAsset.Name);
await this.downloadClient.DownloadFileAsync(installerAsset.DownloadUrl, installerPath, cancellationToken)
.ConfigureAwait(false);
var checksumVerified = false;
var checksumAsset = UpdateAssetSelector.SelectChecksumAsset(release);
if (checksumAsset != null)
{
var checksumText = await this.downloadClient.TryDownloadStringAsync(checksumAsset.DownloadUrl, cancellationToken)
.ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(checksumText) ||
!UpdateChecksumVerifier.TryFindExpectedHash(checksumText, installerAsset.Name, out var expectedHash))
{
throw new InvalidOperationException("SHA256SUMS.txt did not contain the installer checksum.");
}
if (!UpdateChecksumVerifier.Verify(installerPath, expectedHash))
{
throw new InvalidOperationException("Installer SHA256 checksum did not match SHA256SUMS.txt.");
}
checksumVerified = true;
}
var signatureStatus = this.signatureVerifier.Verify(installerPath);
if (signatureStatus == UpdateSignatureStatus.Invalid)
{
throw new InvalidOperationException("Installer Authenticode signature is invalid.");
}
this.logger.LogInformation(
"Downloaded ThreadPilot update installer {InstallerName}; checksum verified: {ChecksumVerified}; signature: {SignatureStatus}",
installerAsset.Name,
checksumVerified,
signatureStatus);
return new UpdateDownloadResult(
installerPath,
tempDirectory,
checksumVerified,
signatureStatus,
checksumVerified ? "Installer checksum verified." : "No SHA256SUMS.txt asset was available.");
}
catch
{
this.TryCleanup(tempDirectory);
throw;
}
}
private void TryCleanup(string tempDirectory)
{
try
{
this.tempDirectoryProvider.Cleanup(tempDirectory);
}
catch (Exception ex)
{
this.logger.LogWarning(ex, "Failed to clean update temp directory {TempDirectory}", tempDirectory);
}
}
}
}