Skip to content

fix(file): do not collapse missing path components onto ancestor files - #67

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-management-1abe
Draft

fix(file): do not collapse missing path components onto ancestor files#67
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-management-1abe

Conversation

@cursor

@cursor cursor Bot commented Aug 15, 2026

Copy link
Copy Markdown

Bug and impact

With sandbox workspace_only enabled (the documented default), write_file on a nested path that does not exist yet could silently destroy an existing workspace file:

write_file path=$workspace/important.md/nested.txt content=PWNED
# important.md is a regular file → truncated to "PWNED"

write_file path=$workspace/missing_dir/notes.md content=PWNED
# notes.md already exists at the workspace root → overwritten with "PWNED"

The same ancestor collapse made read_file on secret.env/x return the contents of secret.env.

Agent-generated nested paths (or a typo that treats a file as a directory) are a realistic trigger. The tool returned {"status":"ok"} while clobbering the wrong file.

Root cause

path_within_workspace walks dirname until an existing ancestor realpaths, which is correct as a membership check. file_write then used that ancestor as the fopen target: if it was a regular file, it opened it with "w"; otherwise it appended only the original basename. Missing intermediate directories therefore collapsed onto a parent file with the same name.

Fix

Resolve the intended write path separately:

  • existing file → write that canonical path
  • new file → require the immediate parent to exist as a directory under the workspace, then write parent/basename

read_file / list_dir now realpath the requested path itself instead of opening the ancestor.

Validation

  • make CI=true test_file && ./build/test_file — 34 tests, 0 failed
  • New cases: write/read through a file used as a directory must fail and preserve important.md; write to missing_dir/notes.md must fail and preserve workspace notes.md
Open in Web View Automation 

write_file treated an existing ancestor as the fopen target when the
requested nested path did not exist, truncating a file used as a directory
or overwriting a same-named file in a parent directory.

Co-authored-by: esadrianno <[email protected]>
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.

1 participant