e2e/web_test.go
236 lines · 8249 bytes
1package e2e
2
3import (
4 "compress/gzip"
5 "fmt"
6 "io"
7 "net/http"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "golang.org/x/crypto/ssh"
14
15 "gitbay.org/gitbay/internal/sig"
16)
17
18func (i *instance) get(t *testing.T, path string) (int, string) {
19 t.Helper()
20 resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d%s", i.httpPort, path))
21 if err != nil {
22 t.Fatal(err)
23 }
24 defer resp.Body.Close()
25 body, _ := io.ReadAll(resp.Body)
26 return resp.StatusCode, string(body)
27}
28
29func TestWebUI(t *testing.T) {
30 inst := startInstance(t)
31
32 aliceKey := inst.newKey(t, "alice")
33 inst.admin(t, "admin", "user", "create", "alice",
34 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
35
36 // Public repo with real content: a README, a source file, a tag, and
37 // one SSHSIG-signed commit for the badge check.
38 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/site"); code != 0 {
39 t.Fatalf("repo create: %s", errOut)
40 }
41 work := t.TempDir()
42 env := inst.gitEnv(aliceKey)
43 mustGit(t, work, env, "clone", inst.sshURL("alice/site"), "w")
44 dir := filepath.Join(work, "w")
45 os.WriteFile(filepath.Join(dir, "README.md"), []byte("# hello site\n\nsome *markdown*\n"), 0o644)
46 os.MkdirAll(filepath.Join(dir, "src"), 0o755)
47 os.WriteFile(filepath.Join(dir, "src", "main.go"), []byte("package main\n\nfunc main() {}\n"), 0o644)
48 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
49 mustGit(t, dir, env, "add", ".")
50 mustGit(t, dir, env, "commit", "-q", "-m", "first commit")
51 mustGit(t, dir, env, "tag", "v1.0")
52 mustGit(t, dir, env, "push", "-q", "origin", "main", "v1.0")
53
54 // A signed commit on top, built with the M4 fixture helpers.
55 sshRaw, _ := os.ReadFile(aliceKey)
56 signer, err := ssh.ParsePrivateKey(sshRaw)
57 if err != nil {
58 t.Fatal(err)
59 }
60 head := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
61 buildSignedCommitOn(t, dir, env, head, "signed tip", "alice@example.test", signer)
62 mustGit(t, dir, env, "push", "-q", "origin", "main")
63
64 // Private repo must be invisible everywhere.
65 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/secret", "--private"); code != 0 {
66 t.Fatal("create private failed")
67 }
68
69 // Explore lists the public repo, not the private one; the anonymous
70 // homepage is a landing page pointing there.
71 status, body := inst.get(t, "/")
72 if status != 200 || !strings.Contains(body, `href="/explore"`) {
73 t.Fatalf("landing: %d\n%s", status, body)
74 }
75 status, body = inst.get(t, "/explore")
76 if status != 200 || !strings.Contains(body, "alice/site") {
77 t.Fatalf("explore: %d\n%s", status, body)
78 }
79 if strings.Contains(body, "secret") {
80 t.Fatal("explore leaks private repo")
81 }
82
83 // Repo home: tree entries plus rendered README.
84 status, body = inst.get(t, "/alice/site")
85 if status != 200 || !strings.Contains(body, "src/") || !strings.Contains(body, "README.md") {
86 t.Fatalf("repo home: %d\n%s", status, body)
87 }
88 if !strings.Contains(body, "<h1>hello site</h1>") || !strings.Contains(body, "<em>markdown</em>") {
89 t.Fatalf("README not rendered:\n%s", body)
90 }
91 for _, tab := range []string{">issues<", ">merge requests<"} {
92 if !strings.Contains(body, tab) {
93 t.Fatalf("repo header missing %s tab", tab)
94 }
95 }
96
97 // Subdirectory tree and blob with highlighting.
98 status, body = inst.get(t, "/alice/site/tree/main/src")
99 if status != 200 || !strings.Contains(body, "main.go") {
100 t.Fatalf("tree src: %d", status)
101 }
102 status, body = inst.get(t, "/alice/site/blob/main/src/main.go")
103 if status != 200 || !strings.Contains(body, "package") {
104 t.Fatalf("blob: %d", status)
105 }
106
107 // Raw serves exact bytes with nosniff.
108 resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/raw/main/src/main.go", inst.httpPort))
109 if err != nil {
110 t.Fatal(err)
111 }
112 raw, _ := io.ReadAll(resp.Body)
113 resp.Body.Close()
114 if string(raw) != "package main\n\nfunc main() {}\n" {
115 t.Fatalf("raw bytes: %q", raw)
116 }
117 if resp.Header.Get("X-Content-Type-Options") != "nosniff" {
118 t.Fatal("raw missing nosniff")
119 }
120
121 // Log: both commits, with badges matching the M4 states exactly.
122 status, body = inst.get(t, "/alice/site/log")
123 if status != 200 {
124 t.Fatalf("log: %d", status)
125 }
126 if !strings.Contains(body, "badge-verified") || !strings.Contains(body, "signed tip") {
127 t.Fatalf("log missing verified badge:\n%s", body)
128 }
129 if !strings.Contains(body, "badge-unsigned") || !strings.Contains(body, "first commit") {
130 t.Fatalf("log missing unsigned badge:\n%s", body)
131 }
132
133 // Commit page for the signed tip.
134 tip := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
135 status, body = inst.get(t, "/alice/site/commit/"+tip)
136 if status != 200 || !strings.Contains(body, "badge-verified") || !strings.Contains(body, "alice") {
137 t.Fatalf("commit page: %d\n%s", status, body)
138 }
139
140 // Refs page shows branch and tag.
141 status, body = inst.get(t, "/alice/site/refs")
142 if status != 200 || !strings.Contains(body, "main") || !strings.Contains(body, "v1.0") {
143 t.Fatalf("refs: %d", status)
144 }
145
146 // Archive downloads a valid gzip.
147 resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/archive/main.tar.gz", inst.httpPort))
148 if err != nil {
149 t.Fatal(err)
150 }
151 gz, err := gzip.NewReader(resp.Body)
152 if err != nil {
153 t.Fatalf("archive not gzip: %v", err)
154 }
155 tarBytes, _ := io.ReadAll(gz)
156 resp.Body.Close()
157 if !strings.Contains(string(tarBytes), "README.md") {
158 t.Fatal("archive missing content")
159 }
160
161 // README formats: org-mode renders, HTML renders sanitized, unknown
162 // extensions fall back to plaintext, and richer formats win conflicts.
163 readmeRepo := func(name, file, content string) {
164 t.Helper()
165 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/"+name); code != 0 {
166 t.Fatalf("repo create %s failed", name)
167 }
168 w := t.TempDir()
169 mustGit(t, w, env, "clone", inst.sshURL("alice/"+name), "r")
170 d := filepath.Join(w, "r")
171 os.WriteFile(filepath.Join(d, file), []byte(content), 0o644)
172 mustGit(t, d, env, "checkout", "-q", "-b", "main")
173 mustGit(t, d, env, "add", ".")
174 mustGit(t, d, env, "commit", "-q", "-m", "readme")
175 mustGit(t, d, env, "push", "-q", "origin", "main")
176 }
177
178 readmeRepo("orgdoc", "README.org", "* Heading\n\nSome /emphasis/ here.\n")
179 status, body = inst.get(t, "/alice/orgdoc")
180 if status != 200 || !strings.Contains(body, "headline-1") || !strings.Contains(body, "<em>emphasis</em>") {
181 t.Fatalf("org README not rendered:\n%s", body)
182 }
183
184 readmeRepo("htmldoc", "README.html", "<p id=\"ok\">fine</p><script>alert(1)</script>")
185 status, body = inst.get(t, "/alice/htmldoc")
186 if status != 200 || !strings.Contains(body, "fine</p>") {
187 t.Fatalf("html README not rendered:\n%s", body)
188 }
189 if strings.Contains(body, "<script>alert") {
190 t.Fatal("repo HTML script survived sanitization")
191 }
192
193 readmeRepo("txtdoc", "README.txt", "plain <text> & stuff\n")
194 status, body = inst.get(t, "/alice/txtdoc")
195 if status != 200 || !strings.Contains(body, "plain <text> & stuff") {
196 t.Fatalf("txt README not escaped-plaintext:\n%s", body)
197 }
198
199 // Owner page: lists visible repos only; unknown owners 404.
200 status, body = inst.get(t, "/alice")
201 if status != 200 || !strings.Contains(body, ">site<") || !strings.Contains(body, "user") {
202 t.Fatalf("owner page: %d", status)
203 }
204 if strings.Contains(body, "secret") {
205 t.Fatal("owner page leaks private repo")
206 }
207 if status, _ := inst.get(t, "/nobody"); status != 404 {
208 t.Fatalf("unknown owner: %d, want 404", status)
209 }
210
211 // Private repo pages: 404, indistinguishable from nonexistent.
212 for _, p := range []string{"/alice/secret", "/alice/secret/log", "/alice/nothere"} {
213 if status, _ := inst.get(t, p); status != 404 {
214 t.Errorf("GET %s = %d, want 404", p, status)
215 }
216 }
217}
218
219// buildSignedCommitOn adds one SSHSIG-signed commit on top of parent,
220// reusing the M4 fixture machinery.
221func buildSignedCommitOn(t *testing.T, dir string, env []string, parent, subject, email string, signer ssh.Signer) {
222 t.Helper()
223 tree := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", parent+"^{tree}"))
224 specs := []commitSpec{{
225 authorEmail: email,
226 subject: subject,
227 sign: func(p []byte) string {
228 s, err := sig.MarshalSSHSig(signer, p)
229 if err != nil {
230 t.Fatal(err)
231 }
232 return string(s)
233 },
234 }}
235 buildChain(t, dir, env, tree, parent, specs)
236}