mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix(scripts/githooks): prevent agents from bypassing git hooks (#22825)
Agents hit short shell timeouts on `git commit` (~13s) before `make pre-commit` finishes (~20s warm), then disable hooks via `git config core.hooksPath /dev/null`. This bypasses all local checks and, because it writes to shared `.git/config`, silently disables hooks for every other worktree too. Add explicit timing guidance to AGENTS.md, and write worktree-scoped `core.hooksPath` in post-checkout, pre-commit, and pre-push hooks to make the bypass ineffective.
This commit is contained in:
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
# Shield this worktree against shared config hooksPath poisoning.
|
||||
# Worktree-scoped config overrides the shared .git/config, so even if
|
||||
# another worktree runs `git config core.hooksPath /dev/null`, this
|
||||
# worktree continues to use the correct hooks.
|
||||
#
|
||||
# This hook runs on `git worktree add` and `git checkout`/`git switch`.
|
||||
# Only needed in linked worktrees where shared config can be poisoned
|
||||
# by another worktree. Skipped in the main checkout to avoid errors
|
||||
# when extensions.worktreeConfig is not set (e.g. fresh clones).
|
||||
if [[ "$(git rev-parse --git-dir)" != "$(git rev-parse --git-common-dir)" ]]; then
|
||||
git config --worktree core.hooksPath scripts/githooks
|
||||
fi
|
||||
@@ -16,4 +16,8 @@ set -euo pipefail
|
||||
cd "$(git rev-parse --show-toplevel)"
|
||||
unset GIT_DIR
|
||||
|
||||
# In linked worktrees, set worktree-scoped hooksPath to override shared config.
|
||||
if [[ "$(git rev-parse --git-dir)" != "$(git rev-parse --git-common-dir)" ]]; then
|
||||
git config --worktree core.hooksPath scripts/githooks
|
||||
fi
|
||||
exec make pre-commit
|
||||
|
||||
@@ -19,4 +19,8 @@ set -euo pipefail
|
||||
cd "$(git rev-parse --show-toplevel)"
|
||||
unset GIT_DIR
|
||||
|
||||
# In linked worktrees, set worktree-scoped hooksPath to override shared config.
|
||||
if [[ "$(git rev-parse --git-dir)" != "$(git rev-parse --git-common-dir)" ]]; then
|
||||
git config --worktree core.hooksPath scripts/githooks
|
||||
fi
|
||||
exec make pre-push
|
||||
|
||||
Reference in New Issue
Block a user