package server import ( "context" "fmt" "net/http" "net/http/httptest" "path/filepath" "strings" "testing" "gitlab.com/Timfa/bluegit/internal/access" "gitlab.com/Timfa/bluegit/internal/auth" "gitlab.com/Timfa/bluegit/internal/config" "gitlab.com/Timfa/bluegit/internal/store" ) // Anoniem en als niet-lid: publiek lezen mag, privé en groepsprivé geeft 404, // schrijven zonder token geeft 401. Geldt voor de leesroutes die agents gebruiken. func TestAPIAnonymousReadsRespectVisibility(t *testing.T) { storage := t.TempDir() st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = st.Close() }) ctx := context.Background() ada, _ := st.UpsertUser(ctx, 1, "ada", "", false) eve, _ := st.UpsertUser(ctx, 2, "eve", "", false) tok := func(u store.User) string { plain, _, err := st.IssueToken(ctx, u.ID, store.TokenRequest{Name: "t", Scopes: "api"}) if err != nil { t.Fatal(err) } return plain } adaTok, eveTok := tok(ada), tok(eve) team, err := st.CreateGroup(ctx, ada.ID, "Team", "team", "", access.RoleDeveloper) if err != nil { t.Fatal(err) } type fixture struct { path string pipe, job int64 mr, issues int64 } mk := func(kind string, owner int64, ownerPath, name, vis string) fixture { repo, err := st.CreateRepo(ctx, kind, owner, name, "", "master", vis) if err != nil { t.Fatal(err) } sha := strings.Repeat("a", 40) pid, err := st.CreatePipeline(ctx, repo.ID, sha, "refs/heads/master", "push", "ada", "c", []store.NewJob{{Name: "t", Stage: "test", Script: "true"}}) if err != nil { t.Fatal(err) } jobs, _ := st.JobsByPipeline(ctx, pid) _ = st.AppendJobLog(ctx, jobs[0].ID, "geheim-log-"+name+"\n") mr, err := st.CreateMergeRequest(ctx, repo.ID, ada.ID, "mr "+name, "", "topic", "master") if err != nil { t.Fatal(err) } if _, err := st.CreateIssue(ctx, repo.ID, ada.ID, store.NewIssue{Title: "issue " + name}); err != nil { t.Fatal(err) } return fixture{path: ownerPath + "/" + name, pipe: pid, job: jobs[0].ID, mr: mr.Number} } pub := mk("user", ada.ID, "ada", "pub", "public") priv := mk("user", ada.ID, "ada", "priv", "private") grp := mk("group", team.ID, "team", "intern", "private") cfg := config.Config{ BaseURL: "http://127.0.0.1:9", StoragePath: storage, Auth: config.Auth{Mode: "dev", DevUser: "ada", DevAuthID: 1}, Git: config.Git{Binary: "git", DefaultBranch: "master"}, CI: config.CI{Enabled: true}, } srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "ada", AuthID: 1}, Git: "git", HookBin: "/bin/true", StateDir: t.TempDir()} ts := httptest.NewServer(srv.Handler()) t.Cleanup(ts.Close) call := func(method, path, token string) (int, string) { t.Helper() req, _ := http.NewRequest(method, ts.URL+path, strings.NewReader("{}")) req.Header.Set("Content-Type", "application/json") if token != "" { req.Header.Set("Authorization", "Bearer "+token) } res, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) } return res.StatusCode, read(t, res) } reads := func(f fixture) []string { p := "/api/v1/projects/" + f.path return []string{ p, p + "/pipelines", p + "/pipelines?ref=master", p + "/pipelines?sha=aaaaaaa", fmt.Sprintf("%s/pipelines/%d", p, f.pipe), fmt.Sprintf("%s/pipelines/%d/jobs", p, f.pipe), fmt.Sprintf("%s/pipelines/%d/jobs/%d", p, f.pipe, f.job), fmt.Sprintf("%s/pipelines/%d/jobs/%d/log", p, f.pipe, f.job), p + "/merge_requests", p + "/merge_requests?source_branch=topic", fmt.Sprintf("%s/merge_requests/%d", p, f.mr), fmt.Sprintf("%s/merge_requests/%d/notes", p, f.mr), p + "/releases", "/api/v1/repos/" + f.path + "/issues", "/api/v1/repos/" + f.path + "/issues/1", } } pubRepo, _ := st.RepoByPath(ctx, "ada", "pub") if _, err := st.CreateIssue(ctx, pubRepo.ID, ada.ID, store.NewIssue{Title: "vertrouwelijk-x", Confidential: true}); err != nil { t.Fatal(err) } for _, token := range []string{"", eveTok} { if code, body := call(http.MethodGet, "/api/v1/repos/ada/pub/issues/2", token); code == 200 || strings.Contains(body, "vertrouwelijk-x") { t.Fatalf("vertrouwelijk issue zichtbaar: %d %s", code, body) } if _, body := call(http.MethodGet, "/api/v1/repos/ada/pub/issues", token); strings.Contains(body, "vertrouwelijk-x") { t.Fatalf("vertrouwelijk issue in lijst: %s", body) } } for _, path := range reads(pub) { if code, body := call(http.MethodGet, path, ""); code != 200 { t.Fatalf("anoniem publiek %s: %d %s", path, code, body) } } for _, f := range []fixture{priv, grp} { for _, path := range reads(f) { for who, token := range map[string]string{"anoniem": "", "niet-lid": eveTok} { code, body := call(http.MethodGet, path, token) if code != 404 || strings.Contains(body, "geheim-log") || strings.Contains(body, "mr ") || strings.Contains(body, "issue ") { t.Fatalf("%s ziet privé %s: %d %s", who, path, code, body) } } if code, body := call(http.MethodGet, path, adaTok); code != 200 { t.Fatalf("eigenaar %s: %d %s", path, code, body) } } } writes := func(f fixture) []string { p := "/api/v1/projects/" + f.path return []string{ p + "/merge_requests", p + "/releases", fmt.Sprintf("%s/merge_requests/%d/notes", p, f.mr), fmt.Sprintf("%s/pipelines/%d/retry", p, f.pipe), "/api/v1/repos/" + f.path + "/issues", } } for _, f := range []fixture{pub, priv, grp} { for _, path := range writes(f) { if code, body := call(http.MethodPost, path, ""); code != 401 { t.Fatalf("anoniem schrijven %s: %d %s", path, code, body) } } } for _, f := range []fixture{priv, grp} { for _, path := range writes(f) { if code, body := call(http.MethodPost, path, eveTok); code != 404 { t.Fatalf("niet-lid schrijft privé %s: %d %s", path, code, body) } } } if code, body := call(http.MethodPost, fmt.Sprintf("/api/v1/projects/%s/pipelines/%d/retry", pub.path, pub.pipe), eveTok); code != 403 { t.Fatalf("niet-lid retry publiek: %d %s", code, body) } }