vole
Clone
Read only, no credentials:
https://rodent.software/git/vole.git
Write over SSH with a registered key:
git clone ssh://git@0.0.0.0:2222/vole.git
A free and open-source forge that lives in your Git repo.
Merge change request 01M70WES5ZZDJ6RP226WB11BBZ: Let people rename and remove their own passkeys
keenan · 1h ago
4b8c9e1d
| 1 | package githttp |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log/slog" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "rodent.software/vole/core/git" |
| 13 | "rodent.software/vole/forge/access" |
| 14 | "rodent.software/vole/forge/repo" |
| 15 | ) |
| 16 | |
| 17 | func TestRoute(t *testing.T) { |
| 18 | t.Parallel() |
| 19 | for _, c := range []struct { |
| 20 | path, name, rest string |
| 21 | ok bool |
| 22 | }{ |
| 23 | {"/demo.git/info/refs", "demo", "info/refs", true}, |
| 24 | {"/demo.git/git-upload-pack", "demo", "git-upload-pack", true}, |
| 25 | // Found by FuzzRoute: an empty name used to pass, and only the store |
| 26 | // refused it. |
| 27 | {"/.git/info/refs", "", "", false}, |
| 28 | {"/demo/info/refs", "", "", false}, |
| 29 | {"/other/demo.git/info/refs", "", "", false}, |
| 30 | } { |
| 31 | name, rest, ok := Route(c.path) |
| 32 | if ok != c.ok || name != c.name || rest != c.rest { |
| 33 | t.Errorf("Route(%q)=(%q,%q,%v) want (%q,%q,%v)", c.path, name, rest, ok, c.name, c.rest, c.ok) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestIsPush(t *testing.T) { |
| 39 | t.Parallel() |
| 40 | for _, c := range []struct { |
| 41 | rest, query string |
| 42 | want bool |
| 43 | }{ |
| 44 | {"info/refs", "service=git-receive-pack", true}, |
| 45 | {"info/refs", "service=git-upload-pack", false}, |
| 46 | {"git-receive-pack", "", true}, |
| 47 | {"git-upload-pack", "", false}, |
| 48 | } { |
| 49 | if got := IsPush(c.rest, c.query); got != c.want { |
| 50 | t.Errorf("IsPush(%q,%q)=%v want %v", c.rest, c.query, got, c.want) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // makeRepo creates a repo with one commit and returns its name. |
| 56 | func makeRepo(t *testing.T, root string, public bool, acls *access.Store) string { |
| 57 | t.Helper() |
| 58 | st := repo.New(root, "/bin/true", acls) |
| 59 | m, err := st.Create("demo", "owner", "") |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | g := git.Open(m.Dir) |
| 64 | blob, err := g.HashObject([]byte("hello\n")) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | tree, err := g.MkTree([]git.Entry{{Mode: "100644", Type: "blob", SHA: blob, Name: "readme.md"}}) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | commit, err := g.CommitTree(tree, "", "init") |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if err := g.UpdateRef("refs/heads/main", commit); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if public { |
| 80 | a, err := m.ACL() |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | a.Default = "read" |
| 85 | if err := m.SetACL(a, "test"); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | } |
| 89 | return m.Name |
| 90 | } |
| 91 | |
| 92 | func newProxy(t *testing.T, public bool) *Proxy { |
| 93 | t.Helper() |
| 94 | root := filepath.Join(t.TempDir(), "repos") |
| 95 | // The creator and the proxy are two Store values over one root, so they have to |
| 96 | // share the database the rules were written to. |
| 97 | acls, err := access.Open(filepath.Join(filepath.Dir(root), "users.db")) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | acls.AuditPath = filepath.Join(filepath.Dir(root), "audit.log") |
| 102 | t.Cleanup(func() { acls.Close() }) |
| 103 | makeRepo(t, root, public, acls) |
| 104 | st := repo.New(root, "/bin/true", acls) |
| 105 | return New(Config{ |
| 106 | Root: root, |
| 107 | Open: st.Open, |
| 108 | Log: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | func TestPrivateRepoIs404(t *testing.T) { |
| 113 | t.Parallel() |
| 114 | p := newProxy(t, false) |
| 115 | req := httptest.NewRequest(http.MethodGet, "/demo.git/info/refs?service=git-upload-pack", nil) |
| 116 | rec := httptest.NewRecorder() |
| 117 | p.ServeHTTP(rec, req) |
| 118 | if rec.Code != http.StatusNotFound { |
| 119 | t.Fatalf("private repo answered %d, want 404 (403 would confirm it exists)", rec.Code) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestPublicRepoServesUploadPack(t *testing.T) { |
| 124 | t.Parallel() |
| 125 | p := newProxy(t, true) |
| 126 | req := httptest.NewRequest(http.MethodGet, "/demo.git/info/refs?service=git-upload-pack", nil) |
| 127 | rec := httptest.NewRecorder() |
| 128 | p.ServeHTTP(rec, req) |
| 129 | if rec.Code != http.StatusOK { |
| 130 | t.Fatalf("status = %d, want 200 (body: %s)", rec.Code, rec.Body.String()) |
| 131 | } |
| 132 | if !strings.Contains(rec.Body.String(), "refs/heads/main") { |
| 133 | t.Fatalf("advertisement missing refs: %q", rec.Body.String()) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestPushOverHTTPRefused(t *testing.T) { |
| 138 | t.Parallel() |
| 139 | p := newProxy(t, true) |
| 140 | for _, target := range []string{ |
| 141 | "/demo.git/info/refs?service=git-receive-pack", |
| 142 | "/demo.git/git-receive-pack", |
| 143 | } { |
| 144 | req := httptest.NewRequest(http.MethodPost, target, strings.NewReader("x")) |
| 145 | req.ContentLength = 1 |
| 146 | rec := httptest.NewRecorder() |
| 147 | p.ServeHTTP(rec, req) |
| 148 | if rec.Code != http.StatusForbidden { |
| 149 | t.Fatalf("%s: status = %d, want 403", target, rec.Code) |
| 150 | } |
| 151 | if !strings.Contains(rec.Body.String(), "SSH") { |
| 152 | t.Fatalf("%s: message should point at SSH: %s", target, rec.Body.String()) |
| 153 | } |
| 154 | } |
| 155 | if got := p.Stats()["push_attempts"]; got != 2 { |
| 156 | t.Fatalf("push_attempts = %d, want 2", got) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestChunkedReadBodyRefused(t *testing.T) { |
| 161 | t.Parallel() |
| 162 | p := newProxy(t, true) |
| 163 | req := httptest.NewRequest(http.MethodPost, "/demo.git/git-upload-pack", strings.NewReader("want x")) |
| 164 | req.ContentLength = -1 |
| 165 | rec := httptest.NewRecorder() |
| 166 | p.ServeHTTP(rec, req) |
| 167 | if rec.Code != http.StatusLengthRequired { |
| 168 | t.Fatalf("status = %d, want 411", rec.Code) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func TestDumbHTTPRefused(t *testing.T) { |
| 173 | t.Parallel() |
| 174 | p := newProxy(t, true) |
| 175 | for _, target := range []string{"/demo.git/HEAD", "/demo.git/objects/info/packs"} { |
| 176 | req := httptest.NewRequest(http.MethodGet, target, nil) |
| 177 | rec := httptest.NewRecorder() |
| 178 | p.ServeHTTP(rec, req) |
| 179 | if rec.Code != http.StatusNotFound { |
| 180 | t.Fatalf("%s: status = %d, want 404", target, rec.Code) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 |