Skip to content

Commit d8f4481

Browse files
peffgitster
authored andcommitted
refs: reject ref updates while GIT_QUARANTINE_PATH is set
As documented in git-receive-pack(1), updating a ref from within the pre-receive hook is dangerous and can corrupt your repo. This patch forbids ref updates entirely during the hook to make it harder for adventurous hook writers to shoot themselves in the foot. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eaeed07 commit d8f4481

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Documentation/git-receive-pack.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ This has a few user-visible effects and caveats:
239239
3. The `pre-receive` hook MUST NOT update any refs to point to
240240
quarantined objects. Other programs accessing the repository will
241241
not be able to see the objects (and if the pre-receive hook fails,
242-
those refs would become corrupted).
242+
those refs would become corrupted). For safety, any ref updates
243+
from within `pre-receive` are automatically rejected.
243244

244245

245246
SEE ALSO

refs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,
14651465
{
14661466
struct ref_store *refs = get_ref_store(NULL);
14671467

1468+
if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
1469+
strbuf_addstr(err,
1470+
_("ref updates forbidden inside quarantine environment"));
1471+
return -1;
1472+
}
1473+
14681474
return refs->be->transaction_commit(refs, transaction, err);
14691475
}
14701476

t/t5547-push-quarantine.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,15 @@ test_expect_success 'rejected objects are removed' '
3333
test_cmp expect actual
3434
'
3535

36+
test_expect_success 'updating a ref from quarantine is forbidden' '
37+
git init --bare update.git &&
38+
write_script update.git/hooks/pre-receive <<-\EOF &&
39+
read old new refname
40+
git update-ref refs/heads/unrelated $new
41+
exit 1
42+
EOF
43+
test_must_fail git push update.git HEAD &&
44+
git -C update.git fsck
45+
'
46+
3647
test_done

0 commit comments

Comments
 (0)