Skip to content

Add load-time SHA-256 integrity gate for the chat/LLM model (companion to embedding gate) #1729

Closed

Description

@mstomar125

Summary

The embedding model now has a load-time SHA-256 integrity gate (ADR-058): EmbeddingService::initialize() verifies the on-disk GGUF against a pinned digest before handing it to native llama.cpp. The chat/LLM model has no equivalent load-time gate.

  • packages/nlp-engine/src/chat/mod.rs (~L202) — load_model takes a caller-supplied path and only does an exists() check before LlamaModel::load_from_file. No digest comparison.
  • The chat models are verified at download time (packages/agent/src/local_agent/model_manager.rs pins per-entry SHA-256 and stream-verifies), so the fetch path is sound. But a model swapped/tampered on disk after download is loaded without re-verification — the same post-install tamper window that the embedding load gate closes.

The chat model is parsed by the same native C++ (llama.cpp) inside the daemon with full authority, so an on-disk-substituted GGUF is both an integrity failure and a memory-safety attack surface.

Why separate from the embedding gate

The embedding model is a single pinned artifact (one digest). The chat models are a catalog (MODEL_CATALOG in model_manager.rs, each with its own pinned sha256), and the loaded model is selected at runtime. A load-time gate must look up the expected digest for the currently selected catalog entry and verify against it before load_from_file.

Acceptance criteria

  • Before LlamaModel::load_from_file in chat/mod.rs, verify the on-disk file against the pinned SHA-256 of the selected catalog entry (reuse the digests already in model_manager.rs)
  • Refuse to load on mismatch with a clear integrity error (mirror EmbeddingError::IntegrityError)
  • Handle models with no catalog entry / user-supplied paths explicitly (fail closed or documented escape hatch)

Context

Surfaced during review of the embedding-model load gate. Threat T9, ADR-058 (Model Artifact Integrity Verification), security-posture.md.

Activity

  1. mstomar125 commented on Jul 27, 2026

    @mstomar125
    ContributorAuthor

    Resolved in #1749 (merged to main as e4893537).

    What shipped

    The chat/LLM model now has the same load-time SHA-256 integrity gate as the embedding model. Before native llama.cpp parses the GGUF, the on-disk file is verified against the pinned digest of the selected catalog entry; a swapped/tampered model is refused with ChatError::IntegrityError.

    • chat/mod.rs — load_model(path, expected_sha256: Option<&str>) verifies before load_from_file (mirrors the embedding gate). None = documented escape hatch for a user-supplied / non-catalog model (loads with a warning — nothing to verify against).
    • config.rs — extracted a generic, case-insensitive verify_file_sha256(path, expected); verify_model_integrity delegates to it.
    • agent/model_manager.rs — expected_sha256_for_filename resolves the pinned digest from the catalog by GGUF basename; agent/inference.rs resolves it at load and passes it down, preserving the agent → nlp-engine dependency direction.

    Verification

    Four tests (digest match/mismatch; catalog-filename → digest / None; a wiring-invariant test that every catalog basename resolves to its pinned digest; and load_model → IntegrityError on mismatch before native load). nlp-engine 99 / agent 301 lib tests pass; clippy clean; full pre-push gate green. Independent adversarial review: no bypass / correctness bug / regression — the only production chat-load path always builds the path from entry.filename, so a catalog model always resolves Some(digest) and is verified; the daemon surface takes a model_id (never an arbitrary path), so the None branch is unreachable today.

    Companion to the embedding-model load gate (#1715); Threat T9, ADR-058.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions