Skip to content

B8: Close TOCTOU in bulk_update / bulk_create_hierarchy (read escapes the transaction)Β #1258

Closed

Description

@malibio

Spun off from architectural-audit tracking issue #1228, finding B8. Backend-only; parallel-safe with all current frontend and #1243 work.

Background (from audit #1228)

B8 β€” Possible TOCTOU in bulk ops 🟑
bulk_update_nodes and bulk_create_hierarchy call get_node() before / outside the surrounding transaction β†’ read-then-update race; bulk op can partially complete.
Action: move reads inside the transaction or document the partial-failure contract.

Verified current state

packages/core/src/db/sqlite_store.rs:

bulk_update (line ~1611)

let tx = self.db.transaction().await?;        // 1626: open transaction
for (id, update) in &updates {
    let current = self.get_node(id).await?     // 1633: READ via self.db POOL β€” outside tx!
        .ok_or_else(|| anyhow!("Node not found: {}", id))?;
    let updated_content = update.content.clone().unwrap_or(current.content);
    let updated_node_type = update.node_type.clone().unwrap_or(current.node_type);
    tx.execute("UPDATE node SET content=?1, node_type=?2, version=version+1, modified_at=?3 WHERE id=?4", ...)  // 1641: WRITE via tx
}
tx.commit().await?;

Two distinct problems:

  1. The read escapes the transaction. get_node (line ~470) runs on self.db (the pool), not on tx. So the "read current values to fill unspecified fields" happens against connection state that is not the transaction's snapshot. Under SQLite's serialized writes the window is small, but the read-then-write is still not atomic with respect to the surrounding tx.
  2. No OCC/version check in the bulk path. bulk_update reads current.content/current.node_type to preserve unspecified fields, then unconditionally version = version + 1. A concurrent single-node update between the read (1633) and the tx.execute (1641) is clobbered with no VersionConflict β€” unlike the single-node update_node path which is version-checked. The "current" snapshot can be stale.

bulk_create_hierarchy (line ~1659)

Inserts within tx (lines ~1696+) and calls self.validate_node_type(node_type) (line ~1684) inside the loop. validate_node_type may itself touch the DB (schema lookup) outside tx. Lower risk than bulk_update (it's INSERTs of new ids, not read-modify-write of existing rows), but the validation read should be confirmed to not depend on uncommitted state, and a partial-failure mid-loop before commit should roll back cleanly (it does β€” tx drops without commit on the ? early-return β€” but document this).

The task

Pick ONE of the two approaches the audit allows, per function:

Option A β€” make the read transactional (preferred for bulk_update). Read current through tx (tx.query(...) against the same transaction) instead of self.get_node() on the pool, so the read-modify-write is one atomic unit. This closes the TOCTOU window.

Option B β€” restructure to avoid the read (cleaner if feasible). bulk_update only reads current to preserve content/node_type when the update leaves them unspecified. A single SQL UPDATE ... SET content = COALESCE(?1, content), node_type = COALESCE(?2, node_type), version = version + 1 ... does the "keep existing if not provided" entirely in SQL β€” no read needed, no TOCTOU. This is the cleanest fix and removes the read entirely. Use it unless an update field can't be expressed as COALESCE.

OCC decision (call it explicitly). Decide and document whether bulk_update should be version-checked like single-node update_node:

  • If callers of bulk_update are trusted/internal batch paths where last-write-wins is acceptable β†’ document that bulk_update is intentionally non-OCC (a deliberate fast-path), so the absence of version checking is a contract, not an oversight.
  • If any caller needs conflict detection β†’ thread a per-node expected version through and abort the transaction on mismatch (VersionConflict), matching the single-node contract.
  • Check the callers (NodeService::bulk_update, the daemon update_nodes_batch RPC) to decide which.

bulk_create_hierarchy: confirm validate_node_type doesn't read uncommitted state, and document the partial-failure contract (early-return before commit rolls back the whole batch β€” verify and state it in a doc comment).

Acceptance criteria

  • bulk_update's read-modify-write is atomic: either the read goes through tx (Option A) or the read is eliminated via in-SQL COALESCE (Option B). No self.get_node()/pool read between tx open and tx.commit.
  • The OCC behavior of bulk_update is decided and documented in a doc comment (either version-checked, or explicitly last-write-wins-by-design with rationale).
  • bulk_create_hierarchy: validation reads confirmed not to depend on uncommitted state; partial-failure/rollback contract documented in a doc comment.
  • No #[allow(...)], no lint suppression. cargo clippy --workspace --all-targets -- -D warnings clean.

Testing

  • Test that bulk_update with partial field updates (only content, only node_type) preserves the unspecified field correctly (proves the COALESCE/transactional-read path works).
  • Test that a failed bulk op (one bad id mid-batch) rolls back ALL changes β€” no partial commit (the audit's "bulk op can partially complete" concern).
  • If OCC is added: test a version mismatch aborts the whole bulk update.
  • cargo test -p nodespace-core + cargo test --workspace green.

Coordination

Standard NodeSpace startup / PR sequence

EnterWorktree({name: "issue-<N>-bulk-op-toctou"}); bun install; baseline cargo test -p nodespace-core; bun run gh:assign/gh:status. Before PR: cargo test --workspace + bun run test:all (no new failures), bun run quality:fix (revert unrelated cargo fmt churn via git status), bun run gh:pr <N>, /pragmatic-code-review, then ExitWorktree + gh pr merge --squash --delete-branch.


Spun off from #1228 (architectural audit), finding B8.

Activity

  1. self-assigned this
    on May 30, 2026
  2. added a commit that references this issue on May 30, 2026
    8a71df2
  3. added a commit that references this issue on Jun 22, 2026
    be09e34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions