Skip to content

fix(terminal): keep the cwd one argument in external_terminal_cmd - #607

Merged
ThomasK33 merged 3 commits into
mainfrom
fix/external-cwd-one-argument
Nov 23, 2026
Merged

ThomasK33 merged 3 commits into
mainfrom
fix/external-cwd-one-argument

Conversation

@ThomasK33

Copy link
Copy Markdown
Member

Why

With terminal.provider = "external" and the documented two-%s template ("alacritty --working-directory %s -e %s"), external.open pasted vim.fn.getcwd() into the template and then split the whole string with utils.parse_command. A folder name from a cloned repo or archive was parsed as arguments: a folder named demo -e sh -c 'touch PWNED' x made the terminal run sh -c 'touch PWNED' (CWE-88). A folder with a space also broke the launch.

What changed

  • external.open formats a marker in place of the cwd, splits, and only then puts the cwd into the one argument that holds the marker. The cwd reaches jobstart as one argument, byte-equal to getcwd().
  • That argument must be the cwd slot alone (%s, quoted or not) or --option=%s. Anywhere else, such as sh -c 'cd %s && ...', the template is refused with one ERROR notification, because the program that gets that argument would parse the folder name again. A function covers that form.
  • README and lua/claudecode/terminal/README.md state the rule.

Scope

Only the two-%s string template. The one-%s template, the command slot, the function form and the other providers behave as before.

Tradeoffs

  • Shell-quoting the cwd before the split was rejected: with a sh -c '...' template, the folder x;touch${IFS}PWNED;: still ran.
  • Splitting the template first and filling the command slot per argument was rejected: it changed gnome-terminal -e '%s' from one argument into several words.

Blast Radius

A two-%s template with the cwd inside a larger argument now refuses to launch, with an error that names the fix.

Verification

  • The first commit adds specs that fail on main: 14 folder names times 2 templates, plus the refusal. All 43 external_spec specs pass on the fix.
  • Real Neovim 0.12.3, :cd into 9 crafted folders times 3 templates, then :ClaudeCode, with a stub terminal that logs its argv and runs what follows -e. main: the attacker's command ran 5 times, and 17 launches had a wrong argv or did not start. This branch: 0 attacker runs, 18 correct launches, 9 refused (the sh -c template).
  • mise run all passes.

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

…uments

`external.open` pastes `vim.fn.getcwd()` into a two-`%s`
`external_terminal_cmd` with `string.format` and then splits the whole
string. These specs run folder names with spaces, quotes, backslashes,
shell syntax and `%s` through `alacritty --working-directory %s -e %s`
and `alacritty --working-directory=%s -e %s`, and expect the cwd to stay
one argument. A folder named `demo -e sh -c 'touch PWNED' x` now becomes
extra alacritty arguments, so 14 of these specs fail. A cwd `%s` inside
a larger argument (`sh -c 'cd %s && ...'`) is expected to be refused,
which fails too.

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

Signed-off-by: Thomas Kosiewski <[email protected]>
With two `%s` placeholders, `external.open` pasted `vim.fn.getcwd()`
into the template with `string.format` and then split the whole string
with `utils.parse_command`. A folder named
`demo -e sh -c 'touch PWNED' x` became extra terminal arguments, so
`alacritty --working-directory %s -e %s` ran `sh -c 'touch PWNED'`
(CWE-88), and a folder with a space split into two arguments.

Format a marker in place of the cwd, split, then put the cwd into the
one argument that holds the marker. That argument must be the marker
alone or `--option=` and the marker. Anywhere else, such as in
`sh -c 'cd %s && ...'`, the program that gets the argument would parse
the folder name, so the template is refused with one ERROR
notification. A function covers that form.

The one-placeholder path and the command slot are unchanged.

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

Signed-off-by: Thomas Kosiewski <[email protected]>
@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-23T16:16:36.916470Z c2e8616 Manual request
🔒 Security Review ✅ Completed 2026-11-23T16:18:27.141307Z c2e8616 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. 🎉

Reviewed commit: c2e8616694

ℹ️ 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: c2e8616694

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 at c2e8616

I'm an independent verifier and did not write this PR. I ran real Neovim 0.12.3 headless (-u NONE -i NONE, XDG and CLAUDE_CONFIG_DIR in a scratch dir), with base 6ad8e0f and head c2e8616 in detached worktrees. Each run did setup({terminal={provider="external"}}), then :cd into a crafted folder, then :ClaudeCode or :ClaudeCode --resume. The terminal stubs record their argv. The alacritty-like stub then execs whatever follows the first -e, and the gnome-like stub shell-splits the one -e string and execs it. A fake claude records its argv and cwd. vim.fn.jobstart was wrapped to log the argv bytes it got. Base and head ran every case on the same paths.

What I ran base head
253 cases: 19 folder names × 11 templates, plus --resume on 4 of the folders
Attacker command ran (a PWNED* file appeared) 20 0
Two-%s cases (207) with the cwd as exactly one argument byte-equal to getcwd() (whole, or -opt= + cwd) 72 161
Two-%s cases refused with exactly one ERROR notify, no jobstart, nothing started 0 46 (all of sh -c 'cd %s && exec X -e %s' and X --a=b=%s -e %s)
Plain folders, and every one-%s template (X -e %s, G -e '%s') with any folder: argv, errors and claude argv same as base — 0 diffs
Command slot :ClaudeCode --resume (-e %s, -e '%s'): same argv as base — same
Extra: --working-directory='%s' / ="%s", a folder name that is not valid UTF-8 (\xff) quote-breakout attacks ran cwd one arg, 0 ran
tests/unit/terminal/external_spec.lua 20/20 43/43. The head spec run against base code fails 16

These are the folder names I used: plain names, a space, a tab, ', ", backslashes with a trailing one, $(touch PWNED), backticks, ;, a component that starts with -e sh -c '…', literal %s %d, the byte \1, \1cwd\1, \1cwd\1 -e sh -c '…' x, a newline, the PR's attack folder demo -e sh -c 'touch PWNED' x, and quote-breakout names made to escape '%s' and "%s". The templates were X --working-directory %s -e %s, X --working-directory=%s -e %s, X --working-directory '%s' -e %s, X --working-directory "%s" -e %s, X -e %s, G -e '%s', G --working-directory %s -e '%s', sh -c 'cd %s && exec X -e %s', X -w=%s -e %s, X %s -e %s and X --a=b=%s -e %s.

On base the attack reproduces. With the attack folder, alacritty --working-directory %s -e %s ran sh -c 'touch PWNED'. The quoted templates '%s' and "%s" could also be broken out of with a matching quote in the folder name, and sh -c 'cd %s …' ran $(…), backtick and ; payloads. Head ran none of these.

Notes:

  • The refusals change behavior on purpose. A two-%s template that puts the cwd anywhere other than a whole argument or a -opt=/--opt= value now refuses even for a plain folder name that works on main. This covers sh -c 'cd %s && …', --a=b=%s, and a KEY=%s argument with no dashes. The README states this, and the error text names the fix (use a function).
  • On base, ordinary folder names with a space, a quote or a backslash also split or garbled the cwd argument, and some kept Claude from starting. Head fixes those too, so the changed argv for those names is a fix, not a regression.
  • This host has only Neovim 0.12.3. I did not run 0.8, 0.9 or 0.10. The change is plain Lua string handling with no new Neovim API, so I expect the same result there, but I did not verify it.
  • From the diff: if the user's own terminal_cmd or :ClaudeCode args contained the byte sequence \1cwd\1, the template would count two cwd markers and refuse. This is harmless and not reachable from a folder name, because the cwd goes in after the split and the loop never scans a replaced argument again.

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

Only the file hash of lua/claudecode/terminal/external.lua changed. It
holds no covered block or call site and cannot reach diff state, an
openDiff answer, a disconnect or a diff command.

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

Signed-off-by: Thomas Kosiewski <[email protected]>
@ThomasK33
ThomasK33 merged commit 8bcc7bb into main Nov 23, 2026
11 checks passed
@ThomasK33
ThomasK33 deleted the fix/external-cwd-one-argument branch November 23, 2026 17:48
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