Skip to content

fix(security): disable git fsmonitor hook execution in project context gathering - #962

Closed
mittalpk wants to merge 1 commit into
mistralai:mainfrom
mittalpk:fix/git-fsmonitor-rce
Closed

mittalpk wants to merge 1 commit into
mistralai:mainfrom
mittalpk:fix/git-fsmonitor-rce

Conversation

@mittalpk

Copy link
Copy Markdown
Contributor

Fixes #942.

Summary

ProjectContextProvider._run_git() runs git status/branch/log via subprocess.run(["git", "--no-optional-locks", *args], ...) on every session start, before any trust dialog is shown to the user (and unconditionally in -p programmatic mode too, per the original report).

git's core.fsmonitor config can name an arbitrary command that git executes on commands like status to refresh the index — and that config is repo-local, so a malicious repo the user merely opens or clones can plant

[core]
    fsmonitor = <payload>

in its own .git/config and have <payload> run with the user's full privileges as soon as get_git_status() is called. Reported publicly with a full PoC and CVSS 3.1 base score 8.8 (High) in #942.

Fix

Pass -c core.fsmonitor= on the git invocation itself:

["git", "-c", "core.fsmonitor=", "--no-optional-locks", *args]

Command-line -c overrides take precedence over a repo's own .git/config, so this can't be bypassed by whatever the inspected repo declares. Since all four call sites (branch --show-current, branch -r, status --porcelain, log) share _run_git, one change covers all of them.

Verification

I reproduced the reported PoC locally before and after the fix, not just read the code and assumed:

$ git init POC_REPO && cd POC_REPO && git commit --allow-empty -m init
$ echo 'fsmonitor = "touch /tmp/PWNED"' >> .git/config

# pre-fix invocation:
$ git --no-optional-locks status --porcelain
$ ls /tmp/PWNED
-rw-r--r-- ... /tmp/PWNED          # payload executed

# post-fix invocation:
$ git -c core.fsmonitor= --no-optional-locks status --porcelain
$ ls /tmp/PWNED
ls: cannot access '/tmp/PWNED': No such file or directory   # neutralized, status still correct

Tests

Added two tests to tests/core/test_system_prompt.py:

  • test_run_git_disables_fsmonitor_hook — asserts -c core.fsmonitor= is present in the argv actually passed to git, positioned ahead of the subcommand.
  • test_fetch_git_status_does_not_execute_malicious_fsmonitor_hook — full end-to-end reproduction of the bug: [Security] Remote Code Execution via git configuration #942 PoC against a real git repo; asserts the payload marker is never created and get_git_status() still returns a normal, non-error status string.

Both confirmed to fail against the pre-fix code (git stash isolating just system_prompt.py) — the malicious-hook test's failure is the payload marker file actually being created by the test harness itself, i.e. a live reproduction of the RCE, not just an assertion mismatch.

uv run pytest tests/ -q
# 8084 passed, 17 skipped, 5 failed -- same 5 pre-existing failures
# reproduced against a clean upstream/main checkout (TUI widget mounting,
# file-fingerprint mtime-resolution timing, two unrelated e2e timeouts,
# latin1 grep-encoding matching); none touch system_prompt.py or this
# call path.

uv run ruff check / ruff format --check
# clean

…t gathering

ProjectContextProvider._run_git() ran `git status`/`branch`/`log` via
`subprocess.run(["git", "--no-optional-locks", *args], ...)` on every
session start, before any trust dialog is shown to the user (and
unconditionally in `-p` programmatic mode too). git's core.fsmonitor
config can name an arbitrary command that git executes on commands like
`status` to refresh the index -- and that config is repo-local, so a
malicious repo the user merely opens or clones can plant

    [core]
        fsmonitor = <payload>

in its own .git/config and have <payload> run with the user's full
privileges as soon as get_git_status() is called. Reported publicly with
a full PoC and CVSS 3.1 base score 8.8 (High) in mistralai#942.

Fix: pass `-c core.fsmonitor=` on the git invocation itself. Command-line
-c overrides take precedence over a repo's own .git/config, so this can't
be bypassed by whatever the inspected repo declares, and it requires no
change to the four call sites that share _run_git (branch --show-current,
branch -r, status --porcelain, log).

Verified against a live reproduction of the reported PoC (a real git repo
with a malicious core.fsmonitor entry that touches a marker file): without
the fix, `git status --porcelain` executes the payload; with `-c
core.fsmonitor=` added, it does not, and status output is unaffected.

Added two tests to tests/core/test_system_prompt.py:
- test_run_git_disables_fsmonitor_hook: asserts the flag is present in the
  argv actually passed to git, ahead of the subcommand.
- test_fetch_git_status_does_not_execute_malicious_fsmonitor_hook: full
  end-to-end reproduction of the mistralai#942 PoC against a real git repo; asserts
  the payload marker is never created and get_git_status() still returns
  a normal, non-error status string.

Both confirmed to fail against the pre-fix code (git stash isolating just
system_prompt.py) -- the malicious-hook test's failure is the payload
marker file actually being created by the test harness itself, i.e. a
live reproduction of the RCE, not just an assertion mismatch.

Full suite: 8084 passed, 17 skipped, 5 failed -- same 5 pre-existing
failures (TUI widget mounting, file-fingerprint mtime-resolution timing,
two unrelated e2e timeouts, latin1 grep-encoding matching) reproduced
against a clean upstream/main checkout; none touch system_prompt.py or
this call path. ruff check / ruff format clean.
@mittalpk
mittalpk requested a review from a team as a code owner July 29, 2026 07:52
@mgesbert mgesbert closed this Jul 31, 2026
@mgesbert

Copy link
Copy Markdown
Contributor

Hi @mittalpk , thanks for your contribution! It's been integrated in our internal repo, you'll be credited as co-author in the next release. Sorry for the abrupt closing, it looks like some automation closed your PR when we merged our PR internally 😛

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.

bug: [Security] Remote Code Execution via git configuration

2 participants