Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Blame · server/media_test.go

Line attribution on main · 440 line(s)

3b2bcc30
Keenan Nemetz
13d ago
1 package server_test
2
3 // Media previews: a blob that is a picture is served as one, shown by the source
4 // view, and compared on both sides of a change. The bytes are untrusted, so the
5 // tests also hold the boundary: an HTML blob stays text and an SVG is inert when
6 // it is opened as a document.
7
8 import (
9 "io"
10 "net/http"
11 "net/url"
12 "os"
13 "path/filepath"
14 "strings"
15 "testing"
16 )
17
18 // png stands in for an image. Nothing decodes it on the server, which is the
19 // point: the type comes from the name and the bytes are the browser's problem.
ffbcb4cc
Keenan Nemetz
11d ago
20 // The NUL is the part that matters to git: without one it calls these bytes text,
21 // prints a ---/+++ pair, and never exercises the case a real PNG hits, where the
22 // diff names the file only in its `diff --git` header.
23 const png = "\x89PNG\r\n\x1a\n\x00payload"
3b2bcc30
Keenan Nemetz
13d ago
24
25 func getWithHeaders(t *testing.T, url string) (*http.Response, string) {
26 t.Helper()
27 resp, err := http.Get(url)
28 if err != nil {
29 t.Fatal(err)
30 }
31 defer resp.Body.Close()
32 body, _ := io.ReadAll(resp.Body)
33 return resp, string(body)
34 }
35
36 func TestRawServesImagesAsImages(t *testing.T) {
37 t.Parallel()
38 f := newFixture(t)
39 f.publish(f.repo)
40 work := workRepo(t, map[string]string{
41 "img/shot.png": png,
42 "logo.svg": `<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>`,
43 "page.html": `<script>alert(1)</script>`,
44 "notes.txt": "plain\n",
45 })
46 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
47 t.Fatalf("push: %s", errs)
48 }
49
50 cases := []struct {
51 name string
52 wantType string
53 wantCSP bool
54 body string
55 }{
56 {name: "img/shot.png", wantType: "image/png", body: png},
57 {name: "logo.svg", wantType: "image/svg+xml", wantCSP: true,
58 body: `<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>`},
59 {name: "page.html", wantType: "text/plain; charset=utf-8", body: `<script>alert(1)</script>`},
60 {name: "notes.txt", wantType: "text/plain; charset=utf-8", body: "plain\n"},
61 }
62 for _, tc := range cases {
63 t.Run(tc.name, func(t *testing.T) {
64 resp, body := getWithHeaders(t, f.http.URL+"/"+f.repo+"/raw/main/"+tc.name)
65 if resp.StatusCode != http.StatusOK {
66 t.Fatalf("%s = %d", tc.name, resp.StatusCode)
67 }
68 if got := resp.Header.Get("Content-Type"); got != tc.wantType {
69 t.Fatalf("%s Content-Type = %q, want %q", tc.name, got, tc.wantType)
70 }
71 if got := resp.Header.Get("X-Content-Type-Options"); got != "nosniff" {
72 t.Fatalf("%s nosniff = %q", tc.name, got)
73 }
74 if got := strings.Contains(resp.Header.Get("Content-Security-Policy"), "default-src 'none'"); got != tc.wantCSP {
75 t.Fatalf("%s CSP = %q, want an inert document: %v", tc.name, resp.Header.Get("Content-Security-Policy"), tc.wantCSP)
76 }
77 if body != tc.body {
78 t.Fatalf("%s body = %q, want %q", tc.name, body, tc.body)
79 }
80 })
81 }
82 }
83
84 // TestBlobShowsAPictureInsteadOfABinaryNote: a raster image has no source worth
85 // reading, so the picture is the file. An SVG is text, so it keeps its source
86 // view and previews only when asked, the same way a markdown file does.
87 func TestBlobShowsAPictureInsteadOfABinaryNote(t *testing.T) {
88 t.Parallel()
89 f := newFixture(t)
90 f.publish(f.repo)
91 work := workRepo(t, map[string]string{
92 "img/shot.png": png,
93 // A GIF begins with ASCII, so its bytes are valid UTF-8; it is still a
94 // picture and must not be mistaken for a source file.
95 "img/tiny.gif": "GIF89a-payload",
96 "logo.svg": `<svg xmlns="http://www.w3.org/2000/svg"></svg>`,
97 })
98 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
99 t.Fatalf("push: %s", errs)
100 }
101
102 code, page := f.get("/" + f.repo + "/blob/main/img/tiny.gif")
103 if code != http.StatusOK {
104 t.Fatalf("gif blob = %d", code)
105 }
106 if !strings.Contains(page, `class="media-preview"`) {
107 t.Fatalf("a GIF that is valid UTF-8 was treated as source:\n%s", page)
108 }
109
110 code, page = f.get("/" + f.repo + "/blob/main/img/shot.png")
111 if code != http.StatusOK {
112 t.Fatalf("png blob = %d", code)
113 }
114 if !strings.Contains(page, `class="media-preview"`) ||
115 !strings.Contains(page, `src="/demo/raw/main/img/shot.png"`) {
116 t.Fatalf("png blob does not show the picture:\n%s", page)
117 }
118 if strings.Contains(page, "Binary file") {
119 t.Fatalf("png blob still says it is a binary file:\n%s", page)
120 }
121
122 code, page = f.get("/" + f.repo + "/blob/main/logo.svg")
123 if code != http.StatusOK {
124 t.Fatalf("svg blob = %d", code)
125 }
126 if !strings.Contains(page, `href="/demo/blob/main/logo.svg?preview=1"`) {
127 t.Fatalf("svg source view has no preview toggle:\n%s", page)
128 }
129 if strings.Contains(page, `class="media-preview"`) {
130 t.Fatalf("svg source view rendered the picture by default:\n%s", page)
131 }
132
133 code, page = f.get("/" + f.repo + "/blob/main/logo.svg?preview=1")
134 if code != http.StatusOK {
135 t.Fatalf("svg preview = %d", code)
136 }
137 if !strings.Contains(page, `class="media-preview"`) ||
138 !strings.Contains(page, `src="/demo/raw/main/logo.svg"`) {
139 t.Fatalf("svg preview does not show the picture:\n%s", page)
140 }
141 }
142
143 // TestCommitDiffShowsBothSidesOfAnImage: git calls a changed PNG binary and
144 // prints no hunks, but both blobs still exist at the parent and the commit, so
145 // the diff can show what changed instead of saying "use git show".
146 func TestCommitDiffShowsBothSidesOfAnImage(t *testing.T) {
147 t.Parallel()
148 f := newFixture(t)
149 f.publish(f.repo)
150 work := workRepo(t, map[string]string{"img/shot.png": png + "one"})
151 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
152 t.Fatalf("push: %s", errs)
153 }
154 old := revParse(t, work, "HEAD")
155
156 if err := os.WriteFile(filepath.Join(work, "img/shot.png"), []byte(png+"two"), 0o644); err != nil {
157 t.Fatal(err)
158 }
159 if _, errs, err := runGit(t, work, "commit", "-qam", "change the picture"); err != nil {
160 t.Fatalf("commit: %s", errs)
161 }
162 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
163 t.Fatalf("push: %s", errs)
164 }
165 newCommit := revParse(t, work, "HEAD")
166
167 code, page := f.get("/" + f.repo + "/commit/" + newCommit + "/img/shot.png")
168 if code != http.StatusOK {
169 t.Fatalf("commit page = %d", code)
170 }
171 if !strings.Contains(page, `class="media-diff"`) {
172 t.Fatalf("the diff has no picture panes:\n%s", page)
173 }
174 for _, want := range []string{
175 `src="/demo/raw/` + old + `/img/shot.png"`,
176 `src="/demo/raw/` + newCommit + `/img/shot.png"`,
177 ">before<",
178 ">after<",
179 } {
180 if !strings.Contains(page, want) {
181 t.Fatalf("the diff is missing %s:\n%s", want, page)
182 }
183 }
184 if strings.Contains(page, "Binary file: no line diff to show") {
185 t.Fatalf("the diff still calls the picture a binary file:\n%s", page)
186 }
187 }
188
189 // TestCompareShowsBothSidesOfAnImage: a three-dot compare diffs from the merge
190 // base, so that is the commit the old picture has to be read at.
191 func TestCompareShowsBothSidesOfAnImage(t *testing.T) {
192 t.Parallel()
193 f := newFixture(t)
194 f.publish(f.repo)
195 work := workRepo(t, map[string]string{"img/shot.png": png + "one"})
196 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
197 t.Fatalf("push: %s", errs)
198 }
199 old := revParse(t, work, "HEAD")
200
201 if _, errs, err := runGit(t, work, "checkout", "-qb", "feature"); err != nil {
202 t.Fatalf("checkout: %s", errs)
203 }
204 if err := os.WriteFile(filepath.Join(work, "img/shot.png"), []byte(png+"two"), 0o644); err != nil {
205 t.Fatal(err)
206 }
207 if _, errs, err := runGit(t, work, "commit", "-qam", "change the picture"); err != nil {
208 t.Fatalf("commit: %s", errs)
209 }
210 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "feature:refs/heads/feature"); err != nil {
211 t.Fatalf("push: %s", errs)
212 }
213 head := revParse(t, work, "HEAD")
214
215 code, page := f.get("/" + f.repo + "/compare?base=main&head=feature")
216 if code != http.StatusOK {
217 t.Fatalf("compare = %d", code)
218 }
219 for _, want := range []string{
220 `src="/demo/raw/` + old + `/img/shot.png"`,
221 `src="/demo/raw/` + head + `/img/shot.png"`,
222 } {
223 if !strings.Contains(page, want) {
224 t.Fatalf("the compare is missing %s:\n%s", want, page)
225 }
226 }
227 }
228
229 // TestRequestFilesShowsBothSidesOfAnImage: an open request's range is resolved
230 // to the commits behind it, so the Files tab can show the picture the branch
231 // changes.
232 func TestRequestFilesShowsBothSidesOfAnImage(t *testing.T) {
233 t.Parallel()
234 f := newFixture(t)
235 f.publish(f.repo)
236 base := workRepo(t, map[string]string{"img/shot.png": png + "one"})
237 if _, errs, err := f.git(base, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
238 t.Fatalf("main push: %s", errs)
239 }
240 feature, _ := f.cloneWithChange(t, map[string]string{"img/shot.png": png + "two"}, "change the picture")
241 if _, errs, err := f.git(feature, "push", f.sshURL(f.repo), "HEAD:refs/heads/feature"); err != nil {
242 t.Fatalf("feature push: %s", errs)
243 }
244
245 sess, cookie := f.browserSession(t, f.ownerID)
246 resp, body := f.browserPost(t, "/"+f.repo+"/changes", cookie, url.Values{
247 "csrf": {sess.CSRF}, "title": {"Change the picture"}, "body": {"body\n"},
248 "base": {"main"}, "head": {"feature"},
249 })
250 if resp.StatusCode != http.StatusSeeOther {
251 t.Fatalf("create change: %d\n%s", resp.StatusCode, body)
252 }
253 id := strings.TrimPrefix(resp.Header.Get("Location"), "/"+f.repo+"/changes/")
254
255 resp, page := f.browserGet(t, "/"+f.repo+"/changes/"+id+"/files/img/shot.png", cookie)
256 if resp.StatusCode != http.StatusOK {
257 t.Fatalf("files tab = %d", resp.StatusCode)
258 }
259 if !strings.Contains(page, `class="media-diff"`) {
260 t.Fatalf("the request diff has no picture panes:\n%s", page)
261 }
262 if strings.Count(page, `src="/demo/raw/`) != 2 {
263 t.Fatalf("the request diff does not show both sides:\n%s", page)
264 }
265 for _, want := range []string{">before<", ">after<"} {
266 if !strings.Contains(page, want) {
267 t.Fatalf("the request diff is missing %s:\n%s", want, page)
268 }
269 }
270 }
271
272 func revParse(t *testing.T, dir, rev string) string {
273 t.Helper()
274 out, errs, err := runGit(t, dir, "rev-parse", rev)
275 if err != nil {
276 t.Fatalf("rev-parse %s: %v: %s", rev, err, errs)
277 }
278 return strings.TrimSpace(out)
279 }
280
281 // binaryModel is not valid UTF-8, which is what the server reads to decide that
282 // a model has no source view. Nothing decodes it.
283 const binaryModel = "\xff\x00\x01binary model"
284
285 func TestRawServesModelsWithTheirType(t *testing.T) {
286 t.Parallel()
287 f := newFixture(t)
288 f.publish(f.repo)
289 work := workRepo(t, map[string]string{
290 "parts/part.stl": binaryModel,
291 "parts/part.obj": "# a text model\nv 0 0 0\n",
292 "parts/scene.glb": binaryModel,
293 "parts/part.3mf": binaryModel,
294 })
295 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
296 t.Fatalf("push: %s", errs)
297 }
298
299 cases := []struct{ name, wantType string }{
300 {"parts/part.stl", "model/stl"},
301 {"parts/part.obj", "model/obj"},
302 {"parts/scene.glb", "model/gltf-binary"},
303 {"parts/part.3mf", "model/3mf"},
304 }
305 for _, tc := range cases {
306 resp, _ := getWithHeaders(t, f.http.URL+"/"+f.repo+"/raw/main/"+tc.name)
307 if resp.StatusCode != http.StatusOK {
308 t.Fatalf("%s = %d", tc.name, resp.StatusCode)
309 }
310 if got := resp.Header.Get("Content-Type"); got != tc.wantType {
311 t.Fatalf("%s Content-Type = %q, want %q", tc.name, got, tc.wantType)
312 }
313 if got := resp.Header.Get("X-Content-Type-Options"); got != "nosniff" {
314 t.Fatalf("%s nosniff = %q", tc.name, got)
315 }
316 }
317 }
318
319 // TestBlobShowsAModelPreview: a binary model is the mesh, like a raster image is
320 // the picture. A text model (OBJ, glTF, ASCII STL) keeps its source view and
321 // previews only when asked, the same way markdown does.
322 func TestBlobShowsAModelPreview(t *testing.T) {
323 t.Parallel()
324 f := newFixture(t)
325 f.publish(f.repo)
326 work := workRepo(t, map[string]string{
327 "parts/bracket.stl": binaryModel,
328 "parts/part.obj": "# a text model\nv 0 0 0\n",
329 "parts/scene.glb": binaryModel,
330 })
331 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
332 t.Fatalf("push: %s", errs)
333 }
334
335 for _, tc := range []struct{ name, format string }{
336 {"parts/bracket.stl", "stl"},
337 {"parts/scene.glb", "glb"},
338 } {
339 code, page := f.get("/" + f.repo + "/blob/main/" + tc.name)
340 if code != http.StatusOK {
341 t.Fatalf("%s blob = %d", tc.name, code)
342 }
343 if !strings.Contains(page, `class="model-viewer"`) ||
344 !strings.Contains(page, `data-format="`+tc.format+`"`) ||
345 !strings.Contains(page, `data-model="/demo/raw/main/`+tc.name+`"`) {
346 t.Fatalf("%s blob does not mount the viewer:\n%s", tc.name, page)
347 }
348 if !strings.Contains(page, "Download") {
349 t.Fatalf("%s blob has no fallback download:\n%s", tc.name, page)
350 }
351 if strings.Contains(page, "Binary file") {
352 t.Fatalf("%s blob still says it is a binary file:\n%s", tc.name, page)
353 }
354 }
355
356 code, page := f.get("/" + f.repo + "/blob/main/parts/part.obj")
357 if code != http.StatusOK {
358 t.Fatalf("obj blob = %d", code)
359 }
360 if !strings.Contains(page, `href="/demo/blob/main/parts/part.obj?preview=1"`) {
361 t.Fatalf("a text model has no preview toggle:\n%s", page)
362 }
363 if strings.Contains(page, `class="model-viewer"`) {
364 t.Fatalf("a text model rendered the viewer by default:\n%s", page)
365 }
366
367 code, page = f.get("/" + f.repo + "/blob/main/parts/part.obj?preview=1")
368 if code != http.StatusOK {
369 t.Fatalf("obj preview = %d", code)
370 }
371 if !strings.Contains(page, `class="model-viewer"`) ||
372 !strings.Contains(page, `data-format="obj"`) ||
373 !strings.Contains(page, `data-model="/demo/raw/main/parts/part.obj"`) {
374 t.Fatalf("the obj preview does not mount the viewer:\n%s", page)
375 }
376 }
377
378 func TestCommitDiffShowsBothSidesOfAModel(t *testing.T) {
379 t.Parallel()
380 f := newFixture(t)
381 f.publish(f.repo)
382 work := workRepo(t, map[string]string{"parts/bracket.stl": binaryModel + "one"})
383 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
384 t.Fatalf("push: %s", errs)
385 }
386 old := revParse(t, work, "HEAD")
387
388 if err := os.WriteFile(filepath.Join(work, "parts/bracket.stl"), []byte(binaryModel+"two"), 0o644); err != nil {
389 t.Fatal(err)
390 }
391 if _, errs, err := runGit(t, work, "commit", "-qam", "change the model"); err != nil {
392 t.Fatalf("commit: %s", errs)
393 }
394 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
395 t.Fatalf("push: %s", errs)
396 }
397 newCommit := revParse(t, work, "HEAD")
398
399 code, page := f.get("/" + f.repo + "/commit/" + newCommit + "/parts/bracket.stl")
400 if code != http.StatusOK {
401 t.Fatalf("commit page = %d", code)
402 }
403 for _, want := range []string{
404 `class="media-diff"`,
405 `data-model="/demo/raw/` + old + `/parts/bracket.stl"`,
406 `data-model="/demo/raw/` + newCommit + `/parts/bracket.stl"`,
407 `data-format="stl"`,
408 ">before<",
409 ">after<",
410 } {
411 if !strings.Contains(page, want) {
412 t.Fatalf("the diff is missing %s:\n%s", want, page)
413 }
414 }
415 if strings.Contains(page, "Binary file: no line diff to show") {
416 t.Fatalf("the diff still calls the model a binary file:\n%s", page)
417 }
418 }
419
420 // The viewer and its vendor bundle are embedded static assets, so the module
421 // import in app.js has somewhere to land.
422 func TestModelViewerAssetsAreServed(t *testing.T) {
423 t.Parallel()
424 f := newFixture(t)
425 for _, tc := range []struct{ path, want string }{
426 {"/-/static/models/model-viewer.js", "export const upgrade"},
427 {"/-/static/models/three.bundle.js", "three.js"},
428 } {
429 resp, body := getWithHeaders(t, f.http.URL+tc.path)
430 if resp.StatusCode != http.StatusOK {
431 t.Fatalf("%s = %d", tc.path, resp.StatusCode)
432 }
433 if ct := resp.Header.Get("Content-Type"); !strings.Contains(ct, "javascript") {
434 t.Fatalf("%s Content-Type = %q, want JavaScript", tc.path, ct)
435 }
436 if !strings.Contains(body, tc.want) {
437 t.Fatalf("%s does not look like the asset it names", tc.path)
438 }
439 }
440 }