Repository navigation
fix(doctor): bound legacy session store read with size cap and stat-failure fix - #110772
Conversation
|
Codex review: needs maintainer review before merge. Reviewed July 19, 2026, 7:00 AM ET / 11:00 UTC. Summary PR surface: Source +52, Tests +35. Total +87 across 2 files. Reproducibility: Source-reproducible: the pre-change path-based reader permits the FIFO and read-after-check scenarios described in the PR, and the PR supplies after-fix terminal evidence. I did not independently execute a FIFO reproduction in this read-only review. Review metrics: none identified. Stored data model Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Next step before merge
Security Review detailsBest possible solution: Merge the focused descriptor-bound reader after ordinary maintainer review, retaining uncapped migration compatibility and the targeted unreadable-store behavior. Do we have a high-confidence way to reproduce the issue? Source-reproducible: the pre-change path-based reader permits the FIFO and read-after-check scenarios described in the PR, and the PR supplies after-fix terminal evidence. I did not independently execute a FIFO reproduction in this read-only review. Is this the best way to solve the issue? Yes. Binding open, validation, and bounded reads to one descriptor is the narrowest maintainable fix for both special-file blocking and the size-validation race while preserving valid legacy stores. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 104691f82231. Label changesLabel changes:
Label justifications:
Evidence reviewedPR surface: Source +52, Tests +35. Total +87 across 2 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
Review history (22 earlier review cycles; latest 8 shown)
|
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
@clawsweeper re-review Updated PR with descriptor-bound read fixes (TOCTOU eliminated) and test evidence. Please re-review. |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
@clawsweeper re-review All P1 findings addressed: descriptor-bound reads eliminate TOCTOU, tests pass, PR body updated with evidence. Please re-review. |
fdae8c9 to
c4a73c4
Compare
|
@clawsweeper re-review Rebased onto latest main and force-pushed. The previous |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
c4a73c4 to
ec67364
Compare
|
@clawsweeper re-review Force-pushed to latest main again. All required checks are green on head ec67364f; no failures. Please re-evaluate for ready-for-maintainer-look. |
5124cea to
fee2d71
Compare
fee2d71 to
489314a
Compare
|
@clawsweeper re-review Added O_NONBLOCK to the legacy session store open path so a substituted FIFO cannot block the CLI; the descriptor-bound fstat rejects non-regular files. PR body now includes a FIFO rejection proof in addition to the descriptor-growth boundary proof. |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
statSync with throwIfNoEntry: false returns undefined for paths whose parent is a regular file (ENOTDIR) rather than throwing. When a store appears missing but its parent directory is not a directory, report store_unreadable instead of silently treating it as an absent store.
Replace statSync-then-readFileSync with openSync/fstatSync/readFileSync on the same file descriptor to eliminate the TOCTOU race where a file can change between size validation and read.
… store migration Replace readFileSync(fd, 'utf-8') — which reads to EOF — with a descriptor-bound loop that reads at most the byte count validated by fstatSync. This prevents same-inode growth after fstat from bypassing the 50 MiB per-store cap and causing OOM.
489314a to
cbb7055
Compare
|
Merged via squash.
|
…ailure fix (openclaw#110772) * fix(doctor): bound legacy session store read with size cap * fix(doctor): handle ENOTDIR in legacy store stat preflight statSync with throwIfNoEntry: false returns undefined for paths whose parent is a regular file (ENOTDIR) rather than throwing. When a store appears missing but its parent directory is not a directory, report store_unreadable instead of silently treating it as an absent store. * fix(doctor): use descriptor-bound read for legacy session store files Replace statSync-then-readFileSync with openSync/fstatSync/readFileSync on the same file descriptor to eliminate the TOCTOU race where a file can change between size validation and read. * fix(doctor): bound descriptor read to validated byte count in session store migration Replace readFileSync(fd, 'utf-8') — which reads to EOF — with a descriptor-bound loop that reads at most the byte count validated by fstatSync. This prevents same-inode growth after fstat from bypassing the 50 MiB per-store cap and causing OOM. * style(doctor): add braces for curly lint rule * fix(doctor): remove hard 50 MiB cap to preserve legacy migration compatibility * fix(doctor): open legacy session store with O_NONBLOCK to prevent FIFO blocking * refactor(doctor): reuse bounded descriptor reader * fix(doctor): report descriptor stat failures --------- Co-authored-by: OpenClaw Agent <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
What Problem This Solves
The
readLegacySessionRecordsfunction insrc/commands/doctor-session-sqlite.tsreads legacy JSON session store files duringopenclaw doctorwith a path-basedfs.readFileSync(path). A malicious or accidental FIFO substitution at that path could block the CLI indefinitely, and a file swapped between any path-based size check and the read could cause unbounded memory consumption.Why This Change Was Made
Doctor migration runs during
openclaw doctor, regularly invoked by users. The fix opens the file first with a pinned descriptor, validates the descriptor as a regular file, and reads no more than the validated byte count. This eliminates both the FIFO-blocking availability risk and the Time-of-Check-Time-of-Use (TOCTOU) race where a different file could be consumed.What Changed
Replaced the path-based read with a descriptor-bound, nonblocking, byte-capped read in
readLegacySessionRecords:fs.openSync(path, O_RDONLY | O_NONBLOCK)(POSIX) /"r"(Windows) — Opens the file first without blocking on FIFOs.fs.fstatSync(fd)— Stats the descriptor (not the path) to confirm it is a regular file and to learn its size.storeStat.sizebytes from the same fd, so same-inode growth after fstat cannot be consumed.fs.closeSync(fd)— Closes the descriptor viatry/finally, guaranteeing cleanup.All error paths (ENOENT, EACCES, non-regular file) produce equivalent
store_unreadableissues as before. The parent-directory validation for missing stores is preserved. No new hard size cap is introduced, so existing valid legacy stores of any size continue to migrate.User Impact
O_NONBLOCKopen returns immediately; FIFOs are rejected by fstatEvidence
All doctor-session-sqlite tests pass (2 skipped, platform-specific):
Real Behavior Proof
Valid legacy store migration
Ran the production
openclaw doctor --session-sqlite inspectcommand against a real legacy session store file using the branch build. Path redacted to<tmp>.{ "targets": [{ "storePath": "<tmp>/valid-sessions.json", "issues": [], "legacyEntries": 1, "sqliteEntries": 0 }], "totals": { "issues": 0, "legacyEntries": 1, "targets": 1 } }The valid 93-byte store is read successfully and reports one legacy entry.
FIFO rejection without blocking
A focused runtime script using the same
O_NONBLOCKdescriptor-acquisition strategy confirms that a substituted FIFO is rejected promptly:Output:
The FIFO path is rejected in milliseconds because
O_NONBLOCKpreventsfs.openSync()from blocking for a writer; the descriptor-boundfstat()sees a FIFO and throws.Descriptor-growth boundary proof
The bounded read loop consumes at most the byte count validated by
fstatSync. A runtime script exercises this by opening a valid store, growing it afterfstatSync, and confirming only the original validated bytes are read:Output:
Even though the file grew by 10 MiB on the same inode, the reader stops at the fstat-validated size and the JSON object still parses correctly.