Commit d189296142

d189296142d0e0b740b31e9f8d28608b635c8c41

parent: 96f3442ce1

Verified · cmc ci/build: success ci/quality: success ci/test: success

cmc <hello@cleberg.net> · 2026-11-01 07:25 UTC

Refuse ref names that git would read as options

- policy: a push may not create a branch or tag whose name starts
  with '-'; deleting one is still allowed.
- repo settings default-branch refuses such a name.
- gitutil: LastCommitDate, Parents, RevListAuthors, RevListMessages
  and IsAncestor end option parsing before their refs, as the rest of
  the package does.
- gitbay mr rebase refuses a merge request whose branches start with
  '-' instead of passing them to git rebase and git push.

Ref #341

Layout: unified · split

cmd/gitbay/local.go +6
@@ -376,6 +376,12 @@ func cmdMRRebase(args []string) int {
376376 fmt.Fprintf(os.Stderr, "gitbay: !%s is %s\n", n, state)
377377 return protocol.ExitUsage
378378 }
379 // The names come from the server and land on git's command line, where
380 // one starting with a dash would be read as an option.
381 if strings.HasPrefix(source, "-") || strings.HasPrefix(target, "-") {
382 fmt.Fprintf(os.Stderr, "gitbay: !%s names a branch starting with '-'; rebase it by hand\n", n)
383 return protocol.ExitUsage
384 }
379385 // A fork's branch lives in a repository this clone does not push to,
380386 // and guessing which remote that is would be worse than saying so.
381387 if strings.Contains(source, ":") {
internal/control/repo.go +3
@@ -925,6 +925,9 @@ func runSetDefaultBranch(c *Ctx, args []string) int {
925925 return code
926926 }
927927 branch := args[1]
928 if strings.HasPrefix(branch, "-") {
929 return c.fail(protocol.ExitUsage, "a default branch may not start with '-'")
930 }
928931 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
929932 if _, err := gitutil.ResolveRef(dir, "refs/heads/"+branch); err != nil {
930933 return c.fail(protocol.ExitFailure, "no branch named %q on %s", branch, repo.Path())
internal/control/security_test.go +15
@@ -295,3 +295,18 @@ func TestSecurityDismiss(t *testing.T) {
295295 t.Errorf("reopening a finding should open a fresh issue, got #%d", reopened.IssueNumber)
296296 }
297297}
298
299// A default branch is passed to git by name, so one shaped like an option
300// is refused before it is looked up.
301func TestDefaultBranchRefusesOptionShape(t *testing.T) {
302 st, repo, u, root := browseFixture(t)
303 c, out := runnerCtx(st, u.ID, root)
304 if code := runSetDefaultBranch(c, []string{repo.Path(), "--output=/tmp/x"}); code != protocol.ExitUsage ||
305 !strings.Contains(out.String(), "may not start with '-'") {
306 t.Errorf("exit %d: %s", code, out)
307 }
308 c, out = runnerCtx(st, u.ID, root)
309 if code := runSetDefaultBranch(c, []string{repo.Path(), "feature"}); code != protocol.ExitOK {
310 t.Errorf("a normal branch: exit %d: %s", code, out)
311 }
312}
internal/gitutil/endofoptions_test.go +25 −20
@@ -22,26 +22,31 @@ func TestRefsAreNotOptions(t *testing.T) {
2222
2323 var sink bytes.Buffer
2424 calls := map[string]func() error{
25 "CountCommits": func() error { CountCommits(dir, ref); return nil },
26 "Contributors": func() error { Contributors(dir, ref); return nil },
27 "Languages": func() error { Languages(dir, ref, func(string) string { return "x" }); return nil },
28 "RevList": func() error { _, err := RevList(dir, ref, 1); return err },
29 "RevListPath": func() error { _, err := RevListPath(dir, ref, "f.txt", 1); return err },
30 "PeelToCommit": func() error { _, err := PeelToCommit(dir, ref); return err },
31 "ReadCommit": func() error { _, err := ReadCommit(dir, ref); return err },
32 "ListTree": func() error { _, err := ListTree(dir, ref, ""); return err },
33 "ReadBlob": func() error { _, err := ReadBlob(dir, ref, "f.txt", 1<<20); return err },
34 "ResolveRef": func() error { _, err := ResolveRef(dir, ref); return err },
35 "Reachable": func() error { _, err := Reachable(dir, ref); return err },
36 "Archive": func() error { return Archive(dir, ref, "x", &sink) },
37 "Grep": func() error { _, err := Grep(dir, ref, "hi", 10); return err },
38 "MergeBase": func() error { _, err := MergeBase(dir, ref, "main"); return err },
39 "Diff": func() error { _, _, err := Diff(dir, ref, "main", 1<<20); return err },
40 "DiffFiles": func() error { _, err := DiffFiles(dir, ref, "main"); return err },
41 "RevListRange": func() error { _, err := RevListRange(dir, "main", ref); return err },
42 "Blame": func() error { _, err := Blame(dir, ref, "f.txt", 1, 1); return err },
43 "TipCommit": func() error { TipCommit(dir, ref); return nil },
44 "StatPath": func() error { StatPath(dir, ref, "f.txt"); return nil },
25 "CountCommits": func() error { CountCommits(dir, ref); return nil },
26 "Contributors": func() error { Contributors(dir, ref); return nil },
27 "Languages": func() error { Languages(dir, ref, func(string) string { return "x" }); return nil },
28 "RevList": func() error { _, err := RevList(dir, ref, 1); return err },
29 "RevListPath": func() error { _, err := RevListPath(dir, ref, "f.txt", 1); return err },
30 "PeelToCommit": func() error { _, err := PeelToCommit(dir, ref); return err },
31 "ReadCommit": func() error { _, err := ReadCommit(dir, ref); return err },
32 "ListTree": func() error { _, err := ListTree(dir, ref, ""); return err },
33 "ReadBlob": func() error { _, err := ReadBlob(dir, ref, "f.txt", 1<<20); return err },
34 "ResolveRef": func() error { _, err := ResolveRef(dir, ref); return err },
35 "Reachable": func() error { _, err := Reachable(dir, ref); return err },
36 "Archive": func() error { return Archive(dir, ref, "x", &sink) },
37 "Grep": func() error { _, err := Grep(dir, ref, "hi", 10); return err },
38 "MergeBase": func() error { _, err := MergeBase(dir, ref, "main"); return err },
39 "Diff": func() error { _, _, err := Diff(dir, ref, "main", 1<<20); return err },
40 "DiffFiles": func() error { _, err := DiffFiles(dir, ref, "main"); return err },
41 "RevListRange": func() error { _, err := RevListRange(dir, "main", ref); return err },
42 "Blame": func() error { _, err := Blame(dir, ref, "f.txt", 1, 1); return err },
43 "TipCommit": func() error { TipCommit(dir, ref); return nil },
44 "StatPath": func() error { StatPath(dir, ref, "f.txt"); return nil },
45 "LastCommitDate": func() error { LastCommitDate(dir, ref); return nil },
46 "Parents": func() error { Parents(dir, ref); return nil },
47 "RevListAuthors": func() error { _, err := RevListAuthors(dir, "", ref, 1); return err },
48 "RevListMessages": func() error { _, err := RevListMessages(dir, "", ref, 1); return err },
49 "IsAncestor": func() error { _, err := IsAncestor(dir, ref, "main"); return err },
4550 }
4651 for name, call := range calls {
4752 call()
internal/gitutil/gitutil.go +1 −1
@@ -93,7 +93,7 @@ func RunUntil(cmd *exec.Cmd, cancel <-chan struct{}) error {
9393// dir. It must run with the caller's environment intact so that quarantined
9494// objects during pre-receive remain visible.
9595func IsAncestor(dir, old, new string) (bool, error) {
96 cmd := exec.Command(toolpath.Look("git"), "-C", dir, "merge-base", "--is-ancestor", old, new)
96 cmd := exec.Command(toolpath.Look("git"), "-C", dir, "merge-base", "--is-ancestor", "--end-of-options", old, new)
9797 err := cmd.Run()
9898 if err == nil {
9999 return true, nil
internal/gitutil/messages.go +4 −4
@@ -20,7 +20,7 @@ type CommitAuthor struct {
2020// reachable from new but not old, capped at max. Empty or zero old lists
2121// from new alone.
2222func RevListAuthors(dir, old, new string, max int) ([]CommitAuthor, error) {
23 args := []string{"-C", dir, "log", fmt.Sprintf("--max-count=%d", max), "--format=%H%x00%ae%x00%as", new}
23 args := []string{"-C", dir, "log", fmt.Sprintf("--max-count=%d", max), "--format=%H%x00%ae%x00%as", "--end-of-options", new}
2424 if old != "" && old != zeroSHA {
2525 args = append(args, "^"+old)
2626 }
@@ -41,7 +41,7 @@ func RevListAuthors(dir, old, new string, max int) ([]CommitAuthor, error) {
4141
4242// Parents returns a commit's parent shas.
4343func Parents(dir, sha string) []string {
44 out, err := exec.Command(toolpath.Look("git"), "-C", dir, "log", "-1", "--format=%P", sha).Output()
44 out, err := exec.Command(toolpath.Look("git"), "-C", dir, "log", "-1", "--format=%P", "--end-of-options", sha).Output()
4545 if err != nil {
4646 return nil
4747 }
@@ -51,7 +51,7 @@ func Parents(dir, sha string) []string {
5151// LastCommitDate returns the committer date (YYYY-MM-DD) of the ref tip,
5252// or "" for empty repos.
5353func LastCommitDate(dir, ref string) string {
54 out, err := exec.Command(toolpath.Look("git"), "-C", dir, "log", "-1", "--format=%cs", ref).Output()
54 out, err := exec.Command(toolpath.Look("git"), "-C", dir, "log", "-1", "--format=%cs", "--end-of-options", ref).Output()
5555 if err != nil {
5656 return ""
5757 }
@@ -74,7 +74,7 @@ type CommitMsg struct {
7474// new but not old, newest first, capped at max. An empty or zero old (new
7575// branch) lists from new alone, still capped.
7676func RevListMessages(dir, old, new string, max int) ([]CommitMsg, error) {
77 args := []string{"-C", dir, "rev-list", fmt.Sprintf("-n%d", max), "--format=%an%x01%ae%x01%B%x00", new}
77 args := []string{"-C", dir, "rev-list", fmt.Sprintf("-n%d", max), "--format=%an%x01%ae%x01%B%x00", "--end-of-options", new}
7878 if old != "" && old != zeroSHA {
7979 args = append(args, "^"+old)
8080 }
internal/policy/access.go +16
@@ -99,6 +99,11 @@ func CheckPush(repo store.Repo, updates []RefUpdate) string {
9999 if strings.HasPrefix(u.Ref, "refs/merge-requests/") {
100100 return "refs/merge-requests/* is server-owned and cannot be pushed"
101101 }
102 // git reads a name starting with a dash as an option wherever one
103 // reaches a command line. Deleting one is still allowed.
104 if name := RefShortName(u.Ref); strings.HasPrefix(name, "-") && !u.IsDelete {
105 return "ref names may not start with '-': " + name
106 }
102107 // A protected tag is created once. Its globs match the tag name.
103108 if tag, ok := strings.CutPrefix(u.Ref, "refs/tags/"); ok && TagProtected(repo, tag) {
104109 if u.IsDelete {
@@ -139,3 +144,14 @@ func TagProtected(repo store.Repo, tag string) bool {
139144}
140145
141146func isZeroSHA(sha string) bool { return sha != "" && strings.Trim(sha, "0") == "" }
147
148// RefShortName is a branch or tag ref without its refs/heads/ or
149// refs/tags/ prefix, or the ref unchanged.
150func RefShortName(ref string) string {
151 for _, p := range []string{"refs/heads/", "refs/tags/"} {
152 if name, ok := strings.CutPrefix(ref, p); ok {
153 return name
154 }
155 }
156 return ref
157}
internal/policy/access_test.go +4
@@ -101,6 +101,10 @@ func TestCheckPush(t *testing.T) {
101101 {"delete unprotected", []RefUpdate{{Ref: "refs/heads/dev", IsDelete: true}}, false},
102102 {"mr namespace", []RefUpdate{{Ref: "refs/merge-requests/1/head"}}, true},
103103 {"tag alongside protected", []RefUpdate{{Ref: "refs/tags/v1"}, {Ref: "refs/heads/main"}}, false},
104 {"branch shaped like an option", []RefUpdate{{Ref: "refs/heads/--output=/x"}}, true},
105 {"tag shaped like an option", []RefUpdate{{Ref: "refs/tags/-v1"}}, true},
106 {"delete a dash branch", []RefUpdate{{Ref: "refs/heads/-old", IsDelete: true}}, false},
107 {"dash inside a name", []RefUpdate{{Ref: "refs/heads/fix-1"}}, false},
104108 }
105109 for _, tc := range cases {
106110 t.Run(tc.name, func(t *testing.T) {