e2e/web_test.go

8e6fc2062ca26f7a848bdf6ad6345fd345351231
gitbay/e2e/web_test.go history · blame · raw

240 lines · 8561 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	// Both clone URLs: SSH for anyone with a key, HTTPS for reading.
 89	if !strings.Contains(body, "git clone ssh://git@gitbay.test:") || !strings.Contains(body, "/alice/site.git</code> · <code>git clone https://gitbay.test/alice/site.git</code>") {
 90		t.Fatalf("clone URLs missing:\n%s", body)
 91	}
 92	if !strings.Contains(body, "<h2 id=\"hello-site\">hello site</h2>") || !strings.Contains(body, "<em>markdown</em>") {
 93		t.Fatalf("README not rendered:\n%s", body)
 94	}
 95	for _, tab := range []string{">Issues<", ">Merge requests<"} {
 96		if !strings.Contains(body, tab) {
 97			t.Fatalf("repo header missing %s tab", tab)
 98		}
 99	}
100
101	// Subdirectory tree and blob with highlighting.
102	status, body = inst.get(t, "/alice/site/tree/main/src")
103	if status != 200 || !strings.Contains(body, "main.go") {
104		t.Fatalf("tree src: %d", status)
105	}
106	status, body = inst.get(t, "/alice/site/blob/main/src/main.go")
107	if status != 200 || !strings.Contains(body, "package") {
108		t.Fatalf("blob: %d", status)
109	}
110
111	// Raw serves exact bytes with nosniff.
112	resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/raw/main/src/main.go", inst.httpPort))
113	if err != nil {
114		t.Fatal(err)
115	}
116	raw, _ := io.ReadAll(resp.Body)
117	resp.Body.Close()
118	if string(raw) != "package main\n\nfunc main() {}\n" {
119		t.Fatalf("raw bytes: %q", raw)
120	}
121	if resp.Header.Get("X-Content-Type-Options") != "nosniff" {
122		t.Fatal("raw missing nosniff")
123	}
124
125	// Log: both commits, with badges matching the M4 states exactly.
126	status, body = inst.get(t, "/alice/site/log")
127	if status != 200 {
128		t.Fatalf("log: %d", status)
129	}
130	if !strings.Contains(body, "badge-verified") || !strings.Contains(body, "signed tip") {
131		t.Fatalf("log missing verified badge:\n%s", body)
132	}
133	if !strings.Contains(body, "badge-unsigned") || !strings.Contains(body, "first commit") {
134		t.Fatalf("log missing unsigned badge:\n%s", body)
135	}
136
137	// Commit page for the signed tip.
138	tip := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
139	status, body = inst.get(t, "/alice/site/commit/"+tip)
140	if status != 200 || !strings.Contains(body, "badge-verified") || !strings.Contains(body, "alice") {
141		t.Fatalf("commit page: %d\n%s", status, body)
142	}
143
144	// Refs page shows branch and tag.
145	status, body = inst.get(t, "/alice/site/refs")
146	if status != 200 || !strings.Contains(body, "main") || !strings.Contains(body, "v1.0") {
147		t.Fatalf("refs: %d", status)
148	}
149
150	// Archive downloads a valid gzip.
151	resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/archive/main.tar.gz", inst.httpPort))
152	if err != nil {
153		t.Fatal(err)
154	}
155	gz, err := gzip.NewReader(resp.Body)
156	if err != nil {
157		t.Fatalf("archive not gzip: %v", err)
158	}
159	tarBytes, _ := io.ReadAll(gz)
160	resp.Body.Close()
161	if !strings.Contains(string(tarBytes), "README.md") {
162		t.Fatal("archive missing content")
163	}
164
165	// README formats: org-mode renders, HTML renders sanitized, unknown
166	// extensions fall back to plaintext, and richer formats win conflicts.
167	readmeRepo := func(name, file, content string) {
168		t.Helper()
169		if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/"+name); code != 0 {
170			t.Fatalf("repo create %s failed", name)
171		}
172		w := t.TempDir()
173		mustGit(t, w, env, "clone", inst.sshURL("alice/"+name), "r")
174		d := filepath.Join(w, "r")
175		os.WriteFile(filepath.Join(d, file), []byte(content), 0o644)
176		mustGit(t, d, env, "checkout", "-q", "-b", "main")
177		mustGit(t, d, env, "add", ".")
178		mustGit(t, d, env, "commit", "-q", "-m", "readme")
179		mustGit(t, d, env, "push", "-q", "origin", "main")
180	}
181
182	readmeRepo("orgdoc", "README.org", "* Heading\n\nSome /emphasis/ here.\n")
183	status, body = inst.get(t, "/alice/orgdoc")
184	if status != 200 || !strings.Contains(body, "headline-1") || !strings.Contains(body, "<em>emphasis</em>") {
185		t.Fatalf("org README not rendered:\n%s", body)
186	}
187
188	readmeRepo("htmldoc", "README.html", "<p id=\"ok\">fine</p><script>alert(1)</script>")
189	status, body = inst.get(t, "/alice/htmldoc")
190	if status != 200 || !strings.Contains(body, "fine</p>") {
191		t.Fatalf("html README not rendered:\n%s", body)
192	}
193	if strings.Contains(body, "<script>alert") {
194		t.Fatal("repo HTML script survived sanitization")
195	}
196
197	readmeRepo("txtdoc", "README.txt", "plain <text> & stuff\n")
198	status, body = inst.get(t, "/alice/txtdoc")
199	if status != 200 || !strings.Contains(body, "plain &lt;text&gt; &amp; stuff") {
200		t.Fatalf("txt README not escaped-plaintext:\n%s", body)
201	}
202
203	// Owner page: lists visible repos only; unknown owners 404.
204	status, body = inst.get(t, "/alice")
205	if status != 200 || !strings.Contains(body, ">site<") || !strings.Contains(body, "user") {
206		t.Fatalf("owner page: %d", status)
207	}
208	if strings.Contains(body, "secret") {
209		t.Fatal("owner page leaks private repo")
210	}
211	if status, _ := inst.get(t, "/nobody"); status != 404 {
212		t.Fatalf("unknown owner: %d, want 404", status)
213	}
214
215	// Private repo pages: 404, indistinguishable from nonexistent.
216	for _, p := range []string{"/alice/secret", "/alice/secret/log", "/alice/nothere"} {
217		if status, _ := inst.get(t, p); status != 404 {
218			t.Errorf("GET %s = %d, want 404", p, status)
219		}
220	}
221}
222
223// buildSignedCommitOn adds one SSHSIG-signed commit on top of parent,
224// reusing the M4 fixture machinery.
225func buildSignedCommitOn(t *testing.T, dir string, env []string, parent, subject, email string, signer ssh.Signer) {
226	t.Helper()
227	tree := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", parent+"^{tree}"))
228	specs := []commitSpec{{
229		authorEmail: email,
230		subject:     subject,
231		sign: func(p []byte) string {
232			s, err := sig.MarshalSSHSig(signer, p)
233			if err != nil {
234				t.Fatal(err)
235			}
236			return string(s)
237		},
238	}}
239	buildChain(t, dir, env, tree, parent, specs)
240}