diff --git a/install/install-agent.sh b/install/install-agent.sh index db958c6..c95b571 100755 --- a/install/install-agent.sh +++ b/install/install-agent.sh @@ -15,10 +15,11 @@ echo "Installing Heimdall Agent for $PLATFORM..." RELEASE_JSON=$(curl -sf "$CENTRAL_URL/api/updates/latest") || { echo "Failed to fetch release" >&2; exit 1; } if command -v jq &>/dev/null; then VERSION=$(echo "$RELEASE_JSON" | jq -r '.version'); DOWNLOAD_URL=$(echo "$RELEASE_JSON" | jq -r ".assets[\"$PLATFORM\"].url // empty"); EXPECTED_SHA256=$(echo "$RELEASE_JSON" | jq -r ".assets[\"$PLATFORM\"].sha256 // empty"); else VERSION=$(echo "$RELEASE_JSON" | grep -o '"version":"[^"]*"' | head -1 | cut -d'"' -f4); DOWNLOAD_URL=$(echo "$RELEASE_JSON" | grep -o "\"$PLATFORM\"" -A5 | grep -o '"url":"[^"]*"' | head -1 | cut -d'"' -f4); EXPECTED_SHA256=$(echo "$RELEASE_JSON" | grep -o "\"$PLATFORM\"" -A5 | grep -o '"sha256":"[^"]*"' | head -1 | cut -d'"' -f4); fi [[ -z "$DOWNLOAD_URL" ]] && { echo "No binary for $PLATFORM" >&2; exit 1; } +[[ ! "$EXPECTED_SHA256" =~ ^[[:xdigit:]]{64}$ ]] && { echo "Release manifest has no valid SHA-256 digest for $PLATFORM; refusing an unverified install" >&2; exit 1; } echo "Latest version: $VERSION" TMP_DIR=$(mktemp -d); trap 'rm -rf "$TMP_DIR"' EXIT curl -sfL "$DOWNLOAD_URL" -o "$TMP_DIR/$BINARY_NAME" || { echo "Download failed" >&2; exit 1; } -if [[ -n "$EXPECTED_SHA256" ]]; then ACTUAL=$(sha256sum "$TMP_DIR/$BINARY_NAME" | cut -d' ' -f1); [[ "$ACTUAL" != "$EXPECTED_SHA256" ]] && { echo "SHA256 mismatch" >&2; exit 1; }; echo "SHA256 verified"; fi +ACTUAL=$(sha256sum "$TMP_DIR/$BINARY_NAME" | cut -d' ' -f1); [[ "${ACTUAL,,}" != "${EXPECTED_SHA256,,}" ]] && { echo "SHA256 mismatch" >&2; exit 1; }; echo "SHA256 verified" chmod +x "$TMP_DIR/$BINARY_NAME"; [[ $EUID -eq 0 ]] && cp "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME" || sudo cp "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME" id "$SERVICE_USER" &>/dev/null || sudo useradd --system --no-create-home --shell /usr/sbin/nologin "$SERVICE_USER" 2>/dev/null || true sudo mkdir -p "$STATE_DIR" && sudo chown "$SERVICE_USER:$SERVICE_USER" "$STATE_DIR" && sudo chmod 700 "$STATE_DIR" diff --git a/src/Heimdall.Agent/Services/AgentUpdateService.cs b/src/Heimdall.Agent/Services/AgentUpdateService.cs index 0e32eea..67d9c19 100644 --- a/src/Heimdall.Agent/Services/AgentUpdateService.cs +++ b/src/Heimdall.Agent/Services/AgentUpdateService.cs @@ -115,6 +115,9 @@ public sealed class AgentUpdateService : BackgroundService private async Task DownloadAndVerifyAsync(string url, string? expectedSha256, string destPath, CancellationToken ct) { + if (expectedSha256 is null || expectedSha256.Length != 64 || expectedSha256.Any(c => !Uri.IsHexDigit(c))) + throw new InvalidOperationException("The release manifest does not contain a valid SHA-256 digest; refusing an unverified update"); + using var client = _httpClientFactory.CreateClient("Central"); client.Timeout = TimeSpan.FromMinutes(10); var response = await client.GetAsync(url, ct); @@ -124,14 +127,12 @@ public sealed class AgentUpdateService : BackgroundService await rs.CopyToAsync(fs, ct); await fs.FlushAsync(ct); await fs.DisposeAsync(); - if (!string.IsNullOrEmpty(expectedSha256)) - { - var hashBytes = await SHA256.HashDataAsync(File.OpenRead(destPath), ct); - var hex = Convert.ToHexString(hashBytes).ToLowerInvariant(); - if (!string.Equals(hex, expectedSha256, StringComparison.OrdinalIgnoreCase)) - throw new InvalidOperationException($"SHA256 mismatch: expected {expectedSha256}, got {hex}"); - _logger.LogInformation("SHA256 verified: {Hash}", hex); - } + await using var downloaded = File.OpenRead(destPath); + var hashBytes = await SHA256.HashDataAsync(downloaded, ct); + var hex = Convert.ToHexString(hashBytes).ToLowerInvariant(); + if (!string.Equals(hex, expectedSha256, StringComparison.OrdinalIgnoreCase)) + throw new InvalidOperationException($"SHA256 mismatch: expected {expectedSha256}, got {hex}"); + _logger.LogInformation("SHA256 verified: {Hash}", hex); } private static bool IsNewerVersion(string remote, string current)