Skip to content

fix(index): let git check the repository before running a commit hook - #2306

Open
Keerthana-64 wants to merge 1 commit into
gitpython-developers:mainfrom
Keerthana-64:hook-safe-directory
Open

Keerthana-64 wants to merge 1 commit into
gitpython-developers:mainfrom
Keerthana-64:hook-safe-directory

Conversation

@Keerthana-64

Copy link
Copy Markdown
Contributor

Repro: in a repository git refuses as "dubious ownership" (simulated with GIT_TEST_ASSUME_DIFFERENT_OWNER=1), repo.index.commit("x") runs .git/hooks/pre-commit and only then fails in git hash-object, while git commit refuses without running any hook.
Cause: run_commit_hook finds and executes the hook without calling git, and IndexFile.commit runs pre-commit and commit-msg before its first git command, so git's safe.directory check (CVE-2022-24765) comes after code from the repository has already run.
Fix: run git rev-parse --git-dir before executing an existing hook so git applies its own ownership policy. Repositories git accepts behave as before, and the new test in test/test_index.py fails before the change and passes after.

`run_commit_hook` finds a hook in `.git/hooks` or `core.hooksPath`, read
through GitPython's own config parser, and executes it directly. No git
command runs before that, so git's check that the repository is safe to
use (the ownership and `safe.directory` check added for CVE-2022-24765)
has not happened when code from the repository is executed.

`IndexFile.commit` runs the `pre-commit` and `commit-msg` hooks before
it calls git at all. In a repository owned by another user, where
`git commit` stops with "detected dubious ownership" without running any
hook, `repo.index.commit()` ran the repository's `pre-commit` hook and
only then failed in `git hash-object`. That is the situation git's check
exists for: a `.git` directory planted by another user in a shared or
parent directory (`Repo(path, search_parent_directories=True)` finds it
the same way git would).

Before executing a hook that exists, `run_commit_hook` now runs
`git rev-parse --git-dir` in the repository, so git applies its own
policy, including `safe.directory` and the exemptions git makes. When
git refuses, the `GitCommandError` it raises is the one the commit
already ended with, only now before the hook runs. When git accepts the
repository, hooks run as before, and nothing changes when there is no
hook. Asking git, rather than reimplementing the ownership rules, keeps
the result identical to `git commit` for every git version, including
versions older than 2.35.2 that make no check.

Adds `test_pre_commit_hook_not_run_in_repository_git_refuses` to
`test/test_index.py`, which uses git's `GIT_TEST_ASSUME_DIFFERENT_OWNER`
to simulate another owner; it fails before this change and passes
after. `test_run_commit_hook` and
`test_run_commit_hook_outside_worktree_on_windows` mock `Git.execute`
(the latter because it fakes the Windows `PATH`), so they now allow this
one call while still rejecting any git command during hook lookup.

`ruff check`, `ruff format --check` and `mypy` pass. In a full clone
prepared with `init-tests-after-clone.sh`, the pytest suite passes
except `test_diff_with_staged_file`, `test_index_mutation`,
`test_blame_content_with_carriage_returns`, `test_empty_repo`,
`test_rebasing` and the two `test_actor_get_uid_laziness_*` tests,
which fail the same way without this change on this machine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant