BlueGit

timfa bluegit

Files

bluegit / internal / server / raw_test.go

raw_test.go

raw_test.go 5.3 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 png1 := "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xf8\xcf\xc0\xf0\x1f\x00\x05\x00\x01\xff\x89\x99=\x1d\x00\x00\x00\x00IEND\xaeB`\x82"
33 _ = os.WriteFile(filepath.Join(src, "favicon.png"), []byte(png1), 0o644)
34 _ = os.WriteFile(filepath.Join(src, "fake.png"), []byte("<html>\x00geen png</html>"), 0o644)
35 git("add", ".")
36 git("commit", "-qm", "files")
37 git("push", "-q", "origin", "master")
38 c := loginClient(t, e.ts)
39 anon, err := http.Get(e.ts.URL + "/tim/app/-/raw/page.html?rev=master")
40 if err != nil {
41 t.Fatal(err)
42 }
43 anon.Body.Close()
44 if anon.StatusCode == 200 {
45 t.Fatal("private repo raw must not be anonymous")
46 }
47 get := func(p string) (*http.Response, string) {
48 res, err := c.Get(e.ts.URL + p)
49 if err != nil {
50 t.Fatal(err)
51 }
52 b, _ := io.ReadAll(res.Body)
53 res.Body.Close()
54 return res, string(b)
55 }
56 // HTML uit de repo komt als platte tekst, nooit als HTML.
57 res, body := get("/tim/app/-/raw/page.html?rev=master")
58 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" {
59 t.Fatalf("raw html: %d %q %q", res.StatusCode, res.Header.Get("Content-Type"), body)
60 }
61 if !strings.Contains(res.Header.Get("Content-Security-Policy"), "sandbox") {
62 t.Fatal("raw needs sandbox CSP")
63 }
64 res, _ = get("/tim/app/-/raw/web/icon.svg?rev=master")
65 if !strings.HasPrefix(res.Header.Get("Content-Type"), "text/plain") {
66 t.Fatalf("svg raw as text: %q", res.Header.Get("Content-Type"))
67 }
68 res, _ = get("/tim/app/-/raw/web/icon.svg?rev=master&as=img")
69 if res.Header.Get("Content-Type") != "image/svg+xml" {
70 t.Fatalf("svg img: %q", res.Header.Get("Content-Type"))
71 }
72 res, _ = get("/tim/app/-/raw/web/icon.svg?rev=master&download=1")
73 if cd := res.Header.Get("Content-Disposition"); !strings.Contains(cd, "attachment") || !strings.Contains(cd, "icon.svg") {
74 t.Fatalf("download: %q", cd)
75 }
76 res, _ = get("/tim/app/-/raw/blob.bin?rev=master")
77 if res.Header.Get("X-BlueGit-Binary") != "1" || res.Header.Get("Content-Type") != "application/octet-stream" {
78 t.Fatalf("binary: %v", res.Header)
79 }
80 if res, _ = get("/tim/app/-/raw/nope.txt?rev=master"); res.StatusCode != 404 {
81 t.Fatalf("missing: %d", res.StatusCode)
82 }
83 if res, _ = get("/tim/app/-/raw/../etc/passwd"); res.StatusCode == 200 {
84 t.Fatal("path escape")
85 }
86 // Bestandsweergave: knoppen altijd, SVG-voorbeeld alleen bij .svg (via <img>).
87 _, page := get("/tim/app/-/blob/web/icon.svg?rev=master")
88 for _, want := range []string{"data-copy-file=", "download=\"icon.svg\"", "target=\"_blank\"", "class=\"svgpreview\"", "as=img"} {
89 if !strings.Contains(page, want) {
90 t.Fatalf("svg page lacks %s", want)
91 }
92 }
93 if strings.Contains(page, "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 8 8\"><script>") {
94 t.Fatal("svg must never be inlined")
95 }
96 _, page = get("/tim/app/-/blob/README.md?rev=master")
97 if strings.Contains(page, "class=\"svgpreview\"") || !strings.Contains(page, "data-copy-file=") {
98 t.Fatal("readme: buttons yes, preview no")
99 }
100 _, page = get("/tim/app/-/blob/blob.bin?rev=master")
101 if !strings.Contains(page, "disabled aria-disabled=\"true\"") {
102 t.Fatal("binary copy must be disabled")
103 }
104 // Rasterafbeeldingen: voorbeeld alleen bij kloppende magic bytes (t699u).
105 res, body = get("/tim/app/-/blob/favicon.png?rev=master")
106 if !strings.Contains(body, `<figure class="imgpreview"><img src="/tim/app/-/raw/favicon.png?rev=master" alt="favicon.png" data-img-preview>`) || strings.Contains(body, "<p>Binary file.</p>") {
107 t.Fatalf("png preview ontbreekt: %d", res.StatusCode)
108 }
109 if res, _ = get("/tim/app/-/raw/favicon.png?rev=master"); res.Header.Get("Content-Type") != "image/png" || res.Header.Get("X-Content-Type-Options") != "nosniff" {
110 t.Fatalf("png raw: %q", res.Header.Get("Content-Type"))
111 }
112 for _, p := range []string{"fake.png", "blob.bin"} {
113 _, body = get("/tim/app/-/blob/" + p + "?rev=master")
114 if strings.Contains(body, "data-img-preview") || !strings.Contains(body, "<p>Binary file.</p>") {
115 t.Fatalf("%s hoort geen preview te krijgen", p)
116 }
117 }
118 if res, _ = get("/tim/app/-/raw/fake.png?rev=master"); strings.HasPrefix(res.Header.Get("Content-Type"), "image/") {
119 t.Fatalf("fake png als image: %q", res.Header.Get("Content-Type"))
120 }
121}