Skip to content

fix(downloader): in-process SHA-256 + per-candidate integrity fallback#321

Merged
Sunrisepeak merged 3 commits into
mainfrom
fix/in-process-sha256
Jun 5, 2026
Merged

fix(downloader): in-process SHA-256 + per-candidate integrity fallback#321
Sunrisepeak merged 3 commits into
mainfrom
fix/in-process-sha256

Conversation

@Sunrisepeak

Copy link
Copy Markdown
Member

Context

mcpp's new macos-14 fresh-install CI lane (mcpp-community/mcpp PR #123) caught mcpplibs.cmdline@0.0.1 failing with SHA256 mismatch on every attempt — including with the author URL first and bytes proven intact via curl (shasum matched the declared sha256).

Root causes

  1. sha256sum shell-out: stock macOS has no sha256sum (coreutils; only shasum ships). run_command_capture fails → treated as mismatch → every sha256-pinned download fails on such hosts, irrespective of content. The macos-14 runner image lacks it; macos-15 happens to have it transitively, masking the bug there.
  2. Post-loop verification: the sha check ran after download_file returned, so a fast-but-corrupt mirror (adaptive ordering may put mirrors before the author URL when bytes are sha-pinned) failed the whole download with the remaining candidates untried.

Fix

  • xlings.libs.sha256: in-process FIPS 180-4 SHA-256 (streaming hasher + hex/hex_file); both downloader call sites switched — no host-tool dependency on any platform.
  • download_file gains an onVerify per-candidate acceptance hook: rejected candidate → file removed, onUrlAttemptFailed fires (host penalized via mirror::adaptive), next URL tried. Wired to the sha check; behavior unchanged when no hook is set.

Tests

  • test_sha256.cpp: NIST vectors (empty/abc/two-block/million-a), padding boundaries (55/56/63/64 + split updates), file digest, missing file.
  • DownloadVerify.*: rejected candidate falls through to next URL; all-rejected fails with file removed; no-hook keeps prior behavior (via a transferOverride test seam — same pattern as the exported StallDetector).
  • Full suite locally: all green except Extract.ZipUtf8PathRoundTrip, which fails identically on clean main (local host zip/locale issue, green on CI).

Two defects found by mcpp's macos-14 fresh-install lane (mcpp PR #123):

1. sha256 verification shelled out to `sha256sum` — a GNU coreutils
   tool absent on stock macOS (only `shasum` exists). On hosts without
   coreutils every sha256-pinned download failed as 'SHA256 mismatch'
   regardless of payload integrity (macos-14 runner image; macos-15
   happens to carry it transitively). Replaced both call sites with an
   in-process FIPS 180-4 implementation (xlings.libs.sha256) — no host
   tool dependency on any platform.

2. Integrity was checked AFTER the multi-candidate download loop: a
   mirror that wins the adaptive latency race but serves corrupted
   bytes failed the whole download with the remaining candidates
   (including the author URL) untried. download_file now takes an
   onVerify per-candidate acceptance hook — a rejected candidate is
   removed, penalized (mirror::adaptive), and the next URL is tried.

Unit tests: NIST vectors + padding boundaries + file digests for
sha256; candidate-fallback behavior via a transferOverride test seam.
@Sunrisepeak Sunrisepeak merged commit d6f1e7e into main Jun 5, 2026
3 checks passed
@Sunrisepeak Sunrisepeak deleted the fix/in-process-sha256 branch June 5, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant