Skip to content

fix(cwd): stop passing buffer- and git-derived paths to vim.fn.expand - #610

Merged
ThomasK33 merged 2 commits into
mainfrom
claude/claude-security-fix-find_01gqxbxfanwgsi8fqbalxw4k-76to65
Dec 3, 2026
Merged

ThomasK33 merged 2 commits into
mainfrom
claude/claude-security-fix-find_01gqxbxfanwgsi8fqbalxw4k-76to65

Conversation

@ThomasK33

Copy link
Copy Markdown
Member

Summary

This PR fixes a security vulnerability where paths containing backticks could be passed to vim.fn.expand(), which interprets backticks as shell command delimiters. This could occur when paths come from untrusted sources like terminal buffer names or git output.

Key Changes

  • New cwd.expand_leading() function: Safely expands only leading ~, ~/, $VAR, or ${VAR} patterns without calling vim.fn.expand(). This prevents backticks and other shell metacharacters from being interpreted as commands.

  • Updated cwd.normalize_dir(): Now uses expand_leading() instead of vim.fn.expand(), ensuring paths with special characters are handled safely.

  • Updated terminal.build_config(): Replaced vim.fn.expand() call with expand_leading() when resolving the working directory from cwd or cwd_provider results.

  • Comprehensive test coverage: Added unit tests in command_args_spec.lua and cwd_spec.lua to verify:

    • Terminal buffer names with backticks are never passed to vim.fn.expand()
    • Git output containing backticks is safely handled
    • expand_leading() correctly handles all expansion patterns while leaving the rest of the path untouched
  • Documentation updates: Updated README and integration test comments to clarify that only leading expansions are performed and paths are never passed to vim.fn.expand().

Implementation Details

The expand_leading() function:

  • Matches patterns at the start of the path only (^%$, ^%${, ^~)
  • Retrieves values from environment variables or vim.loop.os_homedir() for HOME
  • Returns the original path unchanged if no leading pattern is found or the variable is unset
  • Never calls vim.fn.expand(), eliminating the shell injection vector

This approach maintains backward compatibility with existing configurations while securing paths that come from dynamic sources like buffer names or git commands.

`normalize_dir` in cwd.lua and `build_config` in terminal.lua passed any
cwd text that was not an existing directory to `vim.fn.expand`. On Unix,
expand runs text between backticks through 'shell'. With git_repo_cwd or a
cwd_provider that returns ctx.file_dir, the directory part of a terminal,
archive-member or URI buffer name (and the root git prints) reached that
call, so a folder name holding a backtick pair ran a command when the user
opened or toggled Claude.

Add `cwd.expand_leading`, which expands only a leading `~`, `~/`, `$VAR`
or `${VAR}` from the environment, without a shell, and use it in both
places. The rest of the text is taken as written; normalize_dir still
returns nil for text that names no directory, and build_config still
passes a non-directory cwd through literally. `$VAR` later in the text,
`~user` and globs are no longer expanded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015koAoMEFEUXUgoDF7Nam12
@ThomasK33
ThomasK33 enabled auto-merge December 3, 2026 09:33
@ThomasK33 ThomasK33 changed the title Prevent shell command injection via backticks in path expansion fix(cwd): stop passing buffer- and git-derived paths to vim.fn.expand Dec 3, 2026
Only the whole-file hashes of lua/claudecode/terminal.lua and
lua/claudecode/cwd.lua changed. The change is confined to cwd resolution
in build_config and cwd.lua, which no model step covers; the terminal
lifecycle and diff lifecycle models pass against the changed code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015koAoMEFEUXUgoDF7Nam12

Copy link
Copy Markdown
Member Author

Re-recorded covered.lock for verify-terminal-lifecycle and verify-diff-lifecycle (258188a). Only the whole-file file hashes of lua/claudecode/terminal.lua and lua/claudecode/cwd.lua changed: no covered block or tracked site moved. The change is confined to cwd resolution (build_config, cwd.normalize_dir/expand_leading) and cannot reach terminal start, show, hide, close, exit handling, adoption, provider choice, or the diff lifecycle. Both models pass against the changed code before recording (terminal lifecycle: 279,492 states; diff lifecycle: main 255,881 states plus both liveness runs).

Also retitled the PR to a Conventional Commits title so conventional-title passes.


Generated by Claude Code

@ThomasK33
ThomasK33 added this pull request to the merge queue Dec 3, 2026
Merged via the queue into main with commit b9a7edf Dec 3, 2026
11 checks passed
@ThomasK33
ThomasK33 deleted the claude/claude-security-fix-find_01gqxbxfanwgsi8fqbalxw4k-76to65 branch December 3, 2026 10:15
@github-actions github-actions Bot mentioned this pull request Dec 3, 2026
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.

3 participants