feat(web): logo and favicon on the public pages
- Show the mark on every page, linking to the index, inline so the CSP needs no image source - Add the favicon as a data: URI and allow img-src data: only
This commit is contained in:
parent
5f5deb3e30
commit
ec8ca567ff
2 changed files with 50 additions and 3 deletions
|
|
@ -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"}}<!doctype html>
|
||||
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{.}}</title>
|
||||
<link rel="icon" type="image/svg+xml" href="{{favicon}}">
|
||||
<style>
|
||||
body{font:16px/1.5 system-ui,sans-serif;max-width:50rem;margin:2rem auto;padding:0 1rem;color:#222}
|
||||
a{color:#0b5cad}
|
||||
|
|
@ -250,7 +259,10 @@ pre{white-space:pre-wrap;word-wrap:break-word;background:#f6f6f6;padding:1rem;bo
|
|||
input[type=search]{padding:.35rem;width:16rem}
|
||||
button{padding:.5rem 1rem;font-size:1rem}
|
||||
nav a{margin-right:1rem}
|
||||
</style></head><body>{{end}}
|
||||
.brand{display:inline-block;margin-bottom:1rem}
|
||||
.brand svg{width:2rem;height:2rem;display:block}
|
||||
</style></head><body>
|
||||
<a class="brand" href="/" aria-label="All archives">{{mark}}</a>{{end}}
|
||||
{{define "foot"}}</body></html>{{end}}
|
||||
|
||||
{{define "index"}}{{template "head" "Mailing list archives"}}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
|
|
@ -9,6 +11,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"codeberg.org/squidd/parsimony/assets"
|
||||
"codeberg.org/squidd/parsimony/internal/router"
|
||||
"codeberg.org/squidd/parsimony/internal/store"
|
||||
)
|
||||
|
|
@ -289,3 +292,35 @@ func TestUnsubscribeOneClickBodyIsAccepted(t *testing.T) {
|
|||
t.Fatal("bob must be unsubscribed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEveryPageCarriesTheLogoAndTheFavicon(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
id := f.add(t, f.pub.ID, "<1@x>", "Hello", "A", "body", 1_700_000_000, "")
|
||||
favicon := "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte(assets.Mark))
|
||||
for _, p := range []string{"/", "/archive/dev@example.org", fmt.Sprintf("/archive/dev@example.org/%d", id), "/unsubscribe/" + f.bobTok, "/nope"} {
|
||||
rec := get(t, f.h, p)
|
||||
body := html.UnescapeString(rec.Body.String()) // html/template writes "+" in an attribute as +
|
||||
if !strings.Contains(body, `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="Parsimony">`) {
|
||||
t.Errorf("%s: no inline logo", p)
|
||||
}
|
||||
if !strings.Contains(body, `<link rel="icon" type="image/svg+xml" href="`+favicon+`">`) {
|
||||
t.Errorf("%s: no favicon (a data: URI, because the CSP allows nothing else)", p)
|
||||
}
|
||||
if !strings.Contains(body, `<a class="brand" href="/" aria-label="All archives">`) {
|
||||
t.Errorf("%s: the logo must lead back to the index", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheCSPAllowsOnlyDataImages(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
csp := get(t, f.h, "/").Header().Get("Content-Security-Policy")
|
||||
if !strings.Contains(csp, "img-src data:") || !strings.Contains(csp, "default-src 'none'") {
|
||||
t.Fatalf("csp = %q", csp)
|
||||
}
|
||||
for _, loose := range []string{"*", "http:", "https:", "'unsafe-eval'", "script-src"} {
|
||||
if strings.Contains(csp, loose) {
|
||||
t.Errorf("the CSP must stay strict, it contains %q: %s", loose, csp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue