Skip to content

fix(undo): restore only store snapshots and keep deleted rows deleted - #63

Merged
abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/undo-hardening
Oct 24, 2026
Merged

abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/undo-hardening

Conversation

@WhollyMindless

@WhollyMindless WhollyMindless commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

memory_undo accepts any path, the same class of problem as the viewer restore path in #21. Separately, its default mode brings back rows that were deliberately deleted. I found both while reviewing 0.8.0. #60's store lock doesn't change either one, and this PR is built on top of it.

The problem. All of these reproduced on 0.8.0.

In the default check mode:

  • Resurrection: after memory_forget (or a viewer delete) removes a row, for example a leaked secret, memory_undo on the pre-forget snapshot puts it back. The same happens with merge=true.
  • Any *.logfmt file anywhere on disk can be "restored" and becomes active memory.
  • Passing a live day file (e.g. .../2026-10-23.logfmt) duplicates its rows.

Only with merge=true or force=true (the default check mode refuses these, as your repro showed):

  • A source named <n>-blocklist.logfmt or <n>-canonical.logfmt overwrites the reserved file, because the target is the basename with the stamp stripped.
  • Chained: memory_export to a .logfmt path, then memory_undo on it with force=true, overwrites blocklist.logfmt and un-blocks a memory a human had blocked as a leaked secret, destroying the block audit. Neither step asks the host for permission.

Change (src/undo.ts):

  • Only <stamp>-YYYY-MM-DD.logfmt files inside the store's snapshots/ directory are accepted. That's the same grammar the viewer restore uses since viewer /api/restore joins raw body.name — files outside the store can be promoted into memory #21. Both a bare name from the list and its full path work.
  • The memory root, snapshots/ and the source are realpath-checked, so .., symlinks and Windows junctions can't escape. The target is derived from the snapshot name, must be a regular day file, and is never a symlink or a reserved file.
  • A snapshot row that has a deletions.logfmt audit and is missing from the live file stays deleted in check and merge mode, and the tool reports how many it left out. The audit is read inside the store lock. It doesn't depend on timing, which matters because prune now logs the deletion before it takes the snapshot. force=true remains the explicit way to get rows back, and its safety snapshot from fix: serialize store writes across concurrent sessions #60 is unchanged.
  • The realpath checks run before the lock. The lock and the write use the same plain path your other writers use, so they share the in-process queue (/var vs /private/var on macOS).
  • memory_undo list only shows valid snapshot names. The tool and schema descriptions are updated, so it no longer advertises "or a full path" to anywhere.

The blocklist already wins at load time, so restoring a raw blocked row keeps it inactive. The real hole was overwriting blocklist.logfmt itself.

Tests (test/undo.test.ts):

  • A .. path and a path outside snapshots/ are rejected.
  • Crafted -blocklist/-canonical names can't overwrite reserved files.
  • Export followed by undo can't un-block a memory.
  • A symlink or junction escape is rejected.
  • A forgotten row isn't resurrected in check or merge mode, but is with force.
  • A normal snapshot restore still works.

With the source change reverted, the new tests fail.

Checks (Linux, Bun 1.4.4, on 0.8.1): bun test ./test/ 292 pass, 4 skip, 0 fail, including your new concurrency.test.ts. bun run test:e2e against opencode 2.0.26: 9 pass. tsc --noEmit is clean, and biome lint exits 0 with warnings unchanged from main. On macOS, /var → /private/var is handled because every side is realpath'd before comparing.

Follow-up: your two nits (calendar-date validation, documenting the name filter) are in #76.

🤖 Generated with Claude Code

memory_undo accepted any path. Any *.logfmt file became active memory, a
live day file could be restored over itself, and a source named
`<n>-blocklist.logfmt` or `<n>-canonical.logfmt` overwrote the reserved file,
so export followed by undo could un-block a poisoned memory. Separately,
check and merge mode re-added rows that were deliberately deleted, such as
a forgotten leaked secret.

- Only `<stamp>-YYYY-MM-DD.logfmt` files inside the store's snapshots
  directory are accepted, the same grammar as the viewer's restore (abogartz#21).
  Paths are realpath-checked: no `..`, symlink or junction escapes, and
  the target must be a regular day file, never a symlink or reserved file.
  The lock and the write still use the lexical path every other writer
  uses.
- A snapshot row that has a deletions.logfmt audit and is absent from the
  live file stays deleted in check and merge mode. The audit is read inside
  the store lock. force=true is the explicit recovery path, and the tool
  reports how many rows it left out.
- The list only shows valid snapshot names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve — every scenario reproduced, the containment is the same grammar the viewer has used since #21, and the default-mode resurrection fix is the important part. One calibration request for the body, two nits.

What I reproduced on 0.8.1 (main @ 4643ec4), then re-ran against your branch

Scenario main this PR
memory_undo on </tmp>/attacker.logfmt written to <root>/attacker.logfmt, and memoryFiles() globs *.logfmt → became an active memory row invalid snapshot name
memory_undo on a live day file 2026-10-24.logfmt → ^\d+- strips 2026- → target 10-24.logfmt → row count went 3 → 6, every row duplicated and both files read as memory invalid snapshot name
forget then undo in default (check) mode deleted row came back stays deleted, count reported
same, merge=true came back stays deleted
same, force=true came back comes back (explicit path, unchanged)
snapshots/<name> and ./<name> forms, and <root>/2026-10-24.logfmt via .. accepted; the last one silently produced the duplication above rejected with a clear message
symlink placed at snapshots/99-<day>.logfmt followed snapshot is outside the snapshots directory

Also verified: a restore on a store with no deletions.logfmt does not throw (readLines tolerates the missing file), and the viewer's restore is unaffected because it calls restoreSnapshot(..., "force"). 296 pass / 4 skip / 0 fail; 7 of your tests fail against main's source; tsc clean; biome warnings identical to main.

Please correct in the body (severity calibration)

The two reserved-file items — crafting <stamp>-blocklist.logfmt, and the memory_export → memory_undo chain — are refused by the default check mode in my repro (Undo refused: 0 row(s) added and 1 changed…). They need merge=true or force=true. With force=true they are exactly as bad as you describe: I replaced blocklist.logfmt with export JSONL and a row a human had blocked as a leaked secret became active again, with the block audit destroyed.

Please mark those two as force/merge-gated. It matters: the forget→undo resurrection is reachable in default mode, which is the strongest claim in the PR and shouldn't be diluted by two claims that need an explicit opt-in mode.

Nits

  • SNAPSHOT_NAME = /^(\d+)-(\d{4}-\d{2}-\d{2}\.logfmt)$/ accepts impossible dates — 123-2026-13-45.logfmt passes and would restore to a new, glob-picked-up 2026-13-45.logfmt. Names can only be minted by snapshotFiles() today, so it is not reachable, but validating a real calendar date costs nothing.
  • listSnapshots() now hides any pre-existing snapshot whose name doesn't match the grammar (e.g. an old <stamp>-canonical.logfmt). Intentional, but it silently shrinks the list for someone mid-recovery — worth a line in the tool description or the changelog note.

Design opinion

The deletions.logfmt filter changes undo's contract from "this exact state" to "this state, minus what a human deliberately deleted since", keyed on a store-global append-only audit. I think that is the right trade for a memory store whose default mode is meant to be safe, the live.has(key) escape correctly keeps a re-written row, and reporting deletedOmitted instead of silently dropping is the right touch. The one thing to keep an eye on is that the audit is read in full under the store lock on every non-force restore; fine at current scale, worth revisiting if it ever grows past a few thousand lines.

@abogartz
abogartz merged commit aff9948 into abogartz:main Oct 24, 2026
3 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.8.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Thanks for merging, and for the thorough repro. Your nits crossed with the merge, so they're in #76: snapshot names must carry a real calendar date (2026-13-45 and 2026-02-29 are rejected, 2028-02-29 restores), and the tool description and README say non-standard files in snapshots/ aren't listed or restorable. I also corrected this description: it now separates what the default check mode allows (resurrection, foreign files, live day files) from the two items that need merge/force.

Noted on the full read of deletions.logfmt under the lock. I left it as is for now.

abogartz pushed a commit that referenced this pull request Oct 24, 2026
Review follow-up for #63: snapshot names must carry a real calendar date
(UTC round-trip, leap years included), and the tool description and README
now say that non-standard files in snapshots/ are not listed or restorable.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@WhollyMindless
WhollyMindless deleted the fix/undo-hardening branch October 26, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants