diff --git a/internal/web/web.go b/internal/web/web.go index 53d36dc..1ed7d0f 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -3,6 +3,7 @@ package web import ( "bytes" + "encoding/base64" "errors" "html/template" "log" @@ -12,6 +13,7 @@ import ( "strings" "time" + "codeberg.org/squidd/parsimony/assets" "codeberg.org/squidd/parsimony/internal/archive" "codeberg.org/squidd/parsimony/internal/router" "codeberg.org/squidd/parsimony/internal/store" @@ -19,6 +21,9 @@ import ( const pageSize = 25 +// faviconURL is the logo as a data: URI, the only kind of image the CSP allows. +var faviconURL = "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte(assets.Mark)) + type server struct { store *store.Store router *router.Router @@ -29,7 +34,10 @@ type server struct { func New(s *store.Store, rt *router.Router) http.Handler { srv := &server{store: s, router: rt, tmpl: template.Must(template.New("").Funcs(template.FuncMap{ "hide": archive.Obfuscate, - "date": func(t time.Time) string { return t.UTC().Format("2006-01-02 15:04") }, + // The logo is a trusted static asset: inline, so the strict CSP needs no image source for it. + "mark": func() template.HTML { return template.HTML(assets.Mark) }, + "favicon": func() template.URL { return template.URL(faviconURL) }, + "date": func(t time.Time) string { return t.UTC().Format("2006-01-02 15:04") }, }).Parse(templates))} mux := http.NewServeMux() @@ -48,7 +56,7 @@ func New(s *store.Store, rt *router.Router) http.Handler { func secure(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { h := w.Header() - h.Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'") + h.Set("Content-Security-Policy", "default-src 'none'; img-src data:; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'") h.Set("X-Content-Type-Options", "nosniff") h.Set("Referrer-Policy", "no-referrer") h.Set("Content-Type", "text/html; charset=utf-8") @@ -238,6 +246,7 @@ const templates = ` {{define "head"}}