BlueGit

timfa bluegit

Files

bluegit / internal / server / raw_test.go

raw_test.go

raw_test.go 3.9 KiB Download Raw
1package server
2
3import (
4 "io"
5 "net/http"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "gitlab.com/Timfa/bluegit/internal/gitrepo"
13)
14
15func TestRawAndFileActions(t *testing.T) {
16 e := newCIEnv(t)
17 bare := gitrepo.Dir(e.srv.StoragePath(), e.repo.OwnerPath, e.repo.Name)
18 src := t.TempDir()
19 git := func(args ...string) {
20 cmd := exec.Command("git", args...)
21 cmd.Dir = src
22 cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@x", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@x")
23 if out, err := cmd.CombinedOutput(); err != nil {
24 t.Fatalf("git %v: %v %s", args, err, out)
25 }
26 }
27 git("clone", "-q", bare, ".")
28 _ = os.MkdirAll(filepath.Join(src, "web"), 0o755)
29 _ = os.WriteFile(filepath.Join(src, "web", "icon.svg"), []byte(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 8 8"><script>alert(1)</script><rect width="8" height="8"/></svg>`), 0o644)
30 _ = os.WriteFile(filepath.Join(src, "page.html"), []byte("<script>alert(1)</script>"), 0o644)
31 _ = os.WriteFile(filepath.Join(src, "blob.bin"), []byte{0, 1, 2, 3}, 0o644)
32 git("add", ".")
33 git("commit", "-qm", "files")
34 git("push", "-q", "origin", "master")
35 c := loginClient(t, e.ts)
36 anon, err := http.Get(e.ts.URL + "/tim/app/-/raw/page.html?rev=master")
37 if err != nil {
38 t.Fatal(err)
39 }
40 anon.Body.Close()
41 if anon.StatusCode == 200 {
42 t.Fatal("private repo raw must not be anonymous")
43 }
44 get := func(p string) (*http.Response, string) {
45 res, err := c.Get(e.ts.URL + p)
46 if err != nil {
47 t.Fatal(err)
48 }
49 b, _ := io.ReadAll(res.Body)
50 res.Body.Close()
51 return res, string(b)
52 }
53 // HTML uit de repo komt als platte tekst, nooit als HTML.
54 res, body := get("/tim/app/-/raw/page.html?rev=master")
55 if res.StatusCode != 200 || !strings.HasPrefix(res.Header.Get("Content-Type"), "text/plain") || body != "<script>alert(1)</script>" || res.Header.Get("X-Content-Type-Options") != "nosniff" {
56 t.Fatalf("raw html: %d %q %q", res.StatusCode, res.Header.Get("Content-Type"), body)
57 }
58 if !strings.Contains(res.Header.Get("Content-Security-Policy"), "sandbox") {
59 t.Fatal("raw needs sandbox CSP")
60 }
61 res, _ = get("/tim/app/-/raw/web/icon.svg?rev=master")
62 if !strings.HasPrefix(res.Header.Get("Content-Type"), "text/plain") {
63 t.Fatalf("svg raw as text: %q", res.Header.Get("Content-Type"))
64 }
65 res, _ = get("/tim/app/-/raw/web/icon.svg?rev=master&as=img")
66 if res.Header.Get("Content-Type") != "image/svg+xml" {
67 t.Fatalf("svg img: %q", res.Header.Get("Content-Type"))
68 }
69 res, _ = get("/tim/app/-/raw/web/icon.svg?rev=master&download=1")
70 if cd := res.Header.Get("Content-Disposition"); !strings.Contains(cd, "attachment") || !strings.Contains(cd, "icon.svg") {
71 t.Fatalf("download: %q", cd)
72 }
73 res, _ = get("/tim/app/-/raw/blob.bin?rev=master")
74 if res.Header.Get("X-BlueGit-Binary") != "1" || res.Header.Get("Content-Type") != "application/octet-stream" {
75 t.Fatalf("binary: %v", res.Header)
76 }
77 if res, _ = get("/tim/app/-/raw/nope.txt?rev=master"); res.StatusCode != 404 {
78 t.Fatalf("missing: %d", res.StatusCode)
79 }
80 if res, _ = get("/tim/app/-/raw/../etc/passwd"); res.StatusCode == 200 {
81 t.Fatal("path escape")
82 }
83 // Bestandsweergave: knoppen altijd, SVG-voorbeeld alleen bij .svg (via <img>).
84 _, page := get("/tim/app/-/blob/web/icon.svg?rev=master")
85 for _, want := range []string{"data-copy-file=", "download=\"icon.svg\"", "target=\"_blank\"", "class=\"svgpreview\"", "as=img"} {
86 if !strings.Contains(page, want) {
87 t.Fatalf("svg page lacks %s", want)
88 }
89 }
90 if strings.Contains(page, "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 8 8\"><script>") {
91 t.Fatal("svg must never be inlined")
92 }
93 _, page = get("/tim/app/-/blob/README.md?rev=master")
94 if strings.Contains(page, "class=\"svgpreview\"") || !strings.Contains(page, "data-copy-file=") {
95 t.Fatal("readme: buttons yes, preview no")
96 }
97 _, page = get("/tim/app/-/blob/blob.bin?rev=master")
98 if !strings.Contains(page, "disabled aria-disabled=\"true\"") {
99 t.Fatal("binary copy must be disabled")
100 }
101}