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.

Render images and 3D models in the browser views

3b2bcc30534f24b4628b6365e69dcb9065115065 · Keenan Nemetz · 13d ago · parent 87bab8e1

Render images and 3D models in the browser views

A blob whose name is an image is served as that image, so the <img> a README
already emitted works and the source view can show it. An SVG keeps its source
view and previews on request. The instance introduction resolves assets/, so a
landing-page picture is no longer a broken link.

A changed image or model shows both sides in the commit, compare, and change
request diffs, where git only says "binary". The 3D viewer is a vendored
three.js bundle, loaded only when a page has a model, with a download fallback
for a reader without JavaScript or WebGL.

closes: 01M7XQ9AKKKFNEX0F679JRRYQD

server/media_test.go +437

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