Skip to content

Verify model artifact SHA-256 at download and load time (closes #1715) - #1730

Merged
mstomar125 merged 1 commit into
mainfrom
worktree-issue-1715-model-sha256-verify
Jul 24, 2026
Merged

mstomar125 merged 1 commit into
mainfrom
worktree-issue-1715-model-sha256-verify

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Closes the last unmet leg of the T9 supply-chain bar (ADR-058): the build-time model download did curl -L with no SHA-256 verification, so a substituted nomic-embed-text-v1.5.Q8_0.gguf (compromised upstream, redirect hijack, MITM on a CI runner, malicious mirror) would be code-signed and notarized as authentic, then parsed by native llama.cpp inside the daemon with full authority. This adds digest verification at both the download and the load site.

The runtime chat-model download (model_manager.rs) was already hardened (#1543); this is the separate build/bundle path it never touched, plus the companion load-time check.

Changes

Build-time — scripts/download-models.ts

  • Pin the HuggingFace commit in the URL (resolve/0188c9bf…/) so main can't move under us, and pin MODEL_SHA256.
  • curl -fL --proto =https (fail-hard on HTTP errors, HTTPS-only). The digest is the authoritative gate regardless of redirect target.
  • Streaming sha256File() (Bun.CryptoHasher) verifies the artifact after download; on mismatch it deletes the file and throws — never bundles an unverified model.
  • Also verifies an already-present file; a stale/mismatched one is re-downloaded rather than trusted.

Load-time — packages/nlp-engine/

  • config.rs: pinned EMBEDDING_MODEL_SHA256 (single source of truth, kept in lockstep with the script), a streaming compute_file_sha256() (64 KB chunks — a 146 MB GGUF never buffers whole), and verify_model_integrity().
  • embedding.rs: right after the model path resolves and before LlamaModel::load_from_file, verify the on-disk bytes; refuse to load on mismatch. This closes the post-install tamper window — a model swapped on disk after install is rejected.
  • error.rs: new EmbeddingError::IntegrityError so the failure is legible (not folded into a generic load error).
  • Cargo.toml: sha2 = "0.10".

Digest rotation is a single-change contract: rotating the model updates the commit + digest in the script AND the EMBEDDING_MODEL_SHA256 const together (called out in both comments).

Acceptance criteria

  • Pin an expected SHA-256 next to MODEL_URL + a matching constant at the Rust load site
  • After download, compute the digest and fail hard + delete the file on mismatch
  • Verify the on-disk model against the pinned digest at load time; refuse to load on mismatch
  • Download over HTTPS with normal cert validation (digest remains authoritative)
  • Rotating the model updates the pinned digest in the same change

Verification

  • Authoritative HF LFS digest for the pinned commit == the pinned constant (3e24342164b3d94991ba9692fdc0dd08e3fd7362e0aacc396a9a5c54a544c3b7); the shipped local model matches it.
  • Ran the script live: verifies the real 146 MB model in ~0.2 s → "already downloaded and verified".
  • New Rust unit tests (all green): NIST sha256("abc") vector, mismatch rejection, unreadable-file error, digest-format invariant.
  • cargo clippy -p nodespace-nlp-engine clean; cargo test --lib green. No fake-model test exists, and integration tests skip when the model is absent / use the real default config, so the load gate introduces no test breakage.

Closes #1715.

Build-time (scripts/download-models.ts): pin the HuggingFace commit +
SHA-256, verify the artifact after download (and any pre-existing file),
delete + fail hard on mismatch so an unverified model is never bundled.
curl -fL --proto =https; the pinned digest is the authoritative gate.

Load-time (packages/nlp-engine): compute the on-disk GGUF's SHA-256 and
refuse to load on mismatch before LlamaModel::load_from_file, closing the
post-install tamper window. Adds EMBEDDING_MODEL_SHA256, compute_file_sha256,
verify_model_integrity, EmbeddingError::IntegrityError, sha2 dep, and tests.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE

Independent adversarial review (all 7 scrutiny points) returned CONFIRMED-GOOD: the load-time gate runs before load_from_file and is unbypassable (only two load_from_file sites in nlp-engine; the chat one is a separate model, tracked in #1729); Bun streaming + .digest("hex") matches Rust format!("{:x}", …) lowercase; fail-hard + delete is correct; 64 KB stack buffer + sha2 0.10 usage safe; non-optional sha2 dep is correct. Pinned digest verified against the authoritative HF LFS metadata; script verified the real 146 MB model live. Tests green, clippy clean, no test breakage. The one behavior-change note (only pinned Q8_0 bytes load; .f16/override paths resolve but are rejected) is deliberate and now documented on resolve_model_path.

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.

Build-time model download (scripts/download-models.ts) skips SHA-256 verification

1 participant