Skip to content

fix(storage): BlobStore TOCTOU — source file read twice, content can change #470

Closed

Description

@Sinity

Problem

BlobStore.write_from_path() (storage/blob_store.py:64-103) opens the source file twice:

  1. First open (line 64-73): reads full content to compute SHA-256 hash, records blob_size
  2. Second open (line 85): reads content again to write into blob store

If the source file is modified by another process between the two reads (e.g., a JSONL log being appended to), the hash stored in the filename corresponds to the old content but the bytes stored on disk are the new content. The blob is now corrupted — verify() would detect the mismatch, but only if called explicitly.

Additional issue in write_from_fileobj() (lines 105-153): deduplication check happens after the full temp-file write (line 135), wasting I/O on content that already exists. Also a TOCTOU between dest.parent.mkdir() and os.replace() — a concurrent process could write the same blob and be silently overwritten.

Affected code

  • storage/blob_store.py:64-103 — write_from_path() double-read window
  • storage/blob_store.py:105-153 — write_from_fileobj() TOCTOU on dedup

Fix direction

  • write_from_path: compute hash during the first (and only) read pass, buffer or tee the content into the blob store in a single pass
  • write_from_fileobj: check deduplication before writing to temp file; use O_EXCL or similar to detect concurrent writes

Discovered during adversarial audit 2026-04-26.


Severity note (2026-04-26): This requires the source file to be actively modified by another process during the import window (between the two reads). Most export files are static — JSON dumps, zip archives, or completed log files. The practical risk is lower than initially assessed. Treat as HIGH, not CRITICAL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:storagetype:fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions