| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "mime" |
| 5 | "net/http" |
| 6 | "path" |
| 7 | "strings" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/access" |
| 10 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 11 | ) |
| 12 | |
| 13 | // rawMax: groter dan dit levert raw niet (geheugen van de server). |
| 14 | const rawMax = 50 << 20 |
| 15 | |
| 16 | // rawImageTypes worden als afbeelding geserveerd; al het andere als tekst |
| 17 | // of octet-stream, zodat HTML/JS uit een repo nooit op ons domein draait. |
| 18 | var rawImageTypes = map[string]string{ |
| 19 | ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", |
| 20 | ".webp": "image/webp", ".ico": "image/x-icon", ".bmp": "image/bmp", ".avif": "image/avif", |
| 21 | } |
| 22 | |
| 23 | // raw toont alleen de inhoud van een bestand, met dezelfde toegang als de |
| 24 | // bestandsweergave. ?download=1 geeft een bijlage met de bestandsnaam; |
| 25 | // ?as=img levert een .svg als image/svg+xml voor <img> (in een sandbox). |
| 26 | func (s *Server) raw(w http.ResponseWriter, r *http.Request) { |
| 27 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 28 | if !ok { |
| 29 | return |
| 30 | } |
| 31 | rel, err := cleanRel(r.PathValue("path")) |
| 32 | if err != nil || rel == "" { |
| 33 | http.NotFound(w, r) |
| 34 | return |
| 35 | } |
| 36 | rev := strings.TrimSpace(r.URL.Query().Get("rev")) |
| 37 | if rev == "" { |
| 38 | rev = repo.DefaultBranch |
| 39 | } |
| 40 | if strings.HasPrefix(rev, "-") { |
| 41 | http.NotFound(w, r) |
| 42 | return |
| 43 | } |
| 44 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 45 | size, err := gitrepo.BlobSize(r.Context(), s.Git, dir, rev, rel) |
| 46 | if err != nil { |
| 47 | http.NotFound(w, r) |
| 48 | return |
| 49 | } |
| 50 | if size > rawMax { |
| 51 | http.Error(w, s.tr(r, "raw.tooBig"), http.StatusRequestEntityTooLarge) |
| 52 | return |
| 53 | } |
| 54 | data, err := gitrepo.BlobText(r.Context(), s.Git, dir, rev, rel) |
| 55 | if err != nil { |
| 56 | http.NotFound(w, r) |
| 57 | return |
| 58 | } |
| 59 | name := path.Base(rel) |
| 60 | ext := strings.ToLower(path.Ext(name)) |
| 61 | binary := strings.Contains(data, "\x00") |
| 62 | h := w.Header() |
| 63 | h.Set("X-Content-Type-Options", "nosniff") |
| 64 | h.Set("Content-Security-Policy", "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; sandbox") |
| 65 | h.Set("Cache-Control", "private, no-cache") |
| 66 | if binary { |
| 67 | h.Set("X-BlueGit-Binary", "1") |
| 68 | } |
| 69 | switch { |
| 70 | case r.URL.Query().Get("download") == "1": |
| 71 | h.Set("Content-Type", "application/octet-stream") |
| 72 | h.Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": name})) |
| 73 | case ext == ".svg" && r.URL.Query().Get("as") == "img": |
| 74 | h.Set("Content-Type", "image/svg+xml") |
| 75 | case rasterImageType(name, data) != "": |
| 76 | h.Set("Content-Type", rasterImageType(name, data)) |
| 77 | case binary: |
| 78 | h.Set("Content-Type", "application/octet-stream") |
| 79 | h.Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": name})) |
| 80 | default: |
| 81 | h.Set("Content-Type", "text/plain; charset=utf-8") |
| 82 | } |
| 83 | _, _ = w.Write([]byte(data)) |
| 84 | } |