Skip to content

Harden generated-file writes against path escapes and data loss - #20

Merged
adityaramkumar merged 1 commit into
mainfrom
claude/safe-file-writes
Oct 22, 2026
Merged

adityaramkumar merged 1 commit into
mainfrom
claude/safe-file-writes

Conversation

@adityaramkumar

Copy link
Copy Markdown
Owner

Summary

Three ways a build could damage files outside its own output:

  • Path traversal. Path(output_dir) / rel_path accepted LLM-supplied paths like /etc/x or ../../x. New resolve_output_path() rejects any path that resolves outside output_dir. A node with a bad path fails before any of its files are written, and the cascade rolls back.
  • Hand-written files lost on rollback. Only files a previous build generated were backed up. If the model wrote to an untracked hand-written file, rollback deleted it because there was no backup. Now every target path is backed up right before it is written. backup() keeps the first copy of a path, so the backup still holds the pre-build original when two nodes in a cascade write the same file.
  • Ctrl-C mid-build. KeyboardInterrupt skipped rollback, which left half-written files and stale backups that the next build's backup() would treat as originals. The cascade now rolls back on any BaseException and re-raises. build also refuses to start while .specdiff/.backup/ exists from a killed run, so those originals aren't overwritten.

The per-node loop moved into _build_nodes() so that every failure path goes through one rollback().

Testing

  • pytest tests/: 262 passed (9 new tests in TestResolveOutputPath and TestWriteSafety)
  • ruff check / ruff format --check: clean

Generated by Claude Code

- Reject agent-supplied paths that resolve outside output_dir before
  anything is written.
- Back up every file a build overwrites, including hand-written files
  the hash map never tracked, so rollback restores them instead of
  deleting them.
- Keep the first backup of a path so a cascade where two nodes write
  the same file still restores the pre-build original.
- Roll back on KeyboardInterrupt and other BaseExceptions, and refuse
  to build while a leftover .specdiff/.backup/ from a killed build exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FAykpfqqWA2GFjcC1p6FRy
@adityaramkumar
adityaramkumar merged commit aca64bb into main Oct 22, 2026
2 checks passed
@adityaramkumar
adityaramkumar deleted the claude/safe-file-writes branch October 22, 2026 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants