From: Stefan Sperling Subject: Re: gotd: branch namespace protection and forced push To: Cas Hariri Cc: gameoftrees@openbsd.org Date: Wed, 28 Oct 2026 10:17:56 +0100 On Mon, Oct 26, 2026 at 12:57:51PM +0100, Cas Hariri wrote: > > Hi, > > I just installed OpenBSD, beautiful system, love it. > > I'm setting up gotd/gotsysd 0.129 so my git repositories behave like a > ledger: a push may add commits, but nobody, including someone with a > stolen key, can rewrite or delete history. > > Setup > > Every repository in gotsys.conf had: > >     protect { >         branch namespace "refs/heads/" >         tag namespace "refs/tags/" >     } > > What I tested > > In a throwaway repository, from a git client: > >   git push origin HEAD:refs/heads/ledger-test           # accepted >   git push --force origin HEAD~1:refs/heads/ledger-test # accepted >   git push origin --delete ledger-test                  # refused: >     refs/heads/: reference namespace is protected > > The deletion was refused, but the forced push rewound the branch. > gotsys.conf(5) says that for a branch namespace, "attempts to change the > history of branches or delete them will be denied", so I expected the > second push to be refused as well. > > How I worked around it > > I added the branches by name to each protect block: > >         branch "master" >         branch "main" >         branch "ledger-test" > > With that, the same test gives: > >   git push origin HEAD:refs/heads/ledger-test           # accepted >   git push --force origin HEAD~1:refs/heads/ledger-test # refused: >     refs/heads/ledger-test: reference is protected > > Question > > Is this expected behaviour, or a bug? It is indeed a bug! > In case it helps: the rewind sends an empty pack, so I think it goes > through protect_refs_from_moving() in gotd/repo_write.c. There the > namespace check is > >     if (strcmp(pe->path, refname) != 0) >         continue; > > which never matches "refs/heads/" against "refs/heads/ledger-test". > The other path, protect_branch_namespace(), uses a prefix match. If it > is a bug, maybe this: > > --- gotd/repo_write.c > +++ gotd/repo_write.c > @@ -1565,7 +1565,7 @@ > >          RB_FOREACH(pe, got_pathlist_head, >              &repo_write.protected_branch_namespaces) { > -            if (strcmp(pe->path, refname) != 0) > +            if (strncmp(pe->path, refname, strlen(pe->path)) != 0) >                  continue; > >              err = protect_require_yca(&ref_update->new_id, 0, > > Thanks, > Cas Thanks Cas, this is precisely where the problem was. I've made the code call protect_ref_namespace() in this case, which provides an informative error message to the client. The fix is below, along with a new test case which triggers the issue. Does this fix work for you? prevent branches in protected branch namespaces from being rewound Found by Cas Hariri M gotd/repo_write.c | 3+ 2- M regress/gotd/Makefile | 1+ 0- M regress/gotd/repo_write_protected.sh | 87+ 0- 3 files changed, 91 insertions(+), 2 deletions(-) commit - 6c1c3b032c05ecdacbe8627c13135075cd8020aa677cc2f79730596952928608 commit + f44c1876147dec9261c41068d2e9628d35129f00c67ab4be62c1279af446e926 blob - 44e185c805c00bc483846dacb510f32f6ee90967b970e8f824c081d763c53bca blob + d947ec4d2454e9ad65cc2f69da19ec9baf3b3e0ec57548058fe30feff0d9ee2e --- gotd/repo_write.c +++ gotd/repo_write.c @@ -1565,8 +1565,9 @@ protect_refs_from_moving(void) RB_FOREACH(pe, got_pathlist_head, &repo_write.protected_branch_namespaces) { - if (strcmp(pe->path, refname) != 0) - continue; + 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); blob - 1dff288b47b2ab0566b3f581f21411fb8f64760e2820dadb66165f5ca9aad659 blob + 746d51df23e4b646a611c3b3301e17c9da972f57816ef21cec2a112b1c8bd923 --- regress/gotd/Makefile +++ regress/gotd/Makefile @@ -176,6 +176,7 @@ start_gotd_rw_protected: ensure_root @echo ' permit rw $(GOTD_DEVUSER)' >> $(PWD)/gotd.conf @echo ' protect branch foo' >> $(PWD)/gotd.conf @echo ' protect tag namespace "refs/tags/"' >> $(PWD)/gotd.conf + @echo ' protect branch namespace "refs/heads/protected/"' >> $(PWD)/gotd.conf @echo ' protect { branch refs/heads/main }' >> $(PWD)/gotd.conf @echo "}" >> $(PWD)/gotd.conf @$(GOTD_TRAP); $(GOTD_START_CMD) blob - 08e82bd3923554c4117072c3b03ea890b8a087dcc4dfa7bf28f11a21d6daa8ec blob + 9192271b98ef137925eca9e7404d9b277ef206647d7093e3a969fd6818666e38 --- regress/gotd/repo_write_protected.sh +++ regress/gotd/repo_write_protected.sh @@ -354,8 +354,95 @@ test_modify_protected_branch() { test_done "$testroot" $ret } +test_modify_protected_branch_namespace() { + local testroot=`test_init modify_protected_branch_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` + + got branch -r $testroot/repo-clone -c $commit_id protected/foo + + got checkout -b protected/foo $testroot/repo-clone $testroot/wt >/dev/null + echo change file alpha in one way > $testroot/wt/alpha + (cd $testroot/wt && got commit -m 'change alpha on a new branch' > /dev/null) + + # Creating a new branch should succeed. + got send -q -r $testroot/repo-clone -b protected/foo 2> $testroot/stderr + ret=$? + if [ $ret -ne 0 ]; then + echo "got send failed unexpectedly" >&2 + test_done "$testroot" 1 + return 1 + fi + + # Forcefully rewinding an existing branch should fail. + (cd $testroot/wt && got update -c:head:-1 > /dev/null) + (cd $testroot/wt && got histedit -d > /dev/null) + got send -q -r $testroot/repo-clone -b protected/foo -f \ + 2> $testroot/stderr + ret=$? + if [ $ret -eq 0 ]; then + echo "got send succeeded unexpectedly" >&2 + test_done "$testroot" 1 + return 1 + fi + + grep ^gotsh: $testroot/stderr > $testroot/stderr.filtered + cat > $testroot/stderr.expected < /dev/null + got branch -r $testroot/repo-clone -c $commit_id protected/foo + + got checkout -b protected/foo $testroot/repo-clone $testroot/wt2 \ + > /dev/null + echo change file alpha in another way > $testroot/wt2/alpha + (cd $testroot/wt2 && got commit -m 'change alpha on another branch' \ + > /dev/null) + + got send -q -f -r $testroot/repo-clone -b protected/foo \ + 2> $testroot/stderr + ret=$? + if [ $ret -eq 0 ]; then + echo "got send succeeded unexpectedly" >&2 + test_done "$testroot" 1 + return 1 + fi + + grep ^gotsh: $testroot/stderr > $testroot/stderr.filtered + cat > $testroot/stderr.expected <