Skip to content

fix: add integrity verification to RAG cache pickle loading (#447) - #722

Merged
kovtcharov merged 1 commit into
amd:mainfrom
albiol2004:fix/pickle-deserialization-447
Apr 9, 2026
Merged

kovtcharov merged 1 commit into
amd:mainfrom
albiol2004:fix/pickle-deserialization-447

Conversation

@albiol2004

Copy link
Copy Markdown
Contributor

Summary

  • Add GAIA_CACHE_V1 format header and SHA-256 checksum verification
    to cache files before deserializing with pickle.loads()
  • Reject cache files exceeding 500 MB size limit
  • Delete corrupted or outdated cache files and re-index automatically
  • Old-format caches (pre-header) are detected and rebuilt on first load

Context

pickle.load() was called on cached RAG data without any validation
(#447). A corrupted cache file would crash the RAG system permanently
with no recovery path. This adds integrity verification at read time
and writes a self-verifying format at cache time.

Note: the diff includes an indentation change (~80 lines dedented)
because data is now read from the file and validated before processing,
so the processing logic moved outside the with open() block.

Test plan

  • Corrupted cache files are detected and deleted automatically
  • Cache load failures trigger clean re-index, not crash
  • Checksum mismatch is detected and triggers re-index
  • Oversized cache files are rejected
  • Old-format (pre-header) cache files are migrated on first load
  • All 33 tests pass (python -m pytest tests/test_rag.py)
  • All lint checks pass (python util/lint.py --all)

  Add format version header (GAIA_CACHE_V1), SHA-256 checksum verification,
  file size limits, and auto-recovery for corrupted or outdated cache files.
  Corrupted cache files are now deleted and re-indexed automatically.
  Old-format caches (pre-header) are detected and rebuilt on first load.

  Fixes amd#447
@github-actions github-actions Bot added rag RAG system changes tests Test changes performance Performance-critical changes labels Apr 8, 2026
@itomek

itomek commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

@claude review this PR

@github-actions

github-actions Bot commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @itomek's task —— View job


PR Review: Integrity Verification for RAG Cache

  • Read pr-diff.txt and pr-files.txt for context
  • Review src/gaia/rag/sdk.py changes
  • Review tests/test_rag.py changes
  • Provide comprehensive code review feedback

Summary

This PR adds integrity verification to RAG cache pickle loading, addressing issue #447. The implementation adds:

  • Format header (GAIA_CACHE_V1) and SHA-256 checksum verification
  • 500 MB size limit for cache files
  • Automatic deletion and re-indexing for corrupted/outdated caches

Review

🟢 Strengths

  1. Good defensive approach: Verifying checksum before pickle.loads() mitigates corruption and naive tampering (src/gaia/rag/sdk.py:1832-1838)

  2. Clean recovery path: Failed cache loads now delete the corrupted file and re-index automatically (src/gaia/rag/sdk.py:1933-1940)

  3. Backward compatibility: Old-format caches (pre-header) are detected and rebuilt transparently

  4. Comprehensive tests: Four new test cases cover corrupted files, checksum mismatch, oversized files, and old format migration (tests/test_rag.py:284-411)

  5. Proper nosec annotation: The # nosec B301 comment correctly documents that pickle usage is intentional after verification

🟡 Suggestions (Non-blocking)

  1. Consider constant-time comparison for checksum (src/gaia/rag/sdk.py:1833):

    import hmac
    if not hmac.compare_digest(actual_checksum, stored_checksum):

    This prevents timing attacks, though the risk is low in this file-based context.

  2. Log the specific validation failure reason (src/gaia/rag/sdk.py:1927-1928):
    Currently logs Cache load failed: {e}, which includes the message. Consider adding self.log.debug() for the full exception traceback to aid debugging.

  3. Minor: readline(128) buffer size (src/gaia/rag/sdk.py:1826,1829):
    The header is 14 bytes and the checksum line is 65 bytes (64 hex + newline). The 128-byte limit is fine, but a comment explaining this magic number would help future maintainers.

🟢 Security Notes

  • Pickle is still used, but the SHA-256 checksum provides data integrity verification. An attacker would need write access to the cache directory to craft a malicious payload, and the checksum would need to match. This is reasonable for a local cache.
  • The checksum is computed on the serialized pickle bytes, not the original objects, which is correct.

Verdict

Approve - This is a well-implemented security improvement. The test coverage is thorough and the implementation follows defensive programming practices.


@albiol2004 albiol2004 closed this Apr 8, 2026
@albiol2004
albiol2004 deleted the fix/pickle-deserialization-447 branch April 8, 2026 13:36
@albiol2004
albiol2004 restored the fix/pickle-deserialization-447 branch April 8, 2026 13:39
@albiol2004 albiol2004 reopened this Apr 8, 2026
@itomek itomek linked an issue Apr 8, 2026 that may be closed by this pull request
3 tasks
@kovtcharov
kovtcharov added this pull request to the merge queue Apr 9, 2026
Merged via the queue into amd:main with commit 6915528 Apr 9, 2026
41 checks passed
@itomek itomek mentioned this pull request Apr 10, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Apr 10, 2026
Release notes for v0.17.2. See
[docs/releases/v0.17.2.mdx](https://github.com/amd/gaia/blob/v0.17.2-release/docs/releases/v0.17.2.mdx)
for the full body.

**Headline features:**
- One-click desktop installers for Windows, macOS, and Linux (#731 +
#743)
- Customizable agents and Builder Agent (#720)
- MCP tool execution visibility in Agent Activity panel (#717)

**Security:** RAG cache integrity verification (#722, closes #447)

**Bug fixes:** drag-and-drop upload in any browser (#729), Linux install
fixes (#743)

**Release & CI:** unified publish workflow (#715), Claude review on fork
PRs (#685), npm OIDC fix (#683), webui package.json sync (#682)

Full changelog: v0.17.1...v0.17.2

---------

Co-authored-by: Tomasz Iniewicz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance-critical changes rag RAG system changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix pickle deserialization vulnerability in RAG cache

3 participants