package api import ( "encoding/json" "io" "io/fs" "mime" "net/http" "net/url" "os" "path" "strings" "github.com/vblogio/upandown/internal/storage" ) func jsonDecode(r io.Reader, v any) error { return json.NewDecoder(r).Decode(v) } // inlineTypes : types MIME affichables dans le navigateur sans risque. Tout le // reste est servi en téléchargement (Content-Disposition: attachment), pour // qu'un fichier HTML ou un script déposé ne s'exécute jamais dans l'origine de // l'application. var inlineTypes = map[string]bool{ "image/png": true, "image/jpeg": true, "image/gif": true, "image/webp": true, "image/avif": true, "image/bmp": true, "image/x-icon": true, "image/vnd.microsoft.icon": true, "image/svg+xml": true, "video/mp4": true, "video/webm": true, "video/ogg": true, "video/quicktime": true, "video/x-m4v": true, "audio/mpeg": true, "audio/ogg": true, "audio/wav": true, "audio/x-wav": true, "audio/flac": true, "audio/aac": true, "audio/mp4": true, "audio/x-m4a": true, "audio/webm": true, "audio/opus": true, "application/pdf": true, } // contentType renvoie le type MIME d'un fichier d'après son extension. Les // fichiers texte sont toujours servis en text/plain (jamais en HTML ou XML). func contentType(name string) string { if storage.IsText(name) { return "text/plain; charset=utf-8" } t := mime.TypeByExtension(strings.ToLower(path.Ext(name))) if t == "" { return "application/octet-stream" } return t } func baseType(t string) string { t, _, _ = strings.Cut(t, ";") return strings.TrimSpace(strings.ToLower(t)) } // contentDisposition construit l'en-tête avec un nom ASCII de repli et le nom // UTF-8 d'origine (RFC 6266 / 5987). func contentDisposition(inline bool, name string) string { kind := "attachment" if inline { kind = "inline" } ascii := strings.Map(func(r rune) rune { if r < 0x20 || r > 0x7e || r == '"' || r == '\\' || r == '%' { return '_' } return r }, name) return kind + `; filename="` + ascii + `"; filename*=UTF-8''` + url.PathEscape(name) } // serveFile envoie un fichier avec prise en charge de Range, If-Modified-Since // et ETag. Affichage en ligne seulement si inline est demandé et que le type // est sûr ; sinon téléchargement. func serveFile(w http.ResponseWriter, r *http.Request, f *os.File, fi fs.FileInfo, name string, inline bool) { ct := contentType(name) bt := baseType(ct) showInline := inline && (inlineTypes[bt] || strings.HasPrefix(ct, "text/plain")) if !showInline { ct = "application/octet-stream" if t := mime.TypeByExtension(strings.ToLower(path.Ext(name))); t != "" && !showInline { ct = t } // Un téléchargement n'est jamais interprété : type générique pour les types actifs. if bt := baseType(ct); bt == "text/html" || bt == "application/xhtml+xml" || bt == "image/svg+xml" || bt == "text/xml" || bt == "application/xml" { ct = "application/octet-stream" } } h := w.Header() h.Set("Content-Type", ct) h.Set("Content-Disposition", contentDisposition(showInline, name)) h.Set("ETag", storage.ETag(fi)) h.Set("Cache-Control", "private, no-cache") h.Set("X-Content-Type-Options", "nosniff") if showInline && bt != "application/pdf" { // Aucun script ni sous-ressource, même en navigation directe (SVG). h.Set("Content-Security-Policy", "sandbox; default-src 'none'; img-src data:; media-src 'self'; style-src 'unsafe-inline'") } http.ServeContent(w, r, name, fi.ModTime(), f) }