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.

Code

527 commit(s) on main

Put job execution behind an interface Keenan Nemetz · 2h ago de3eec16
history blame raw 3.2 KiB
1package web
2
3// Media previews. A blob whose name says it is an image is served as that image,
4// so the <img> a README already emits works and the source view can show the
5// picture. Everything else keeps text/plain: a blob is untrusted bytes, and
6// serving an HTML file as text/html would run it on this origin.
7//
8// The list is an allowlist rather than mime.TypeByExtension because the type
9// decides whether a browser treats the response as a document. An extension a
10// browser does not decode as an image is not on it, so a `.html` blob stays
11// text and a `.txt` blob that happens to hold markup cannot be sniffed as one.
12
13import (
14 "path"
15 "strings"
16)
17
18// maxMediaPreviewBytes bounds the picture the source view loads in place. The
19// raw route streams anything, but asking a browser to decode a 500 MiB image is
20// a way to freeze a tab; a file that size is a download.
21const maxMediaPreviewBytes = 32 << 20
22
23// maxModelPreviewBytes bounds the mesh the viewer holds in memory. A part past
24// this is a download; drawing it would cost the reader more than it shows.
25const maxModelPreviewBytes = 64 << 20
26
27// imageTypes maps the extensions a browser decodes to the type it must be told.
28var imageTypes = map[string]string{
29 ".avif": "image/avif",
30 ".bmp": "image/bmp",
31 ".gif": "image/gif",
32 ".ico": "image/x-icon",
33 ".jpeg": "image/jpeg",
34 ".jpg": "image/jpeg",
35 ".png": "image/png",
36 ".svg": "image/svg+xml",
37 ".webp": "image/webp",
38}
39
40// modelType is what the viewer needs to draw one model format: the loader key
41// the browser picks, and the type a download is served as. None of these types
42// is a document, and raw marks every response nosniff, so a model is never
43// rendered as one.
44type modelType struct {
45 format string
46 media string
47}
48
49var modelTypes = map[string]modelType{
50 ".3mf": {format: "3mf", media: "model/3mf"},
51 ".glb": {format: "glb", media: "model/gltf-binary"},
52 ".gltf": {format: "gltf", media: "model/gltf+json"},
53 ".obj": {format: "obj", media: "model/obj"},
54 ".stl": {format: "stl", media: "model/stl"},
55}
56
57// isImage reports whether a path names a picture the web views preview.
58func isImage(p string) bool {
59 _, ok := imageTypes[strings.ToLower(path.Ext(p))]
60 return ok
61}
62
63// isModel reports whether a path names a 3D model the viewer can draw.
64func isModel(p string) bool {
65 _, ok := modelTypes[strings.ToLower(path.Ext(p))]
66 return ok
67}
68
69// modelFormat is the loader key the browser uses for a model path.
70func modelFormat(p string) string {
71 return modelTypes[strings.ToLower(path.Ext(p))].format
72}
73
74// isSVG reports whether a path is an SVG, which is the one image that is also a
75// document and so needs a policy of its own when served.
76func isSVG(p string) bool {
77 return strings.EqualFold(path.Ext(p), ".svg")
78}
79
80// rawContentType names a raw blob for the browser. Only an allowed image gets a
81// type a browser acts on; a model gets its real media type, which no browser
82// renders; a text file, including one that contains HTML, stays text/plain.
83func rawContentType(p string) string {
84 ext := strings.ToLower(path.Ext(p))
85 if t, ok := imageTypes[ext]; ok {
86 return t
87 }
88 if m, ok := modelTypes[ext]; ok {
89 return m.media
90 }
91 return "text/plain; charset=utf-8"
92}
93