Files
ThreadPilot/Services/UpdateAssetSelector.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

82 lines
2.5 KiB
C#

/*
* ThreadPilot - release asset selection for safe installer updates.
*/
namespace ThreadPilot.Services
{
using System;
using System.IO;
using System.Linq;
public static class UpdateAssetSelector
{
public static bool TrySelectInstaller(UpdateReleaseInfo release, out UpdateAsset asset)
{
var selected = release.Assets
.Where(IsInstallerAsset)
.OrderByDescending(candidate => candidate.Name.Contains("setup", StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
asset = selected!;
return selected != null;
}
public static UpdateAsset? SelectChecksumAsset(UpdateReleaseInfo release)
{
return release.Assets.FirstOrDefault(asset =>
string.Equals(asset.Name, "SHA256SUMS.txt", StringComparison.OrdinalIgnoreCase));
}
public static bool IsSafeGitHubAssetUrl(Uri uri)
{
if (uri.Scheme != Uri.UriSchemeHttps)
{
return false;
}
return string.Equals(uri.Host, "github.com", StringComparison.OrdinalIgnoreCase) ||
string.Equals(uri.Host, "objects.githubusercontent.com", StringComparison.OrdinalIgnoreCase);
}
public static bool IsSafeAssetFileName(string assetName)
{
if (string.IsNullOrWhiteSpace(assetName))
{
return false;
}
if (!string.Equals(Path.GetFileName(assetName), assetName, StringComparison.Ordinal))
{
return false;
}
return assetName.IndexOfAny(Path.GetInvalidFileNameChars()) < 0;
}
private static bool IsInstallerAsset(UpdateAsset asset)
{
if (!IsSafeGitHubAssetUrl(asset.DownloadUrl) || !IsSafeAssetFileName(asset.Name))
{
return false;
}
if (!asset.Name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (!asset.Name.StartsWith("ThreadPilot", StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (asset.Name.Contains("portable", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return asset.Name.Contains("setup", StringComparison.OrdinalIgnoreCase) ||
asset.Name.Contains("installer", StringComparison.OrdinalIgnoreCase);
}
}
}