| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "archive/tar" |
| 5 | "archive/zip" |
| 6 | "bytes" |
| 7 | "compress/gzip" |
| 8 | "crypto/sha256" |
| 9 | "encoding/hex" |
| 10 | "io" |
| 11 | "net/http" |
| 12 | "net/http/cookiejar" |
| 13 | "net/http/httptest" |
| 14 | "net/url" |
| 15 | "os" |
| 16 | "os/exec" |
| 17 | "path/filepath" |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 22 | "gitlab.com/Timfa/bluegit/internal/config" |
| 23 | "gitlab.com/Timfa/bluegit/internal/store" |
| 24 | ) |
| 25 | |
| 26 | func TestCommitArchivePublicAndStable(t *testing.T) { |
| 27 | if _, err := exec.LookPath("git"); err != nil { |
| 28 | t.Skip("git ontbreekt") |
| 29 | } |
| 30 | storage := t.TempDir() |
| 31 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | t.Cleanup(func() { _ = st.Close() }) |
| 36 | cfg := config.Config{ |
| 37 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 38 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 39 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 40 | } |
| 41 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 42 | ts := httptest.NewServer(srv.Handler()) |
| 43 | t.Cleanup(ts.Close) |
| 44 | jar, _ := cookiejar.New(nil) |
| 45 | tim := &http.Client{Jar: jar} |
| 46 | anon := &http.Client{} |
| 47 | |
| 48 | page := read(t, mustGet(t, tim, ts.URL+"/login")) |
| 49 | page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{ |
| 50 | "csrf": {csrfOf(t, page)}, "name": {"archief"}, "visibility": {"public"}, |
| 51 | })) |
| 52 | page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{ |
| 53 | "csrf": {csrfOf(t, page)}, "name": {"laptop"}, |
| 54 | })) |
| 55 | token := between(page, `<p class="code">`, "</p>") |
| 56 | work := t.TempDir() |
| 57 | git(t, work, "init", "-b", "master") |
| 58 | git(t, work, "config", "user.email", "tim@users.git.timfalken.com") |
| 59 | git(t, work, "config", "user.name", "tim") |
| 60 | writeFile(t, filepath.Join(work, "hello.txt"), "alpha\n") |
| 61 | git(t, work, "add", "hello.txt") |
| 62 | git(t, work, "commit", "-m", "start") |
| 63 | remote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/archief.git" |
| 64 | git(t, work, "remote", "add", "origin", remote) |
| 65 | git(t, work, "push", "-u", "origin", "master") |
| 66 | sha := strings.TrimSpace(gitOutOK(t, work, "rev-parse", "HEAD")) |
| 67 | |
| 68 | tarURL := ts.URL + "/tim/archief/-/archive/" + sha + "/archief-" + sha + ".tar.gz" |
| 69 | first := getBody(t, anon, tarURL) |
| 70 | second := getBody(t, anon, tarURL) |
| 71 | if first.code != http.StatusOK || !bytes.Equal(first.body, second.body) { |
| 72 | t.Fatalf("tar.gz %d, gelijk %v, lengte %d", first.code, bytes.Equal(first.body, second.body), len(first.body)) |
| 73 | } |
| 74 | if len(first.body) < 2 || first.body[0] != 0x1f || first.body[1] != 0x8b { |
| 75 | t.Fatal("geen gzip") |
| 76 | } |
| 77 | if !strings.Contains(first.header.Get("Content-Disposition"), "archief-"+sha+".tar.gz") { |
| 78 | t.Fatalf("bestandsnaam: %s", first.header.Get("Content-Disposition")) |
| 79 | } |
| 80 | sum := sha256.Sum256(first.body) |
| 81 | if hex.EncodeToString(sum[:]) != hex.EncodeToString(sha256Sum(second.body)) { |
| 82 | t.Fatal("sha256 verschilt tussen twee downloads") |
| 83 | } |
| 84 | names := tarNames(t, first.body) |
| 85 | want := "archief-" + sha + "/hello.txt" |
| 86 | if !containsString(names, want) { |
| 87 | t.Fatalf("tar mist %s in %v", want, names) |
| 88 | } |
| 89 | |
| 90 | zipURL := ts.URL + "/tim/archief/-/archive/" + sha + "/archief-" + sha + ".zip" |
| 91 | zipRes := getBody(t, anon, zipURL) |
| 92 | if zipRes.code != http.StatusOK || len(zipRes.body) < 2 || zipRes.body[0] != 'P' || zipRes.body[1] != 'K' { |
| 93 | t.Fatalf("zip %d", zipRes.code) |
| 94 | } |
| 95 | zr, err := zip.NewReader(bytes.NewReader(zipRes.body), int64(len(zipRes.body))) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | found := false |
| 100 | for _, f := range zr.File { |
| 101 | if f.Name == want { |
| 102 | found = true |
| 103 | } |
| 104 | } |
| 105 | if !found { |
| 106 | t.Fatalf("zip mist %s", want) |
| 107 | } |
| 108 | again := getBody(t, anon, zipURL) |
| 109 | if !bytes.Equal(zipRes.body, again.body) { |
| 110 | t.Fatal("zip verschilt tussen twee downloads") |
| 111 | } |
| 112 | |
| 113 | for _, raw := range []string{ |
| 114 | ts.URL + "/tim/archief/-/archive/" + sha[:12] + "/archief.tar.gz", |
| 115 | ts.URL + "/tim/archief/-/archive/master/archief-master.tar.gz", |
| 116 | ts.URL + "/tim/archief/-/archive/" + sha + "/archief-" + sha + ".tar.bz2", |
| 117 | ts.URL + "/tim/archief/-/archive/" + strings.Repeat("ab", 20) + "/archief.tar.gz", |
| 118 | } { |
| 119 | res := getBody(t, anon, raw) |
| 120 | if res.code != http.StatusNotFound { |
| 121 | t.Fatalf("%s: %d", raw, res.code) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{ |
| 126 | "csrf": {csrfOf(t, page)}, "name": {"geheim"}, "visibility": {"private"}, |
| 127 | })) |
| 128 | secret := t.TempDir() |
| 129 | git(t, secret, "init", "-b", "master") |
| 130 | git(t, secret, "config", "user.email", "tim@users.git.timfalken.com") |
| 131 | git(t, secret, "config", "user.name", "tim") |
| 132 | writeFile(t, filepath.Join(secret, "hello.txt"), "geheim\n") |
| 133 | git(t, secret, "add", "hello.txt") |
| 134 | git(t, secret, "commit", "-m", "geheim") |
| 135 | privRemote := strings.Replace(ts.URL, "http://", "http://tim:"+token+"@", 1) + "/tim/geheim.git" |
| 136 | git(t, secret, "remote", "add", "origin", privRemote) |
| 137 | git(t, secret, "push", "-u", "origin", "master") |
| 138 | privSHA := strings.TrimSpace(gitOutOK(t, secret, "rev-parse", "HEAD")) |
| 139 | hidden := getBody(t, anon, ts.URL+"/tim/geheim/-/archive/"+privSHA+"/geheim-"+privSHA+".tar.gz") |
| 140 | if hidden.code != http.StatusNotFound { |
| 141 | t.Fatalf("privé-archief: %d", hidden.code) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | type bodyResult struct { |
| 146 | code int |
| 147 | header http.Header |
| 148 | body []byte |
| 149 | } |
| 150 | |
| 151 | func getBody(t *testing.T, c *http.Client, raw string) bodyResult { |
| 152 | t.Helper() |
| 153 | res := mustGet(t, c, raw) |
| 154 | defer res.Body.Close() |
| 155 | body, err := io.ReadAll(res.Body) |
| 156 | if err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | return bodyResult{code: res.StatusCode, header: res.Header, body: body} |
| 160 | } |
| 161 | |
| 162 | // TestPKGBUILDArchiveURL vraagt de bron-URL uit packaging/aur/PKGBUILD. |
| 163 | // Die vorm is /timfa/bluegit/-/archive/<sha>/bluegit-<sha>.tar.gz. |
| 164 | // /-/archive/master/bluegit-master.tar.gz blijft 404: fullHexSHA eist |
| 165 | // 40 kleine hex-tekens, met en zonder Authorization-header. |
| 166 | func TestPKGBUILDArchiveURL(t *testing.T) { |
| 167 | if _, err := exec.LookPath("git"); err != nil { |
| 168 | t.Skip("git ontbreekt") |
| 169 | } |
| 170 | pkg, err := os.ReadFile(filepath.Join("..", "..", "packaging", "aur", "PKGBUILD")) |
| 171 | if err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | const published = "https://git.timfalken.com/timfa/bluegit/-/archive/${_commit}/bluegit-${_commit}.tar.gz" |
| 175 | if !strings.Contains(string(pkg), published) { |
| 176 | t.Fatalf("PKGBUILD mist %s", published) |
| 177 | } |
| 178 | |
| 179 | storage := t.TempDir() |
| 180 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | t.Cleanup(func() { _ = st.Close() }) |
| 185 | cfg := config.Config{ |
| 186 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 187 | Auth: config.Auth{Mode: "dev", DevUser: "timfa", DevAuthID: 1, UserAgent: "test", BaseURL: "https://auth.timfalken.com"}, |
| 188 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 189 | } |
| 190 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "timfa", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 191 | ts := httptest.NewServer(srv.Handler()) |
| 192 | t.Cleanup(ts.Close) |
| 193 | jar, _ := cookiejar.New(nil) |
| 194 | tim := &http.Client{Jar: jar} |
| 195 | |
| 196 | page := read(t, mustGet(t, tim, ts.URL+"/login")) |
| 197 | page = read(t, mustPost(t, tim, ts.URL+"/new/repo", url.Values{ |
| 198 | "csrf": {csrfOf(t, page)}, "name": {"bluegit"}, "visibility": {"public"}, |
| 199 | })) |
| 200 | page = read(t, mustPost(t, tim, ts.URL+"/settings/tokens", url.Values{ |
| 201 | "csrf": {csrfOf(t, page)}, "name": {"aur"}, |
| 202 | })) |
| 203 | token := between(page, `<p class="code">`, "</p>") |
| 204 | if !strings.HasPrefix(token, "bg_") { |
| 205 | t.Fatalf("token %q", token) |
| 206 | } |
| 207 | work := t.TempDir() |
| 208 | git(t, work, "init", "-b", "master") |
| 209 | git(t, work, "config", "user.email", "timfa@users.git.timfalken.com") |
| 210 | git(t, work, "config", "user.name", "timfa") |
| 211 | writeFile(t, filepath.Join(work, "hello.txt"), "alpha\n") |
| 212 | git(t, work, "add", "hello.txt") |
| 213 | git(t, work, "commit", "-m", "start") |
| 214 | remote := strings.Replace(ts.URL, "http://", "http://timfa:"+token+"@", 1) + "/timfa/bluegit.git" |
| 215 | git(t, work, "remote", "add", "origin", remote) |
| 216 | git(t, work, "push", "-u", "origin", "master") |
| 217 | sha := strings.TrimSpace(gitOutOK(t, work, "rev-parse", "HEAD")) |
| 218 | |
| 219 | path := strings.TrimPrefix(published, "https://git.timfalken.com") |
| 220 | path = strings.ReplaceAll(path, "${_commit}", sha) |
| 221 | tarURL := ts.URL + path |
| 222 | anon := getBody(t, &http.Client{}, tarURL) |
| 223 | if anon.code != http.StatusOK || len(anon.body) < 2 || anon.body[0] != 0x1f || anon.body[1] != 0x8b { |
| 224 | t.Fatalf("PKGBUILD-archief %d, lengte %d", anon.code, len(anon.body)) |
| 225 | } |
| 226 | if !strings.Contains(anon.header.Get("Content-Disposition"), "bluegit-"+sha+".tar.gz") { |
| 227 | t.Fatalf("bestandsnaam: %s", anon.header.Get("Content-Disposition")) |
| 228 | } |
| 229 | want := "bluegit-" + sha + "/hello.txt" |
| 230 | if !containsString(tarNames(t, anon.body), want) { |
| 231 | t.Fatal("tar mist " + want) |
| 232 | } |
| 233 | tokenRes := getBearer(t, tarURL, token) |
| 234 | tokenBody := []byte(read(t, tokenRes)) |
| 235 | if tokenRes.StatusCode != http.StatusOK || !bytes.Equal(anon.body, tokenBody) { |
| 236 | t.Fatalf("met token %d, gelijk %v", tokenRes.StatusCode, bytes.Equal(anon.body, tokenBody)) |
| 237 | } |
| 238 | |
| 239 | masterURL := ts.URL + "/timfa/bluegit/-/archive/master/bluegit-master.tar.gz" |
| 240 | anonMaster := getBody(t, &http.Client{}, masterURL) |
| 241 | masterToken := getBearer(t, masterURL, token) |
| 242 | masterTokenBody := read(t, masterToken) |
| 243 | if anonMaster.code != http.StatusNotFound || string(anonMaster.body) != "404 page not found\n" { |
| 244 | t.Fatalf("branch-archief anoniem %d %q", anonMaster.code, anonMaster.body) |
| 245 | } |
| 246 | if masterToken.StatusCode != http.StatusNotFound || masterTokenBody != "404 page not found\n" { |
| 247 | t.Fatalf("branch-archief met token %d %q", masterToken.StatusCode, masterTokenBody) |
| 248 | } |
| 249 | if anonMaster.header.Get("Content-Security-Policy") == "" || masterToken.Header.Get("Content-Security-Policy") == "" { |
| 250 | t.Fatal("404 mist de BlueGit-headers") |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | func tarNames(t *testing.T, gz []byte) []string { |
| 255 | t.Helper() |
| 256 | zr, err := gzip.NewReader(bytes.NewReader(gz)) |
| 257 | if err != nil { |
| 258 | t.Fatal(err) |
| 259 | } |
| 260 | defer zr.Close() |
| 261 | tr := tar.NewReader(zr) |
| 262 | var names []string |
| 263 | for { |
| 264 | hdr, err := tr.Next() |
| 265 | if err == io.EOF { |
| 266 | break |
| 267 | } |
| 268 | if err != nil { |
| 269 | t.Fatal(err) |
| 270 | } |
| 271 | names = append(names, hdr.Name) |
| 272 | } |
| 273 | return names |
| 274 | } |
| 275 | |
| 276 | func sha256Sum(b []byte) []byte { |
| 277 | sum := sha256.Sum256(b) |
| 278 | return sum[:] |
| 279 | } |
| 280 | |
| 281 | func containsString(list []string, want string) bool { |
| 282 | for _, item := range list { |
| 283 | if item == want { |
| 284 | return true |
| 285 | } |
| 286 | } |
| 287 | return false |
| 288 | } |