| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/cookiejar" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 16 | "gitlab.com/Timfa/bluegit/internal/config" |
| 17 | "gitlab.com/Timfa/bluegit/internal/store" |
| 18 | ) |
| 19 | |
| 20 | func TestWebhookSecretChoices(t *testing.T) { |
| 21 | if _, err := exec.LookPath("git"); err != nil { |
| 22 | t.Skip("git ontbreekt") |
| 23 | } |
| 24 | storage := t.TempDir() |
| 25 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | t.Cleanup(func() { _ = st.Close() }) |
| 30 | cfg := config.Config{ |
| 31 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 32 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"}, |
| 33 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 34 | } |
| 35 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"} |
| 36 | ts := httptest.NewServer(srv.Handler()) |
| 37 | t.Cleanup(ts.Close) |
| 38 | |
| 39 | jar, _ := cookiejar.New(nil) |
| 40 | client := &http.Client{Jar: jar} |
| 41 | res, err := client.Get(ts.URL + "/login") |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | page := read(t, res) |
| 46 | res, err = client.PostForm(ts.URL+"/new/repo", url.Values{ |
| 47 | "csrf": {csrfOf(t, page)}, "name": {"hooks"}, "visibility": {"private"}, |
| 48 | }) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | _ = read(t, res) |
| 53 | |
| 54 | ctx := context.Background() |
| 55 | tim, err := st.UserByUsername(ctx, "tim") |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | repo, err := st.RepoByPath(ctx, "tim", "hooks") |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if err := st.PutSecret(ctx, "repo", repo.ID, "Ada_Webhook", "waarde-niet-tonen", false); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | g, err := st.CreateGroup(ctx, tim.ID, "Team", "team", "", "developer") |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if err := st.PutSecret(ctx, "group", g.ID, "GROUP_TOKEN", "groepswaarde", false); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | grown, err := st.CreateRepo(ctx, "group", g.ID, "app", "", "master", "private") |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | if err := st.PutSecret(ctx, "repo", grown.ID, "Ada_Webhook", "repo-wint", true); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | |
| 81 | res, err = client.Get(ts.URL + "/tim/hooks/-/settings?tab=webhooks") |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | form := read(t, res) |
| 86 | for _, needle := range []string{ |
| 87 | `name="token"`, `data-secret-button`, `data-secret-filter`, ">None<", |
| 88 | "Ada_Webhook", "Repository tim/hooks", |
| 89 | } { |
| 90 | if !strings.Contains(form, needle) { |
| 91 | t.Fatalf("formulier mist %q", needle) |
| 92 | } |
| 93 | } |
| 94 | if strings.Contains(form, "waarde-niet-tonen") { |
| 95 | t.Fatal("geheime waarde op de webhookpagina") |
| 96 | } |
| 97 | res, err = client.PostForm(ts.URL+"/tim/hooks/-/secrets", url.Values{ |
| 98 | "csrf": {csrfOf(t, form)}, "name": {"Ada Webhook"}, "value": {"x"}, |
| 99 | }) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | bad := read(t, res) |
| 104 | if !strings.Contains(bad, `[A-Za-z_][A-Za-z0-9_]*`) || !strings.Contains(bad, "Ada Webhook") { |
| 105 | t.Fatalf("foutmelding:\n%s", bad) |
| 106 | } |
| 107 | |
| 108 | res, err = client.Get(ts.URL + "/team/app/-/settings?tab=webhooks") |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | groupForm := read(t, res) |
| 113 | if !strings.Contains(groupForm, "GROUP_TOKEN") || !strings.Contains(groupForm, "Group team") || !strings.Contains(groupForm, "Ada_Webhook") { |
| 114 | t.Fatalf("groep en repository:\n%s", groupForm) |
| 115 | } |
| 116 | if strings.Contains(groupForm, "groepswaarde") || strings.Contains(groupForm, "repo-wint") { |
| 117 | t.Fatal("waarde van een overgeërfd geheim op de pagina") |
| 118 | } |
| 119 | |
| 120 | merged, err := srv.hookSecrets(ctx, "repo", grown.ID, true, grown) |
| 121 | if err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | if merged["GROUP_TOKEN"] != "groepswaarde" || merged["Ada_Webhook"] != "repo-wint" { |
| 125 | t.Fatalf("samenvoegen: %#v", merged) |
| 126 | } |
| 127 | open, err := srv.hookSecrets(ctx, "repo", grown.ID, false, grown) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | if _, ok := open["Ada_Webhook"]; ok { |
| 132 | t.Fatal("beschermd repositorygeheim hoort weg te blijven op een gewone branch") |
| 133 | } |
| 134 | if open["GROUP_TOKEN"] != "groepswaarde" { |
| 135 | t.Fatalf("onbeschermd groepgeheim: %#v", open) |
| 136 | } |
| 137 | |
| 138 | token, err := st.CreateToken(ctx, tim.ID, "laptop") |
| 139 | if err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | call := func(method, path, body, auth string) (int, string) { |
| 143 | t.Helper() |
| 144 | req, err := http.NewRequest(method, ts.URL+path, strings.NewReader(body)) |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | if auth != "" { |
| 149 | req.Header.Set("Authorization", "Bearer "+auth) |
| 150 | } |
| 151 | req.Header.Set("Content-Type", "application/json") |
| 152 | res, err := client.Do(req) |
| 153 | if err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | buf := read(t, res) |
| 157 | return res.StatusCode, buf |
| 158 | } |
| 159 | code, body := call(http.MethodGet, "/api/v1/repos/tim/hooks/webhook-secrets", "", "") |
| 160 | if code != http.StatusUnauthorized { |
| 161 | t.Fatalf("zonder token %d %s", code, body) |
| 162 | } |
| 163 | code, body = call(http.MethodGet, "/api/v1/repos/tim/hooks/webhook-secrets", "", token) |
| 164 | if code != http.StatusOK || !strings.Contains(body, "Ada_Webhook") || strings.Contains(body, "waarde-niet-tonen") || strings.Contains(body, `"value"`) { |
| 165 | t.Fatalf("lijst %d %s", code, body) |
| 166 | } |
| 167 | var listed struct { |
| 168 | Secrets []SecretChoice `json:"secrets"` |
| 169 | } |
| 170 | if err := json.Unmarshal([]byte(body), &listed); err != nil { |
| 171 | t.Fatal(err) |
| 172 | } |
| 173 | if len(listed.Secrets) != 1 || listed.Secrets[0].Source != "repo" || listed.Secrets[0].Name != "Ada_Webhook" { |
| 174 | t.Fatalf("keuzes %#v", listed.Secrets) |
| 175 | } |
| 176 | code, body = call(http.MethodGet, "/api/v1/groups/team/webhook-secrets", "", token) |
| 177 | if code != http.StatusOK || !strings.Contains(body, "GROUP_TOKEN") || strings.Contains(body, "groepswaarde") { |
| 178 | t.Fatalf("groepslijst %d %s", code, body) |
| 179 | } |
| 180 | code, body = call(http.MethodGet, "/api/v1/repos/team/app/webhook-secrets", "", token) |
| 181 | if code != http.StatusOK || !strings.Contains(body, `"source":"group"`) || !strings.Contains(body, `"source":"repo"`) { |
| 182 | t.Fatalf("overerving %d %s", code, body) |
| 183 | } |
| 184 | ada, err := st.UpsertUser(ctx, 2, "ada", "", false) |
| 185 | if err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | if err := st.SetMember(ctx, "repo", repo.ID, "ada", "developer"); err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | adaTok, err := st.CreateToken(ctx, ada.ID, "ada") |
| 192 | if err != nil { |
| 193 | t.Fatal(err) |
| 194 | } |
| 195 | code, body = call(http.MethodGet, "/api/v1/repos/tim/hooks/webhook-secrets", "", adaTok) |
| 196 | if code != http.StatusForbidden { |
| 197 | t.Fatalf("ontwikkelaar %d %s", code, body) |
| 198 | } |
| 199 | raw, _ := json.Marshal(map[string]any{ |
| 200 | "name": "tg", "preset": "telegram", "token": "Ada_Webhook", "chat": "100", "events": []string{"push"}, |
| 201 | }) |
| 202 | code, body = call(http.MethodPost, "/api/v1/repos/tim/hooks/webhooks", string(raw), token) |
| 203 | if code != http.StatusCreated { |
| 204 | t.Fatalf("aanmaken %d %s", code, body) |
| 205 | } |
| 206 | hooks, err := st.ListWebhooks(ctx, "repo", repo.ID) |
| 207 | if err != nil { |
| 208 | t.Fatal(err) |
| 209 | } |
| 210 | if len(hooks) != 1 || !strings.Contains(hooks[0].URLTemplate, "{{secret.Ada_Webhook}}") { |
| 211 | t.Fatalf("webhook %#v", hooks) |
| 212 | } |
| 213 | raw, _ = json.Marshal(map[string]any{ |
| 214 | "name": "slecht", "preset": "telegram", "token": "Ada Webhook", "chat": "1", "events": []string{"push"}, |
| 215 | }) |
| 216 | code, body = call(http.MethodPost, "/api/v1/repos/tim/hooks/webhooks", string(raw), token) |
| 217 | if code != http.StatusBadRequest || !strings.Contains(body, "[A-Za-z_][A-Za-z0-9_]*") { |
| 218 | t.Fatalf("api-fout %d %s", code, body) |
| 219 | } |
| 220 | raw, _ = json.Marshal(map[string]any{ |
| 221 | "name": "ping", "preset": "ntfy", "topic": "builds", "events": []string{"push"}, |
| 222 | }) |
| 223 | code, body = call(http.MethodPost, "/api/v1/groups/team/hooks", string(raw), token) |
| 224 | if code != http.StatusCreated { |
| 225 | t.Fatalf("groepshaak %d %s", code, body) |
| 226 | } |
| 227 | } |