diff mbox series

[7/8] refs: check refnames as fully qualified when writing

Message ID 20240429083401.GF233423@coredump.intra.peff.net (mailing list archive)
State New, archived
Headers show
Series tightening ref handling outside of refs/ | expand

Commit Message

Jeff King April 29, 2024, 8:34 a.m. UTC
When a ref update is queued via ref_transaction_update(), we call
check_refname_format() to make sure the name is acceptable. We pass
REFNAME_ALLOW_ONELEVEL, which allows pseudorefs like MERGE_HEAD. But
that's not enough to forbid names outside of refs/ like "foo/bar" or
even scary stuff like "config" (though fortunately I think that should
never work because we cannot resolve "config" to read the old value).

Let's instead pass REFNAME_FULLY_QUALIFIED, which tells the checking
function that we really do have a full refname, and it can enforce
it as such.

This means that "git update-ref foo/bar HEAD" will now be rejected. Note
that _deleting_ such a ref is already forbidden (and there is a test in
t1430 for that already), due to some confusing differences between
check_refname_format() and refname_is_safe(). See the previous commit
for more details.

Signed-off-by: Jeff King <peff@peff.net>
---
 refs.c                  |  2 +-
 t/t1430-bad-ref-name.sh | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/refs.c b/refs.c
index 44b4419050..57663cfe9e 100644
--- a/refs.c
+++ b/refs.c
@@ -1267,7 +1267,7 @@  int ref_transaction_update(struct ref_transaction *transaction,
 
 	if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
 	    ((new_oid && !is_null_oid(new_oid)) ?
-		     check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
+		     check_refname_format(refname, REFNAME_FULLY_QUALIFIED) :
 			   !refname_is_safe(refname))) {
 		strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
 			    refname);
diff --git a/t/t1430-bad-ref-name.sh b/t/t1430-bad-ref-name.sh
index 0c00118c2b..120e1557d7 100755
--- a/t/t1430-bad-ref-name.sh
+++ b/t/t1430-bad-ref-name.sh
@@ -390,4 +390,14 @@  test_expect_success 'branch -m can rename refs/heads/-dash' '
 	git show-ref refs/heads/dash
 '
 
+test_expect_success 'update-ref refuses lowercase outside of refs/' '
+	test_must_fail git update-ref lowercase HEAD 2>err &&
+	test_grep "refusing to update ref with bad name" err
+'
+
+test_expect_success 'update-ref refuses non-underscore outside of refs/' '
+	test_must_fail git update-ref FOO/HEAD HEAD 2>err &&
+	test_grep "refusing to update ref with bad name" err
+'
+
 test_done