"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Anton Kasimov <kasimov.an@gmail.com>
Subject:
gotd: fix reference protection for creations sent with empty pack files
To:
gameoftrees@openbsd.org
Date:
Wed, 2 Dec 2026 19:15:50 +0300

Download raw body.

Thread
Hello,

I was investigating why pushing tags created with jujutsu to gotd
failed with:

   fatal: remote error: refs/tags/: reference namespace is protected

whenever "protect tag namespace refs/tags/" is configured. I searched
for the cause together with an AI agent (Crush with Z.AI GLM), and
asked it to prepare tests once the problems were confirmed; I reviewed
the resulting code and diffs as much as I could.

The initial failure turned out to be by design: jujutsu can only
create lightweight tags 
(https://jj-vcs.github.io/jj/latest/git-compatibility.html
says "you cannot create annotated tags"), while tags in a protected
tag namespace must point at tag objects. So tools like jj cannot
create tags inside a protected tag namespace, even though gotd.conf(5)
documents that new tags may be created there.

However, while reproducing this the agent found several related bugs
in gotd's handling of reference updates which arrive with an empty
pack file (protect_refs_from_moving() in gotd/repo_write.c). Clients
such as git push send an empty pack file when creating new references
whose objects already exist on the server, and this code path does not
distinguish creations of new references from moves of existing ones:

1. Creating a new branch inside a protected branch namespace is
    rejected with "reference namespace is protected" when the client
    sends an empty pack file, even though gotd.conf(5) documents that
    new branches may be created in the namespace. This regression came
    in with the fix for Cas Hariri's forced-push report:
    https://marc.gameoftrees.org/mail/1793020501.68587_0.html

2. Moves of existing references which are not covered by any protected
    branch namespace still run a YCA check, so rewinding an unprotected
    branch with an empty pack file fails with "reference is protected"
    whenever any protected branch namespace is configured.

3. Creating a new tag in a protected tag namespace is rejected with
    "reference namespace is protected" when the tag object already
    exists on the server, because protect_refs_from_moving() rejects
    everything under the namespace regardless of whether the reference
    exists. This has been broken since empty-pack protection was added
    (commit 876c0ca, January 2025; the same function was already
    involved in Martijn van Duren's "can't append to protected branch"
    report: https://marc.gameoftrees.org/mail/1749816301.52528_0.html).

4. Creating a lightweight tag in a protected tag namespace is rejected
    with the bogus "reference namespace is protected" error instead of
    an error saying that the new tag does not point at a tag object.
    This is what initially sent my investigation in the wrong
    direction.

The first patch below adds four tests to 
regress/gotd/repo_write_protected.sh
which reproduce these bugs. They expect the documented behaviour and
use the xfail mechanism, so they will start passing once the fix is
applied.

The second patch fixes protect_refs_from_moving() by applying the same
checks which verify_packfile() applies to clients that send a pack
file: creations of new tags in protected tag namespaces must point at
a tag object, and creations of new branches in protected branch
namespaces and protected branches must point at a commit.
verify_object_type() now verifies the object type in the repository
when no pack file was sent. The patch also removes a YCA check which
ran for existing references not covered by any protected branch
namespace, and restores checks against all protected branch namespaces
rather than only the first one (the check was escaping everything
after the first list entry).

With these patches applied, all tests in regress/gotd pass on
OpenBSD/amd64, including the four new ones.

One policy question I would like to raise: lightweight tags are
rejected inside protected tag namespaces by design, and this patch
series keeps it that way but reports the real reason instead of a
bogus namespace error. Given that tools such as jujutsu cannot create
annotated tags at all, users of such tools cannot create any tags in
a protected namespace. Would you consider relaxing this requirement
(e.g. also accepting tags which point at commits)?

--
Anton Kasimov

From: Anton Kasimov <kasimov.an@gmail.com>
Subject: [patch got 1/2] add tests for gotd reference protection with 
empty pack files

Four new tests in regress/gotd/repo_write_protected.sh reproduce
bugs in gotd reference protection checks which are triggered when
clients send an empty pack file:

- creating a new branch in a protected branch namespace was
   rejected with "reference namespace is protected"
- moving an existing unprotected branch triggered a YCA check
   whenever any protected branch namespace was configured
- creating a new tag in a protected tag namespace was rejected
   even when the new tag points at a tag object which already
   exists on the server
- creating a lightweight tag in a protected tag namespace was
   rejected with a bogus "reference namespace is protected" error
   instead of an error which says that the tag does not point at
   a tag object

The tests expect the documented behaviour where new tags and
branches may be created inside protected namespaces, and use the
xfail mechanism until the fix lands.

diff 116623f9a978ac5705813760d1ecc9ec7fe72c1797fb835e2b9e5b47e29e0abf 
0b76d7794b515a099125d5316526caa6ecac0a633d1ebf185ae7e255e27b4dad
commit - 116623f9a978ac5705813760d1ecc9ec7fe72c1797fb835e2b9e5b47e29e0abf
commit + 0b76d7794b515a099125d5316526caa6ecac0a633d1ebf185ae7e255e27b4dad
blob - 9192271b98ef137925eca9e7404d9b277ef206647d7093e3a969fd6818666e38
blob + 1b1ed49a62f4cb72fb73b414d9e0999ae0322ca315f84ab86ee3bd7b37a86b58
--- regress/gotd/repo_write_protected.sh
+++ regress/gotd/repo_write_protected.sh
@@ -440,9 +440,270 @@ EOF
      test_done "$testroot" 0
  }

+test_create_branch_in_protected_namespace() {
+    local testroot=`test_init create_branch_in_protected_namespace`
+
+    got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got clone failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    local commit_id=`git_show_head $testroot/repo-clone`
+
+    # The commit already exists on the server, so git sends an empty
+    # pack file. Creating a new branch in a protected branch namespace
+    # should succeed.
+    (cd $testroot/repo-clone && git push -q origin \
+        refs/heads/main:refs/heads/protected/newbranch \
+        2> $testroot/stderr)
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        if egrep -q '(fatal: remote error|gotsh): 
refs/heads/protected/: reference namespace is protected' \
+            $testroot/stderr; then
+            test_done "$testroot" "xfail (gotd rejects creating a new 
branch in a protected branch namespace when an empty pack file is sent)"
+        else
+            echo -n "error message unexpected or missing: " >&2
+            cat $testroot/stderr >&2
+            test_done "$testroot" 1
+        fi
+        return 1
+    fi
+
+    # Verify that the branch was created on the server.
+    got clone -l ${GOTD_TEST_REPO_URL} | grep protected/newbranch \
+        > $testroot/stdout
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got clone -l failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    echo "refs/heads/protected/newbranch: $commit_id" \
+        > $testroot/stdout.expected
+    cmp -s $testroot/stdout.expected $testroot/stdout
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        diff -u $testroot/stdout.expected $testroot/stdout
+    fi
+
+    test_done "$testroot" $ret
+}
+
+test_rewind_unprotected_branch() {
+    local testroot=`test_init rewind_unprotected_branch`
+
+    got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got clone failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got checkout failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    # Create an unprotected branch and add two commits on it.
+    (cd $testroot/wt && got branch testbranch) >/dev/null
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got branch failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    for i in 1 2; do
+        echo "more alpha" >> $testroot/wt/alpha
+        (cd $testroot/wt && got commit -m "more") >/dev/null
+    done
+    local commit_id=`git_show_branch_head $testroot/repo-clone testbranch`
+    local parent_commit_id=`git_show_parent_commit $testroot/repo-clone \
+        "$commit_id"`
+
+    # Creating and sending the new branch should succeed.
+    got send -q -r $testroot/repo-clone -b testbranch 2> $testroot/stderr
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got send failed unexpectedly" >&2
+        cat $testroot/stderr >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    # Rewind the branch by one commit. The commit being restored
+    # already exists on the server, so git sends an empty pack file.
+    # Rewinding an unprotected branch should succeed.
+    (cd $testroot/repo-clone && git update-ref refs/heads/testbranch \
+        "$parent_commit_id")
+    (cd $testroot/repo-clone && git push -q -f origin testbranch \
+        2> $testroot/stderr)
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        if egrep -q '(fatal: remote error|gotsh): 
refs/heads/testbranch: reference is protected' \
+            $testroot/stderr; then
+            test_done "$testroot" "xfail (gotd applies branch 
protection checks to an unprotected branch when a protected branch 
namespace is configured)"
+        else
+            echo -n "error message unexpected or missing: " >&2
+            cat $testroot/stderr >&2
+            test_done "$testroot" 1
+        fi
+        return 1
+    fi
+
+    # Verify that the branch moved on the server.
+    got clone -l ${GOTD_TEST_REPO_URL} | grep refs/heads/testbranch \
+        > $testroot/stdout
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got clone -l failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    echo "refs/heads/testbranch: $parent_commit_id" > 
$testroot/stdout.expected
+    cmp -s $testroot/stdout.expected $testroot/stdout
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        diff -u $testroot/stdout.expected $testroot/stdout
+    fi
+
+    test_done "$testroot" $ret
+}
+
+test_create_tag_in_protected_namespace() {
+    local testroot=`test_init create_tag_in_protected_namespace`
+
+    got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got clone failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    # Create an annotated tag in the local clone. Its tag object does
+    # not exist on the server yet.
+    got tag -r $testroot/repo-clone -m "2.0" 2.0 >/dev/null
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got tag failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    local tag_id=`git -C $testroot/repo-clone rev-parse refs/tags/2.0`
+
+    # Upload the tag object to the server by pushing it as a second
+    # tag which points at the same tag object. Creating a new tag in
+    # a protected tag namespace is allowed, so this should succeed.
+    (cd $testroot/repo-clone && git push -q origin \
+        refs/tags/2.0:refs/tags/2.0-obj 2> $testroot/stderr)
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "git push failed unexpectedly" >&2
+        cat $testroot/stderr >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    # Now push the tag itself. The tag object already exists on the
+    # server, so git sends an empty pack file. Creating a new tag in a
+    # protected tag namespace should succeed.
+    (cd $testroot/repo-clone && git push -q origin refs/tags/2.0 \
+        2> $testroot/stderr)
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        if egrep -q '(fatal: remote error|gotsh): refs/tags/: reference 
namespace is protected' \
+            $testroot/stderr; then
+            test_done "$testroot" "xfail (gotd rejects creating a new 
tag in a protected tag namespace when an empty pack file is sent)"
+        else
+            echo -n "error message unexpected or missing: " >&2
+            cat $testroot/stderr >&2
+            test_done "$testroot" 1
+        fi
+        return 1
+    fi
+
+    # Verify that the tag was created on the server.
+    got clone -l ${GOTD_TEST_REPO_URL} | grep '^refs/tags/2.0:' \
+        > $testroot/stdout
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got clone -l failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    echo "refs/tags/2.0: $tag_id" > $testroot/stdout.expected
+    cmp -s $testroot/stdout.expected $testroot/stdout
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        diff -u $testroot/stdout.expected $testroot/stdout
+    fi
+
+    test_done "$testroot" $ret
+}
+
+test_create_lightweight_tag_in_protected_namespace() {
+    local testroot=`test_init 
create_lightweight_tag_in_protected_namespace`
+
+    got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
+    ret=$?
+    if [ $ret -ne 0 ]; then
+        echo "got clone failed unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    local commit_id=`git_show_head $testroot/repo-clone`
+
+    # A lightweight tag points at a commit instead of a tag object.
+    # Such tags may not be created in a protected tag namespace,
+    # however the error message should identify the actual problem
+    # rather than claim that the namespace itself is protected.
+    (cd $testroot/repo-clone && git tag 1.1 "$commit_id")
+    (cd $testroot/repo-clone && git push -q origin 1.1 \
+        2> $testroot/stderr)
+    ret=$?
+    if [ $ret -eq 0 ]; then
+        echo "git push succeeded unexpectedly" >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    if egrep -q '(fatal: remote error|gotsh): refs/tags/: reference 
namespace is protected' \
+        $testroot/stderr; then
+        test_done "$testroot" "xfail (gotd claims the namespace is 
protected instead of rejecting a lightweight tag with a proper error 
message)"
+        return 1
+    fi
+
+    if ! egrep -q '(fatal: remote error|gotsh): [0-9a-f]{40,} is not 
pointing at a tag object' \
+        $testroot/stderr; then
+        echo -n "error message unexpected or missing: " >&2
+        cat $testroot/stderr >&2
+        test_done "$testroot" 1
+        return 1
+    fi
+
+    test_done "$testroot" 0
+}
+
  test_parseargs "$@"
  run_test test_create_protected_branch
  run_test test_modify_protected_tag_namespace
  run_test test_delete_protected_branch
  run_test test_modify_protected_branch
  run_test test_modify_protected_branch_namespace
+run_test test_create_branch_in_protected_namespace
+run_test test_rewind_unprotected_branch
+run_test test_create_tag_in_protected_namespace
+run_test test_create_lightweight_tag_in_protected_namespace

From: Anton Kasimov <kasimov.an@gmail.com>
Subject: [patch got 2/2] fix gotd reference protection for creations 
sent with empty pack files

Clients such as git push create new tags and branches whose objects
already exist on the server by sending an empty pack file. The
protect_refs_from_moving() function could not tell creations of new
references apart from moves of existing references, and rejected
creations under protected tag and branch namespaces with a bogus
"reference namespace is protected" error.

Teach verify_object_type() to verify the object type in the repository
when no pack file was sent, and apply the same checks in
protect_refs_from_moving() which verify_packfile() applies to clients
which send a pack file: new tags in protected tag namespaces must
point at a tag object, and new branches in protected branch namespaces
and protected branches must point at a commit.

While here, remove a YCA check which protect_refs_from_moving() ran
for existing references not covered by any protected branch namespace,
and restore checks against all protected branch namespaces rather
than only the first one.

Found while investigating why git push of tags created by jujutsu
failed when "protect tag namespace refs/tags/" was configured.

diff 0b76d7794b515a099125d5316526caa6ecac0a633d1ebf185ae7e255e27b4dad 
ee9abb38dc54fab81d4b7990a364749365d4292524229d20e841fcad49c3791e
commit - 0b76d7794b515a099125d5316526caa6ecac0a633d1ebf185ae7e255e27b4dad
commit + ee9abb38dc54fab81d4b7990a364749365d4292524229d20e841fcad49c3791e
blob - 63cf646d37e3f95368b91173a6a9ac0a9e4517c7b01aa5e92692f5801f5e8f38
blob + 4ade3a5f87451a5b4b134ad535cbebc4b5f256909daf8813e0ff31f4e0d09752
--- gotd/repo_write.c
+++ gotd/repo_write.c
@@ -387,9 +387,23 @@ verify_object_type(struct got_object_id *id, int expec
      const struct got_error *err;
      char hex[GOT_HASH_DIGEST_STRING_MAXLEN];
      struct got_object *obj;
-    int idx;
+    int idx, obj_type;
      const char *typestr;

+    if (pack == NULL) {
+        err = got_object_get_type(&obj_type, repo_write.repo, id);
+        if (err)
+            return err;
+
+        if (obj_type == expected_obj_type)
+            return NULL;
+
+        got_object_id_hex(id, hex, sizeof(hex));
+        got_object_type_label(&typestr, expected_obj_type);
+        return got_error_fmt(GOT_ERR_OBJ_TYPE,
+            "%s is not pointing at a %s object", hex, typestr);
+    }
+
      idx = got_packidx_get_object_idx(packidx, id);
      if (idx == -1) {
          got_object_id_hex(id, hex, sizeof(hex));
@@ -1620,6 +1634,34 @@ protect_refs_from_moving(void)

          refname = got_ref_get_name(ref_update->ref);

+        if (ref_update->ref_is_new) {
+            RB_FOREACH(pe, got_pathlist_head,
+                &repo_write.protected_tag_namespaces) {
+                err = protect_tag_namespace(pe->path,
+                    NULL, NULL, ref_update);
+                if (err)
+                    return err;
+            }
+
+            RB_FOREACH(pe, got_pathlist_head,
+                &repo_write.protected_branch_namespaces) {
+                err = protect_branch_namespace(pe->path,
+                    NULL, NULL, ref_update);
+                if (err)
+                    return err;
+            }
+
+            RB_FOREACH(pe, got_pathlist_head,
+                &repo_write.protected_branches) {
+                err = protect_branch(pe->path,
+                    NULL, NULL, ref_update);
+                if (err)
+                    return err;
+            }
+
+            continue;
+        }
+
          RB_FOREACH(pe, got_pathlist_head,
              &repo_write.protected_tag_namespaces) {
              err = protect_ref_namespace(refname, pe->path);
@@ -1632,12 +1674,6 @@ protect_refs_from_moving(void)
              err = protect_ref_namespace(refname, pe->path);
              if (err)
                  return err;
-
-            err = protect_require_yca(&ref_update->new_id, 0,
-                NULL, NULL, ref_update->ref);
-            if (err)
-                return err;
-            break;
          }

          RB_FOREACH(pe, got_pathlist_head,