vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Let a release change until a channel names it
214e7ea67ccb37d356a52097f8ce838bdf4c1731 · Keenan Nemetz · 17d ago · parent 502119f4
Let a release change until a channel names it A release is mutable again. Notes can be edited and files added, replaced, or removed after publication, because a mistyped note or a rebuilt binary is not a reason to cut a new version. Editing the notes re-tags the same commit, which the old rule refused from the first attached file. The one thing that stays frozen is a release a channel names. A channel records the tag name and the tag object, so a re-tag would dangle it and a changed pointer would serve different bytes under a URL a package manager already cached. Pre-receive reads the channel table from the instance database and refuses every change to a channeled release — tag moves and deletes, and added, replaced, or removed pointers alike — naming the channel and how to move it. The browser applies the same rule: the edit form, the attach form, and the list link are all drawn from it. Attaching also works from the browser now, at parity with the CLI. The Artifacts card takes files, or a directory, and an optional directory to publish under; the handler streams each part to a temp file while hashing it, checks the object-store quota before storing anything, writes bytes first and pointers second, and calls the same metadata writer the CLI does. A directory upload keeps its paths, which is why the body is read as a multipart stream rather than through ParseForm.
server/release_attach_web_test.go +262
| @@ -0,0 +1,262 @@ | |||
| 1 | package server_test | ||
| 2 | |||
| 3 | // Browser parity for release files: the same attach the CLI performs, the same | ||
| 4 | // pointer it writes, and the same rule — a release a channel names does not | ||
| 5 | // change. | ||
| 6 | |||
| 7 | import ( | ||
| 8 | "bytes" | ||
| 9 | "io" | ||
| 10 | "mime/multipart" | ||
| 11 | "net/http" | ||
| 12 | "net/url" | ||
| 13 | "strings" | ||
| 14 | "testing" | ||
| 15 | "time" | ||
| 16 | |||
| 17 | "rodent.software/vole/metadata" | ||
| 18 | ) | ||
| 19 | |||
| 20 | // uploadRelease posts the attach form the way a browser does: one multipart body | ||
| 21 | // carrying the CSRF field, an optional path prefix, and one part per file. | ||
| 22 | func uploadRelease(t *testing.T, f *fixture, cookie, tag, csrf, prefix string, files map[string]string) (*http.Response, string) { | ||
| 23 | t.Helper() | ||
| 24 | var buf bytes.Buffer | ||
| 25 | mw := multipart.NewWriter(&buf) | ||
| 26 | if err := mw.WriteField("csrf", csrf); err != nil { | ||
| 27 | t.Fatal(err) | ||
| 28 | } | ||
| 29 | if prefix != "" { | ||
| 30 | if err := mw.WriteField("path", prefix); err != nil { | ||
| 31 | t.Fatal(err) | ||
| 32 | } | ||
| 33 | } | ||
| 34 | for name, body := range files { | ||
| 35 | w, err := mw.CreateFormFile("files", name) | ||
| 36 | if err != nil { | ||
| 37 | t.Fatal(err) | ||
| 38 | } | ||
| 39 | if _, err := io.WriteString(w, body); err != nil { | ||
| 40 | t.Fatal(err) | ||
| 41 | } | ||
| 42 | } | ||
| 43 | if err := mw.Close(); err != nil { | ||
| 44 | t.Fatal(err) | ||
| 45 | } | ||
| 46 | req, err := http.NewRequest("POST", f.http.URL+"/"+f.repo+"/releases/"+tag+"/attach", &buf) | ||
| 47 | if err != nil { | ||
| 48 | t.Fatal(err) | ||
| 49 | } | ||
| 50 | req.Header.Set("Content-Type", mw.FormDataContentType()) | ||
| 51 | if cookie != "" { | ||
| 52 | req.Header.Set("Cookie", cookie) | ||
| 53 | } | ||
| 54 | resp, err := noRedirectAuth.Do(req) | ||
| 55 | if err != nil { | ||
| 56 | t.Fatal(err) | ||
| 57 | } | ||
| 58 | body, _ := io.ReadAll(resp.Body) | ||
| 59 | resp.Body.Close() | ||
| 60 | return resp, string(body) | ||
| 61 | } | ||
| 62 | |||
| 63 | // waitForPage polls a page until it shows what a write just published. A browser | ||
| 64 | // write wakes the metadata loop rather than waiting for it, so the read model is | ||
| 65 | // allowed to lag by that one tick. | ||
| 66 | func waitForPage(t *testing.T, f *fixture, cookie, path, want string) string { | ||
| 67 | t.Helper() | ||
| 68 | deadline := time.Now().Add(3 * time.Second) | ||
| 69 | var body string | ||
| 70 | for { | ||
| 71 | resp, got := f.browserGet(t, path, cookie) | ||
| 72 | body = got | ||
| 73 | if resp.StatusCode == http.StatusOK && strings.Contains(got, want) { | ||
| 74 | return got | ||
| 75 | } | ||
| 76 | if time.Now().After(deadline) { | ||
| 77 | t.Fatalf("%s does not show %q:\n%s", path, want, body) | ||
| 78 | } | ||
| 79 | time.Sleep(25 * time.Millisecond) | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | func TestAttachFromTheBrowser(t *testing.T) { | ||
| 84 | t.Parallel() | ||
| 85 | f := newFixture(t) | ||
| 86 | f.publish(f.repo) | ||
| 87 | work := workRepo(t, map[string]string{"a.txt": "one\n"}) | ||
| 88 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil { | ||
| 89 | t.Fatalf("push: %s", errs) | ||
| 90 | } | ||
| 91 | if _, errs, err := runGit(t, work, "tag", "-a", "v1.0.0", "-m", "notes\n"); err != nil { | ||
| 92 | t.Fatalf("tag: %s", errs) | ||
| 93 | } | ||
| 94 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil { | ||
| 95 | t.Fatalf("push tag: %s", errs) | ||
| 96 | } | ||
| 97 | sess, cookie := f.browserSession(t, f.ownerID) | ||
| 98 | |||
| 99 | // The release page offers the form to a maintainer, and to nobody else. | ||
| 100 | _, page := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0", cookie) | ||
| 101 | if !strings.Contains(page, `enctype="multipart/form-data"`) || !strings.Contains(page, "Attach files") { | ||
| 102 | t.Fatalf("release page has no attach form:\n%s", page) | ||
| 103 | } | ||
| 104 | |||
| 105 | // A flat file keeps its base name. | ||
| 106 | resp, body := uploadRelease(t, f, cookie, "v1.0.0", sess.CSRF, "", map[string]string{"tool": "the released bytes"}) | ||
| 107 | if resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != "/"+f.repo+"/releases/v1.0.0" { | ||
| 108 | t.Fatalf("attach = %d %q:\n%s", resp.StatusCode, resp.Header.Get("Location"), body) | ||
| 109 | } | ||
| 110 | // A directory upload keeps its paths under the named prefix. | ||
| 111 | resp, body = uploadRelease(t, f, cookie, "v1.0.0", sess.CSRF, "apt", map[string]string{"Packages": "Package: vole\n"}) | ||
| 112 | if resp.StatusCode != http.StatusSeeOther { | ||
| 113 | t.Fatalf("attach under a path = %d:\n%s", resp.StatusCode, body) | ||
| 114 | } | ||
| 115 | |||
| 116 | // The pointer is the whole record, exactly as `vole release attach` writes it. | ||
| 117 | m := mustRepo(t, f) | ||
| 118 | arts, err := metadata.ListReleaseArtifacts(m.Git(), "v1.0.0") | ||
| 119 | if err != nil { | ||
| 120 | t.Fatal(err) | ||
| 121 | } | ||
| 122 | if len(arts) != 2 || arts[0].Name != "apt/Packages" || arts[1].Name != "tool" { | ||
| 123 | t.Fatalf("attached artifacts = %+v", arts) | ||
| 124 | } | ||
| 125 | if arts[1].Size != int64(len("the released bytes")) { | ||
| 126 | t.Fatalf("tool size = %d", arts[1].Size) | ||
| 127 | } | ||
| 128 | if !f.srv.LFS.Has(f.repo, arts[1].OID) { | ||
| 129 | t.Fatal("the bytes were not stored") | ||
| 130 | } | ||
| 131 | |||
| 132 | page = waitForPage(t, f, cookie, "/"+f.repo+"/releases/v1.0.0", "/artifacts/tool") | ||
| 133 | for _, want := range []string{"/artifacts/apt/Packages", "Attach files"} { | ||
| 134 | if !strings.Contains(page, want) { | ||
| 135 | t.Fatalf("release page does not show %q:\n%s", want, page) | ||
| 136 | } | ||
| 137 | } | ||
| 138 | resp, body = f.browserGet(t, "/"+f.repo+"/releases/v1.0.0/artifacts/tool", cookie) | ||
| 139 | if resp.StatusCode != http.StatusOK || body != "the released bytes" { | ||
| 140 | t.Fatalf("download = %d %q", resp.StatusCode, body) | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | func TestAttachRefusalsFromTheBrowser(t *testing.T) { | ||
| 145 | t.Parallel() | ||
| 146 | f := newFixture(t) | ||
| 147 | f.publish(f.repo) | ||
| 148 | work := workRepo(t, map[string]string{"a.txt": "one\n"}) | ||
| 149 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil { | ||
| 150 | t.Fatalf("push: %s", errs) | ||
| 151 | } | ||
| 152 | if _, errs, err := runGit(t, work, "tag", "-a", "v1.0.0", "-m", "notes\n"); err != nil { | ||
| 153 | t.Fatalf("tag: %s", errs) | ||
| 154 | } | ||
| 155 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil { | ||
| 156 | t.Fatalf("push tag: %s", errs) | ||
| 157 | } | ||
| 158 | m := mustRepo(t, f) | ||
| 159 | commit, _ := m.Git().RevParse("main") | ||
| 160 | if err := m.Git().UpdateRef("refs/tags/light", commit); err != nil { | ||
| 161 | t.Fatal(err) | ||
| 162 | } | ||
| 163 | sess, cookie := f.browserSession(t, f.ownerID) | ||
| 164 | |||
| 165 | // A token in the body still has to be this session's token. | ||
| 166 | if resp, body := uploadRelease(t, f, cookie, "v1.0.0", "not-a-token", "", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusForbidden { | ||
| 167 | t.Fatalf("bad csrf = %d:\n%s", resp.StatusCode, body) | ||
| 168 | } | ||
| 169 | // A path that climbs out of the release is refused before anything is stored. | ||
| 170 | if resp, body := uploadRelease(t, f, cookie, "v1.0.0", sess.CSRF, "../env", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusBadRequest { | ||
| 171 | t.Fatalf("path traversal = %d:\n%s", resp.StatusCode, body) | ||
| 172 | } | ||
| 173 | // A lightweight tag has no release to attach to. | ||
| 174 | if resp, body := uploadRelease(t, f, cookie, "light", sess.CSRF, "", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusBadRequest || !strings.Contains(body, "lightweight") { | ||
| 175 | t.Fatalf("lightweight attach = %d:\n%s", resp.StatusCode, body) | ||
| 176 | } | ||
| 177 | // An anonymous visitor gets the login page, not the form. | ||
| 178 | if resp, _ := uploadRelease(t, f, "", "v1.0.0", sess.CSRF, "", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusSeeOther || !strings.HasPrefix(resp.Header.Get("Location"), "/-/login") { | ||
| 179 | t.Fatalf("anonymous attach = %d %q", resp.StatusCode, resp.Header.Get("Location")) | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | // TestEditNotesAfterAttachingFiles is the change this rule set exists for: an | ||
| 184 | // attached file no longer freezes the notes, but a channel still does. | ||
| 185 | func TestEditNotesAfterAttachingFiles(t *testing.T) { | ||
| 186 | t.Parallel() | ||
| 187 | f := newFixture(t) | ||
| 188 | f.publish(f.repo) | ||
| 189 | work := workRepo(t, map[string]string{"a.txt": "one\n"}) | ||
| 190 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil { | ||
| 191 | t.Fatalf("push: %s", errs) | ||
| 192 | } | ||
| 193 | if _, errs, err := runGit(t, work, "tag", "-a", "v1.0.0", "-m", "First release.\n"); err != nil { | ||
| 194 | t.Fatalf("tag: %s", errs) | ||
| 195 | } | ||
| 196 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil { | ||
| 197 | t.Fatalf("push tag: %s", errs) | ||
| 198 | } | ||
| 199 | attachArtifact(t, f, "v1.0.0", "tool", []byte("bytes")) | ||
| 200 | sess, cookie := f.browserSession(t, f.ownerID) | ||
| 201 | |||
| 202 | resp, page := f.browserGet(t, "/"+f.repo+"/releases/edit?tag=v1.0.0", cookie) | ||
| 203 | if resp.StatusCode != http.StatusOK || !strings.Contains(page, "Save notes") { | ||
| 204 | t.Fatalf("edit page locks an attached release = %d:\n%s", resp.StatusCode, page) | ||
| 205 | } | ||
| 206 | form := url.Values{"csrf": {sess.CSRF}, "tag": {"v1.0.0"}, "notes": {"Corrected notes.\n"}} | ||
| 207 | resp, body := f.browserPost(t, "/"+f.repo+"/releases/edit", cookie, form) | ||
| 208 | if resp.StatusCode != http.StatusSeeOther { | ||
| 209 | t.Fatalf("editing after an attach = %d:\n%s", resp.StatusCode, body) | ||
| 210 | } | ||
| 211 | m := mustRepo(t, f) | ||
| 212 | if tag, err := m.Git().Tag("v1.0.0"); err != nil || !strings.Contains(tag.Message, "Corrected notes.") { | ||
| 213 | t.Fatalf("edited notes = %+v %v", tag, err) | ||
| 214 | } | ||
| 215 | // The attachment survives the re-tag: the pointers name paths, not the tag object. | ||
| 216 | if arts, err := metadata.ListReleaseArtifacts(m.Git(), "v1.0.0"); err != nil || len(arts) != 1 || arts[0].Name != "tool" { | ||
| 217 | t.Fatalf("artifacts after a notes edit = %+v %v", arts, err) | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | func TestChanneledReleaseIsLockedInTheBrowser(t *testing.T) { | ||
| 222 | t.Parallel() | ||
| 223 | f := releaseFixture(t) | ||
| 224 | setChannel(t, f, "stable", "v1.0.0") | ||
| 225 | sess, cookie := f.browserSession(t, f.ownerID) | ||
| 226 | |||
| 227 | _, page := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0", cookie) | ||
| 228 | if !strings.Contains(page, "A channel targets this release") { | ||
| 229 | t.Fatalf("release page does not explain the lock:\n%s", page) | ||
| 230 | } | ||
| 231 | for _, unwanted := range []string{"Edit notes", `enctype="multipart/form-data"`} { | ||
| 232 | if strings.Contains(page, unwanted) { | ||
| 233 | t.Fatalf("release page still offers %q:\n%s", unwanted, page) | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | resp, page := f.browserGet(t, "/"+f.repo+"/releases/edit?tag=v1.0.0", cookie) | ||
| 238 | if resp.StatusCode != http.StatusOK || !strings.Contains(page, "Channel stable targets this release") { | ||
| 239 | t.Fatalf("edit page = %d:\n%s", resp.StatusCode, page) | ||
| 240 | } | ||
| 241 | if strings.Contains(page, "Save notes") { | ||
| 242 | t.Fatalf("edit page offers a submit button it would refuse:\n%s", page) | ||
| 243 | } | ||
| 244 | form := url.Values{"csrf": {sess.CSRF}, "tag": {"v1.0.0"}, "notes": {"sneaky\n"}} | ||
| 245 | if resp, body := f.browserPost(t, "/"+f.repo+"/releases/edit", cookie, form); resp.StatusCode != http.StatusBadRequest || !strings.Contains(body, "Channel stable") { | ||
| 246 | t.Fatalf("editing a channeled release = %d:\n%s", resp.StatusCode, body) | ||
| 247 | } | ||
| 248 | if resp, body := uploadRelease(t, f, cookie, "v1.0.0", sess.CSRF, "", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusConflict || !strings.Contains(body, "Channel stable") { | ||
| 249 | t.Fatalf("attaching to a channeled release = %d:\n%s", resp.StatusCode, body) | ||
| 250 | } | ||
| 251 | |||
| 252 | // The list does not link to a form that cannot save. | ||
| 253 | _, list := f.browserGet(t, "/"+f.repo+"/releases", cookie) | ||
| 254 | if strings.Contains(list, "/releases/edit?tag=v1.0.0") { | ||
| 255 | t.Fatalf("the release list still offers to edit a channeled release:\n%s", list) | ||
| 256 | } | ||
| 257 | // Moving the channel re-opens the form. | ||
| 258 | setChannel(t, f, "stable", "v0.9.0") | ||
| 259 | if _, page := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0", cookie); !strings.Contains(page, "Edit notes") { | ||
| 260 | t.Fatalf("release page did not re-open after the channel moved:\n%s", page) | ||
| 261 | } | ||
| 262 | } | ||