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 {
376 fmt.Fprintf(os.Stderr, "gitbay: !%s is %s\n", n, state) 376 fmt.Fprintf(os.Stderr, "gitbay: !%s is %s\n", n, state)
377 return protocol.ExitUsage 377 return protocol.ExitUsage
378 } 378 }
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 }
379 // A fork's branch lives in a repository this clone does not push to, 385 // A fork's branch lives in a repository this clone does not push to,
380 // and guessing which remote that is would be worse than saying so. 386 // and guessing which remote that is would be worse than saying so.
381 if strings.Contains(source, ":") { 387 if strings.Contains(source, ":") {
internal/control/repo.go +3
@@ -925,6 +925,9 @@ func runSetDefaultBranch(c *Ctx, args []string) int {
925 return code 925 return code
926 } 926 }
927 branch := args[1] 927 branch := args[1]
928 if strings.HasPrefix(branch, "-") {
929 return c.fail(protocol.ExitUsage, "a default branch may not start with '-'")
930 }
928 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name) 931 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
929 if _, err := gitutil.ResolveRef(dir, "refs/heads/"+branch); err != nil { 932 if _, err := gitutil.ResolveRef(dir, "refs/heads/"+branch); err != nil {
930 return c.fail(protocol.ExitFailure, "no branch named %q on %s", branch, repo.Path()) 933 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) {
295 t.Errorf("reopening a finding should open a fresh issue, got #%d", reopened.IssueNumber) 295 t.Errorf("reopening a finding should open a fresh issue, got #%d", reopened.IssueNumber)
296 } 296 }
297} 297}
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) {
22 22
23 var sink bytes.Buffer 23 var sink bytes.Buffer
24 calls := map[string]func() error{ 24 calls := map[string]func() error{
25 "CountCommits": func() error { CountCommits(dir, ref); return nil }, 25 "CountCommits": func() error { CountCommits(dir, ref); return nil },
26 "Contributors": func() error { Contributors(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 }, 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 }, 28 "RevList": func() error { _, err := RevList(dir, ref, 1); return err },
29 "RevListPath": func() error { _, err := RevListPath(dir, ref, "f.txt", 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 }, 30 "PeelToCommit": func() error { _, err := PeelToCommit(dir, ref); return err },
31 "ReadCommit": func() error { _, err := ReadCommit(dir, ref); return err }, 31 "ReadCommit": func() error { _, err := ReadCommit(dir, ref); return err },
32 "ListTree": func() error { _, err := ListTree(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 }, 33 "ReadBlob": func() error { _, err := ReadBlob(dir, ref, "f.txt", 1<<20); return err },
34 "ResolveRef": func() error { _, err := ResolveRef(dir, ref); return err }, 34 "ResolveRef": func() error { _, err := ResolveRef(dir, ref); return err },
35 "Reachable": func() error { _, err := Reachable(dir, ref); return err }, 35 "Reachable": func() error { _, err := Reachable(dir, ref); return err },
36 "Archive": func() error { return Archive(dir, ref, "x", &sink) }, 36 "Archive": func() error { return Archive(dir, ref, "x", &sink) },
37 "Grep": func() error { _, err := Grep(dir, ref, "hi", 10); return err }, 37 "Grep": func() error { _, err := Grep(dir, ref, "hi", 10); return err },
38 "MergeBase": func() error { _, err := MergeBase(dir, ref, "main"); 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 }, 39 "Diff": func() error { _, _, err := Diff(dir, ref, "main", 1<<20); return err },
40 "DiffFiles": func() error { _, err := DiffFiles(dir, ref, "main"); return err }, 40 "DiffFiles": func() error { _, err := DiffFiles(dir, ref, "main"); return err },
41 "RevListRange": func() error { _, err := RevListRange(dir, "main", ref); 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 }, 42 "Blame": func() error { _, err := Blame(dir, ref, "f.txt", 1, 1); return err },
43 "TipCommit": func() error { TipCommit(dir, ref); return nil }, 43 "TipCommit": func() error { TipCommit(dir, ref); return nil },
44 "StatPath": func() error { StatPath(dir, ref, "f.txt"); 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 },
45 } 50 }
46 for name, call := range calls { 51 for name, call := range calls {
47 call() 52 call()
internal/gitutil/gitutil.go +1 −1
@@ -93,7 +93,7 @@ func RunUntil(cmd *exec.Cmd, cancel <-chan struct{}) error {
93// dir. It must run with the caller's environment intact so that quarantined 93// dir. It must run with the caller's environment intact so that quarantined
94// objects during pre-receive remain visible. 94// objects during pre-receive remain visible.
95func IsAncestor(dir, old, new string) (bool, error) { 95func 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)
97 err := cmd.Run() 97 err := cmd.Run()
98 if err == nil { 98 if err == nil {
99 return true, nil 99 return true, nil
internal/gitutil/messages.go +4 −4
@@ -20,7 +20,7 @@ type CommitAuthor struct {
20// reachable from new but not old, capped at max. Empty or zero old lists 20// reachable from new but not old, capped at max. Empty or zero old lists
21// from new alone. 21// from new alone.
22func RevListAuthors(dir, old, new string, max int) ([]CommitAuthor, error) { 22func 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}
24 if old != "" && old != zeroSHA { 24 if old != "" && old != zeroSHA {
25 args = append(args, "^"+old) 25 args = append(args, "^"+old)
26 } 26 }
@@ -41,7 +41,7 @@ func RevListAuthors(dir, old, new string, max int) ([]CommitAuthor, error) {
41 41
42// Parents returns a commit's parent shas. 42// Parents returns a commit's parent shas.
43func Parents(dir, sha string) []string { 43func 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()
45 if err != nil { 45 if err != nil {
46 return nil 46 return nil
47 } 47 }
@@ -51,7 +51,7 @@ func Parents(dir, sha string) []string {
51// LastCommitDate returns the committer date (YYYY-MM-DD) of the ref tip, 51// LastCommitDate returns the committer date (YYYY-MM-DD) of the ref tip,
52// or "" for empty repos. 52// or "" for empty repos.
53func LastCommitDate(dir, ref string) string { 53func 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()
55 if err != nil { 55 if err != nil {
56 return "" 56 return ""
57 } 57 }
@@ -74,7 +74,7 @@ type CommitMsg struct {
74// new but not old, newest first, capped at max. An empty or zero old (new 74// new but not old, newest first, capped at max. An empty or zero old (new
75// branch) lists from new alone, still capped. 75// branch) lists from new alone, still capped.
76func RevListMessages(dir, old, new string, max int) ([]CommitMsg, error) { 76func 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}
78 if old != "" && old != zeroSHA { 78 if old != "" && old != zeroSHA {
79 args = append(args, "^"+old) 79 args = append(args, "^"+old)
80 } 80 }
internal/policy/access.go +16
@@ -99,6 +99,11 @@ func CheckPush(repo store.Repo, updates []RefUpdate) string {
99 if strings.HasPrefix(u.Ref, "refs/merge-requests/") { 99 if strings.HasPrefix(u.Ref, "refs/merge-requests/") {
100 return "refs/merge-requests/* is server-owned and cannot be pushed" 100 return "refs/merge-requests/* is server-owned and cannot be pushed"
101 } 101 }
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 }
102 // A protected tag is created once. Its globs match the tag name. 107 // A protected tag is created once. Its globs match the tag name.
103 if tag, ok := strings.CutPrefix(u.Ref, "refs/tags/"); ok && TagProtected(repo, tag) { 108 if tag, ok := strings.CutPrefix(u.Ref, "refs/tags/"); ok && TagProtected(repo, tag) {
104 if u.IsDelete { 109 if u.IsDelete {
@@ -139,3 +144,14 @@ func TagProtected(repo store.Repo, tag string) bool {
139} 144}
140 145
141func isZeroSHA(sha string) bool { return sha != "" && strings.Trim(sha, "0") == "" } 146func 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) {
101 {"delete unprotected", []RefUpdate{{Ref: "refs/heads/dev", IsDelete: true}}, false}, 101 {"delete unprotected", []RefUpdate{{Ref: "refs/heads/dev", IsDelete: true}}, false},
102 {"mr namespace", []RefUpdate{{Ref: "refs/merge-requests/1/head"}}, true}, 102 {"mr namespace", []RefUpdate{{Ref: "refs/merge-requests/1/head"}}, true},
103 {"tag alongside protected", []RefUpdate{{Ref: "refs/tags/v1"}, {Ref: "refs/heads/main"}}, false}, 103 {"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},
104 } 108 }
105 for _, tc := range cases { 109 for _, tc := range cases {
106 t.Run(tc.name, func(t *testing.T) { 110 t.Run(tc.name, func(t *testing.T) {