nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] git_diff passes "base" to git unchecked, so --output=<file> lets a read-only tool write files

Open addyCooks opened this issue 2 weeks ago • 1 comments

Description

git_diff appends the model-supplied base value to the git command line. A value like --output=<path> makes git write the diff to that path. git_diff is marked readOnly and has no approval rule, so it runs without asking. git_log already blocks this (source/tools/git/git-log.tsx:37); git_diff does not.

Environment

  • OS: Windows 11 Home 10.0.26300
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at f92cce9c)
  • Provider: Not provider-specific
  • Model: Not model-specific

Steps to Reproduce

  1. Create a git repo with one modified file.
  2. From that folder run the built tool (from dist/tools/git/git-diff.js): await gitDiffTool.tool.execute({base: '--output=<absolute path outside the repo>/outside.txt'}, {toolCallId: 'x', messages: []})
  3. Look for the file.

Expected Behavior

The call is rejected, for example Error: Invalid base: --output=.... No file is created.

Actual Behavior

The tool answers "No differences with --output=..." and the file outside the repo exists and contains the full patch. A prompt injection (or a model mistake) can overwrite any file the user can write, with no approval prompt.

Logs/Screenshots

No differences with --output=C:/Users/.../tmp.zYPgDofmuS/outside.txt
outside.txt now exists:
diff --git a/f.txt b/f.txt
index 5626abf..f719efd 100644

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder (main)

Suggested fix:

  • source/tools/git/git-diff.tsx:43 (if (args.base) gitArgs.push(args.base)): reject values that start with -, like git-log.tsx:37 does. Better, put --end-of-options before the revision, or validate it as a ref name.
  • Review every other model-controlled value in the git_* tools the same way.
  • source/tools/approval-policy.ts:82 treats readOnly tools as safe to run unattended, which is why this skips approval.
  • Add tests with base set to --output=... and --ext-diff.

Related: #1933 / PR #1945 (git_add without --).

addyCooks avatar Nov 14 '26 06:11 addyCooks

I'd like to take this one and will fix it following the suggested approach .

Ember452 avatar Nov 14 '26 08:11 Ember452