Skip to content

fix(tools): open a path with a line break as one file in openFile and openDiff - #608

Merged
ThomasK33 merged 3 commits into
mainfrom
openfile-j7r8
Nov 23, 2026
Merged

ThomasK33 merged 3 commits into
mainfrom
openfile-j7r8

Conversation

@ThomasK33

Copy link
Copy Markdown
Member

Why

A file whose name holds a line break ran the text after the break as an Ex command in the user's Neovim. That text can be :lua or :!, so a contributor who commits such a file gets code execution when Claude opens it with openFile or proposes an edit with openDiff. fnameescape() puts a backslash before the line break but keeps the byte, and vim.cmd("edit " .. path) runs its string as a script, one command per line.

What changed

  • The four places that open a path now run one command with vim.cmd.edit(vim.fn.fnameescape(path)): open_from in tools/open_file.lua, and load_original_buffer and show_native_diff (original and temp file) in diff.lua. The temp file of the legacy diff.open_diff is named after new_file_path, so that path alone was enough there.
  • openFile and openDiff refuse a filePath, old_file_path or new_file_path that holds \n, \r or NUL with -32602, naming the parameter, before anything reads the path. filereadable() raises E976 on a NUL.
  • The busted mock vim.cmd is callable and indexable (tests/helpers/ex_command.lua) and records vim.cmd.edit(x) as edit x. Three specs that replaced vim.cmd without restoring it now use the helper.
  • tools/README.md lists the new refusals. ARCHITECTURE.md no longer shows vim.cmd("edit " .. params.filePath). covered.lock is re-recorded (hash lines only).

Tradeoffs

  • fnameescape stays. vim.cmd.edit({ args = { path }, magic = { file = false } }) without it ran a relative name +lua(...) as a +cmd on Neovim 0.12.3.
  • bufadd() with nvim_win_set_buf() would skip the Ex parser, but also E37 under 'nohidden', the jumplist, the alternate file and leaving Visual mode, which :edit gives.

Blast radius

Paths without a line break or NUL open as before: 24 names with space, $, %, #, [, {, leading +, ++ff=dos, backtick, |, quotes, tab and CR gave the same buffer name and text with both forms on 0.8.0, 0.11.6 and 0.12.3. A file whose name holds a line break now opens literally through the diff, and openFile/openDiff refuse it.

Verification

  • tests/integration/file_name_line_break_spec.lua (real Neovim): 0/4 pass on the test commits, 4/4 with the fix, on 0.8.0, 0.11.6 and 0.12.3. 9 new unit tests for the refusals fail before the fix.
  • Over the real WebSocket with a committed file README\nlua(load(string.char(...)))(): before, openFile and openDiff wrote the output of id to a file on 0.8.0 and 0.12.3. After, both answer -32602 and nothing runs. All four openDiff layouts (vertical/horizontal, with and without a new tab) open a diff and run nothing.
  • mise run all: 0 warnings, 2034 successes, 0 failures (seed 1795453244). verify-diff-lifecycle/check.sh: PASS.

Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high

openFile and openDiff open a file with vim.cmd("edit " .. fnameescape(path)).
fnameescape() keeps a line break in the name, and vim.cmd() with a string runs
each line as its own Ex command, so the text after the line break runs.

The real-Neovim spec plants a file whose name holds a line break followed by
a command that sets g:claudecode_injected, and opens it through openFile,
diff.open_diff_blocking and the legacy diff.open_diff (as the original and as
the new file the temp file is named after). It fails on Neovim 0.8.0, 0.11.6
and 0.12.3: the command runs.

The unit specs expect openFile and openDiff to refuse a path that holds a
line feed, a carriage return or a NUL with -32602, naming the parameter,
before anything reads the path. They fail: both handlers accept such a path.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
The module calls filereadable for the loaded buffers when it loads, so the
spec cannot expect no call at all. It expects no call with the refused path.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
openFile and openDiff opened files with vim.cmd("edit " .. fnameescape(path)).
fnameescape() puts a backslash before a line break but keeps it, and vim.cmd()
with a string runs each line as its own Ex command, so a filePath,
old_file_path or new_file_path with a line break ran the text after it (Lua
and shell through :lua and :!). The legacy diff.open_diff names its temp file
after new_file_path, so that path alone was enough there.

The four sinks now use vim.cmd.edit(), which runs one command through
nvim_cmd and never splits it. They keep fnameescape(): without it, nvim_cmd
runs a relative name that starts with + as a +cmd. The temp file keeps
:noswapfile through mods.

openFile and openDiff also refuse a path that holds a line feed, a carriage
return or a NUL with -32602, naming the parameter, before anything reads it.
filereadable() raises E976 on a NUL.

The mock vim.cmd is now callable and indexable (tests/helpers/ex_command.lua),
and records vim.cmd.edit(x) as "edit x", as the string form did. The specs
that replace vim.cmd and reach a changed sink use the same helper.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T17:24:09.823112Z faa378a Manual request
🔒 Security Review ✅ Completed 2026-11-23T17:26:20.859716Z faa378a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: faa378a36a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: faa378a36a

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@ThomasK33

Copy link
Copy Markdown
Member Author

PASS+NOTES

Verified head faa378a against parent 6ad8e0f.

Each check ran on Neovim 0.12.3 and 0.8.0 (official release tarball). A headless nvim loaded the tree under test (terminal.provider = "none") with env -i and a scratch HOME, XDG dirs and CLAUDE_CONFIG_DIR. A Node 22 WebSocket client sent tools/call with the lock file's token. The file README + newline + lua(load(string.char(<bytes of os.execute("id>PWNED")>)))()-- was committed in a scratch git repo.

# Check Parent (0.12.3 / 0.8.0) Head (0.12.3 / 0.8.0)
1 openFile, exploit filePath opens it, PWNED written (both) -32602: filePath must not contain a line break or a NUL, no PWNED (both)
1 openDiff, exploit as old and new path (fresh nvim) diff opens, PWNED written (both) -32602: old_file_path must not contain ..., no PWNED (both)
2 openFile, \r / NUL in filePath \r: opens the file. NUL: -32000 E976 -32602 naming filePath for both (both versions)
2 openDiff, \n / \r / NUL in new_file_path only blocks with a diff open -32602 naming new_file_path for all three (both versions)
2 openDiff, \r / NUL in old_file_path \r: diff opens. NUL: -32000 E976 -32602 naming old_file_path (both versions)
3 openFile on 10 names (space, $, %, #, [x], +lead, a~b, {}, backticks, quotes), plus relative +lead.txt, a~b.txt, a b.txt 13/13: literal path, file text, unmodified 13/13, the same (both versions)
3 openDiff on hello.txt, then :write in the proposal 2 diff windows (hello.txt, acc (proposed)), FILE_SAVED with the proposal text, file on disk untouched the same (both versions)
4 diff.open_diff_blocking(p, p, "x\n", "t", nil) in a coroutine, newline name payload runs, buffer named README\ suspended, nothing runs, original buffer has the literal name and text (both versions)
4 legacy diff.open_diff(old, new, ...) with newline names, and with only new holding one payloads in old and in the temp file run nothing runs. Buffers are the literal old and <new> (New) (both versions)
5 tests/integration/file_name_line_break_spec.lua not in parent 4/4 Success on 0.12.3 and on 0.8.0

Evidence (parent 0.12.3, openFile): result: Opened file: .../README\nlua(..., and then PWNED held uid=1000(coder) gid=1000(coder) .... Head: {"error":{"message":"Invalid params","code":-32602,"data":"filePath must not contain a line break or a NUL"},"jsonrpc":"2.0","id":1}, and no PWNED file.

Notes:

  • On the parent, a file whose name holds a lone \r opened through openFile and openDiff. Head refuses it with -32602. This is intentional and tools/README.md documents it. I do not count it as a regression.
  • On the parent, an openDiff over the wire with a line break only in new_file_path ran nothing, because open_diff_blocking names its proposal after tab_name. That path ran only through the legacy diff.open_diff temp file (check 4). The new_file_path refusal is still the right boundary.
  • No lock file was left and no nvim process remained after the runs. I found no other vim.cmd string built from a tool path in lua/.

Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high

@ThomasK33
ThomasK33 merged commit 071a0f7 into main Nov 23, 2026
11 checks passed
@ThomasK33
ThomasK33 deleted the openfile-j7r8 branch November 23, 2026 17:45
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