Merge pull request 'A logo for Parsimony' (#35) from feat/logo into main
|
|
@ -1,3 +1,10 @@
|
|||
<p align="center">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="assets/logo-dark.svg">
|
||||
<img src="assets/logo.svg" alt="Parsimony" height="72">
|
||||
</picture>
|
||||
</p>
|
||||
|
||||
# parsimony
|
||||
|
||||
A modern, single-binary mailing list manager: an evolution of Mailman.
|
||||
|
|
@ -316,6 +323,6 @@ Sending is bounded by the SMTP round trips to your relay: raise `relay.concurren
|
|||
## Licence
|
||||
|
||||
Parsimony is free software, released under the GNU General Public License, version 3 or (at your option) any later
|
||||
version. See [LICENSE](LICENSE). The components in `admin/src/components/ui` come from shadcn/ui (MIT); the Go
|
||||
version. See [LICENSE](LICENSE). The logo in `assets/` (generated by `assets/make-logo.py`) is released under the same licence. The components in `admin/src/components/ui` come from shadcn/ui (MIT); the Go
|
||||
dependencies (go-smtp, go-message, go-msgauth, modernc.org/sqlite, yaml.v3) use MIT, BSD or Apache 2.0 licences, which are
|
||||
compatible with the GPL.
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>admin</title>
|
||||
<title>Parsimony</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
|
|
|||
|
Before Width: | Height: | Size: 9.3 KiB After Width: | Height: | Size: 316 B |
1
admin/public/logo.svg
Normal file
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 338.7 56.4" role="img" aria-label="Parsimony"><g transform="translate(0 0.2)"><g transform="scale(0.875)"><rect width="64" height="64" rx="15" fill="#0f766e"/><g fill="none" stroke="#fff" stroke-width="4.2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21H48V45H16Z"/><path d="M16 21L32 37.32L48 21"/></g></g></g><g transform="translate(80.2 0.2)" fill="none" stroke="#0f172a" stroke-width="4.4" stroke-linecap="round" stroke-linejoin="round"><g transform="translate(0.0 0)"><path d="M0 16V54"/><circle cx="12" cy="28" r="12"/></g><g transform="translate(32.0 0)"><circle cx="12" cy="28" r="12"/><path d="M24 16V40"/></g><g transform="translate(64.5 0)"><path d="M0 16V40"/><path d="M0 29A13 13 0 0 1 13 16"/></g><g transform="translate(82.0 0)"><path d="M18 21C15 17 11 16 9 16C4 16 2 19.5 2 22.5C2 27 7 28 10.5 29C15 30 18 32 18 35C18 38.5 14 40 9 40C5 40 2 38.5 1 35"/></g><g transform="translate(108.5 0)"><path d="M0 16V40"/><path stroke="#0f766e" d="M0 6v0.01"/></g><g transform="translate(117.0 0)"><path d="M0 16V40"/><path d="M0 26A10 10 0 0 1 20 26V40"/><path d="M20 26A10 10 0 0 1 40 26V40"/></g><g transform="translate(165.5 0)"><circle cx="12" cy="28" r="12"/></g><g transform="translate(198.0 0)"><path d="M0 16V40"/><path d="M0 28A12 12 0 0 1 24 28V40"/></g><g transform="translate(230.5 0)"><path d="M0 16L12.6 40"/><path d="M24 16L6 54"/></g></g></svg>
|
||||
|
After Width: | Height: | Size: 1.4 KiB |
|
|
@ -3,6 +3,26 @@ import { describe, expect, it } from 'vitest'
|
|||
import { aList, installFakeApi, newState } from '@/test/fakeApi'
|
||||
import { renderApp } from '@/test/render'
|
||||
|
||||
describe('branding', () => {
|
||||
it('shows the logo on the login page', async () => {
|
||||
installFakeApi(newState())
|
||||
renderApp('/login')
|
||||
const logo = await screen.findByRole('img', { name: 'Parsimony' })
|
||||
expect(logo).toHaveAttribute('src', '/logo.svg')
|
||||
})
|
||||
|
||||
it('shows the mark next to the name in the header, as a decoration', async () => {
|
||||
installFakeApi(newState())
|
||||
renderApp('/', { token: 'good' })
|
||||
const brand = await screen.findByRole('link', { name: 'Parsimony' })
|
||||
expect(brand).toHaveAttribute('href', '/')
|
||||
const mark = brand.querySelector('img')
|
||||
expect(mark).not.toBeNull()
|
||||
expect(mark).toHaveAttribute('src', '/favicon.svg')
|
||||
expect(mark).toHaveAttribute('alt', '') // the name is already text: a screen reader must not say it twice
|
||||
})
|
||||
})
|
||||
|
||||
describe('authentication', () => {
|
||||
it('does not keep the token when the check fails for another reason than 401', async () => {
|
||||
installFakeApi(newState({ fail: { 'GET /lists': { status: 500, error: 'database is locked' } } }))
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ export function Shell() {
|
|||
<header className="border-b">
|
||||
<div className="mx-auto flex max-w-5xl items-center justify-between p-4">
|
||||
<div className="flex items-center gap-6">
|
||||
<Link to="/" className="text-lg font-semibold">
|
||||
<Link to="/" className="flex items-center gap-2 text-lg font-semibold">
|
||||
<img src="/favicon.svg" alt="" className="size-7" />
|
||||
Parsimony
|
||||
</Link>
|
||||
<Link to="/queue" className="text-sm text-muted-foreground hover:text-foreground">
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ export function LoginPage() {
|
|||
}
|
||||
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center p-4">
|
||||
<div className="flex min-h-screen flex-col items-center justify-center gap-6 p-4">
|
||||
<img src="/logo.svg" alt="Parsimony" className="h-12" />
|
||||
<Card className="w-full max-w-sm">
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
|
|
|
|||
19
assets/assets.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Package assets holds the Parsimony logo. The files are generated by make-logo.py.
|
||||
package assets
|
||||
|
||||
import _ "embed"
|
||||
|
||||
// Mark is the square mark: an envelope in a rounded square. It is also the favicon.
|
||||
//
|
||||
//go:embed logo-mark.svg
|
||||
var Mark string
|
||||
|
||||
// Logo is the mark with the wordmark, for light backgrounds.
|
||||
//
|
||||
//go:embed logo.svg
|
||||
var Logo string
|
||||
|
||||
// LogoDark is the mark with the wordmark, for dark backgrounds.
|
||||
//
|
||||
//go:embed logo-dark.svg
|
||||
var LogoDark string
|
||||
123
assets/assets_test.go
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
package assets
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var files = map[string]string{"logo-mark.svg": Mark, "logo.svg": Logo, "logo-dark.svg": LogoDark}
|
||||
|
||||
func TestLogosAreWellFormedSVG(t *testing.T) {
|
||||
for name, src := range files {
|
||||
if src == "" {
|
||||
t.Fatalf("%s is empty", name)
|
||||
}
|
||||
dec := xml.NewDecoder(strings.NewReader(src))
|
||||
var root *xml.StartElement
|
||||
for {
|
||||
tok, err := dec.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", name, err)
|
||||
}
|
||||
if se, ok := tok.(xml.StartElement); ok && root == nil {
|
||||
se := se
|
||||
root = &se
|
||||
}
|
||||
}
|
||||
if root == nil || root.Name.Local != "svg" || root.Name.Space != "http://www.w3.org/2000/svg" {
|
||||
t.Fatalf("%s: root element %+v", name, root)
|
||||
}
|
||||
attrs := map[string]string{}
|
||||
for _, a := range root.Attr {
|
||||
attrs[a.Name.Local] = a.Value
|
||||
}
|
||||
if attrs["viewBox"] == "" || attrs["role"] != "img" || attrs["aria-label"] != "Parsimony" {
|
||||
t.Errorf("%s: it must scale (viewBox) and be named for screen readers: %v", name, attrs)
|
||||
}
|
||||
if attrs["width"] != "" || attrs["height"] != "" {
|
||||
t.Errorf("%s: no fixed size: the page decides", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogosNeedNothingFromOutside(t *testing.T) {
|
||||
// Inline in a page with a strict CSP, in a README, on a dark background: no fonts, scripts, images or links.
|
||||
for name, src := range files {
|
||||
for _, bad := range []string{"<text", "<script", "<image", "<use", "<style", "xlink:", "href=", "url(", "@import", "font-family", "<foreignObject"} {
|
||||
if strings.Contains(src, bad) {
|
||||
t.Errorf("%s contains %q", name, bad)
|
||||
}
|
||||
}
|
||||
if n := strings.Count(src, "http"); n != 1 {
|
||||
t.Errorf("%s: only the SVG namespace may mention http, found %d", name, n)
|
||||
}
|
||||
if len(src) > 4000 {
|
||||
t.Errorf("%s is %d bytes: a logo should stay tiny", name, len(src))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheWordmarkLogosDifferOnlyInColour(t *testing.T) {
|
||||
strip := func(s string) string {
|
||||
for _, c := range []string{"#0f172a", "#ffffff", "#0f766e", "#2dd4bf"} {
|
||||
s = strings.ReplaceAll(s, c, "#")
|
||||
}
|
||||
return s
|
||||
}
|
||||
if strip(Logo) != strip(LogoDark) {
|
||||
t.Fatal("the light and dark logos must have the same shapes")
|
||||
}
|
||||
if Logo == LogoDark {
|
||||
t.Fatal("the dark logo must use light colours")
|
||||
}
|
||||
if !strings.Contains(LogoDark, "#ffffff") || strings.Contains(LogoDark, "#0f172a") {
|
||||
t.Fatal("the dark logo draws the letters in white, not in the dark ink")
|
||||
}
|
||||
if !strings.Contains(Logo, "#0f172a") {
|
||||
t.Fatal("the light logo draws the letters in the dark ink")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheAdminUIShipsTheSameLogos(t *testing.T) {
|
||||
for public, want := range map[string]string{"../admin/public/favicon.svg": Mark, "../admin/public/logo.svg": Logo} {
|
||||
got, err := os.ReadFile(public)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v (copy it from assets/)", public, err)
|
||||
}
|
||||
if !bytes.Equal(got, []byte(want)) {
|
||||
t.Errorf("%s differs from the file in assets/: run `python3 assets/make-logo.py assets` and copy it", public)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The mark is white on teal: that pair must stay readable (WCAG AA for graphics asks 3:1).
|
||||
func TestTheMarkHasEnoughContrast(t *testing.T) {
|
||||
lum := func(hex string) float64 {
|
||||
var r, g, b int
|
||||
if _, err := fmtSscanf(hex, &r, &g, &b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := func(c int) float64 {
|
||||
v := float64(c) / 255
|
||||
if v <= 0.03928 {
|
||||
return v / 12.92
|
||||
}
|
||||
return pow((v+0.055)/1.055, 2.4)
|
||||
}
|
||||
return 0.2126*f(r) + 0.7152*f(g) + 0.0722*f(b)
|
||||
}
|
||||
l1, l2 := lum("#ffffff"), lum("#0f766e")
|
||||
if ratio := (l1 + 0.05) / (l2 + 0.05); ratio < 4.5 {
|
||||
t.Fatalf("contrast %.2f:1", ratio)
|
||||
}
|
||||
if !strings.Contains(Mark, "#0f766e") || !strings.Contains(Mark, "#ffffff") {
|
||||
t.Fatal("the contrast test checks the colours the mark really uses")
|
||||
}
|
||||
}
|
||||
11
assets/helpers_test.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package assets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func fmtSscanf(hex string, r, g, b *int) (int, error) {
|
||||
return fmt.Sscanf(hex, "#%02x%02x%02x", r, g, b)
|
||||
}
|
||||
func pow(x, y float64) float64 { return math.Pow(x, y) }
|
||||
1
assets/logo-dark.svg
Normal file
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 338.7 56.4" role="img" aria-label="Parsimony"><g transform="translate(0 0.2)"><g transform="scale(0.875)"><rect width="64" height="64" rx="15" fill="#2dd4bf"/><g fill="none" stroke="#fff" stroke-width="4.2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21H48V45H16Z"/><path d="M16 21L32 37.32L48 21"/></g></g></g><g transform="translate(80.2 0.2)" fill="none" stroke="#ffffff" stroke-width="4.4" stroke-linecap="round" stroke-linejoin="round"><g transform="translate(0.0 0)"><path d="M0 16V54"/><circle cx="12" cy="28" r="12"/></g><g transform="translate(32.0 0)"><circle cx="12" cy="28" r="12"/><path d="M24 16V40"/></g><g transform="translate(64.5 0)"><path d="M0 16V40"/><path d="M0 29A13 13 0 0 1 13 16"/></g><g transform="translate(82.0 0)"><path d="M18 21C15 17 11 16 9 16C4 16 2 19.5 2 22.5C2 27 7 28 10.5 29C15 30 18 32 18 35C18 38.5 14 40 9 40C5 40 2 38.5 1 35"/></g><g transform="translate(108.5 0)"><path d="M0 16V40"/><path stroke="#2dd4bf" d="M0 6v0.01"/></g><g transform="translate(117.0 0)"><path d="M0 16V40"/><path d="M0 26A10 10 0 0 1 20 26V40"/><path d="M20 26A10 10 0 0 1 40 26V40"/></g><g transform="translate(165.5 0)"><circle cx="12" cy="28" r="12"/></g><g transform="translate(198.0 0)"><path d="M0 16V40"/><path d="M0 28A12 12 0 0 1 24 28V40"/></g><g transform="translate(230.5 0)"><path d="M0 16L12.6 40"/><path d="M24 16L6 54"/></g></g></svg>
|
||||
|
After Width: | Height: | Size: 1.4 KiB |
BIN
assets/logo-mark-512.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
1
assets/logo-mark.svg
Normal file
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="Parsimony"><rect width="64" height="64" rx="15" fill="#0f766e"/><g fill="none" stroke="#ffffff" stroke-width="4.2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21H48V45H16Z"/><path d="M16 21L32 37.32L48 21"/></g></svg>
|
||||
|
After Width: | Height: | Size: 316 B |
BIN
assets/logo.png
Normal file
|
After Width: | Height: | Size: 29 KiB |
1
assets/logo.svg
Normal file
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 338.7 56.4" role="img" aria-label="Parsimony"><g transform="translate(0 0.2)"><g transform="scale(0.875)"><rect width="64" height="64" rx="15" fill="#0f766e"/><g fill="none" stroke="#fff" stroke-width="4.2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21H48V45H16Z"/><path d="M16 21L32 37.32L48 21"/></g></g></g><g transform="translate(80.2 0.2)" fill="none" stroke="#0f172a" stroke-width="4.4" stroke-linecap="round" stroke-linejoin="round"><g transform="translate(0.0 0)"><path d="M0 16V54"/><circle cx="12" cy="28" r="12"/></g><g transform="translate(32.0 0)"><circle cx="12" cy="28" r="12"/><path d="M24 16V40"/></g><g transform="translate(64.5 0)"><path d="M0 16V40"/><path d="M0 29A13 13 0 0 1 13 16"/></g><g transform="translate(82.0 0)"><path d="M18 21C15 17 11 16 9 16C4 16 2 19.5 2 22.5C2 27 7 28 10.5 29C15 30 18 32 18 35C18 38.5 14 40 9 40C5 40 2 38.5 1 35"/></g><g transform="translate(108.5 0)"><path d="M0 16V40"/><path stroke="#0f766e" d="M0 6v0.01"/></g><g transform="translate(117.0 0)"><path d="M0 16V40"/><path d="M0 26A10 10 0 0 1 20 26V40"/><path d="M20 26A10 10 0 0 1 40 26V40"/></g><g transform="translate(165.5 0)"><circle cx="12" cy="28" r="12"/></g><g transform="translate(198.0 0)"><path d="M0 16V40"/><path d="M0 28A12 12 0 0 1 24 28V40"/></g><g transform="translate(230.5 0)"><path d="M0 16L12.6 40"/><path d="M24 16L6 54"/></g></g></svg>
|
||||
|
After Width: | Height: | Size: 1.4 KiB |
85
assets/make-logo.py
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
# Generates the Parsimony logo files: python3 assets/make-logo.py assets
|
||||
# The wordmark is drawn with monoline strokes, not typeset: it needs no font and looks the same everywhere.
|
||||
# The mark is an envelope in a rounded square; the dot of the i echoes it in the accent colour.
|
||||
import math, sys
|
||||
|
||||
INK, TEAL, WHITE = "#0f172a", "#0f766e", "#ffffff"
|
||||
SW = 4.4 # stroke width of the letters
|
||||
H = 24 # x-height
|
||||
B = 40 # baseline y in the wordmark box
|
||||
T = B - H
|
||||
GAP = 8.5
|
||||
|
||||
def circle(cx, cy, r): return f'<circle cx="{cx}" cy="{cy}" r="{r}"/>'
|
||||
def path(d): return f'<path d="{d}"/>'
|
||||
|
||||
def letter(ch):
|
||||
"""returns (svg elements, advance width) with x from 0"""
|
||||
if ch == "o": return circle(12, B-12, 12), 24
|
||||
if ch == "a": return circle(12, B-12, 12) + path(f"M24 {T}V{B}"), 24
|
||||
if ch == "p": return path(f"M0 {T}V{B+14}") + circle(12, B-12, 12), 24
|
||||
if ch == "n": return path(f"M0 {T}V{B}") + path(f"M0 {T+12}A12 12 0 0 1 24 {T+12}V{B}"), 24
|
||||
if ch == "m": return path(f"M0 {T}V{B}") + path(f"M0 {T+10}A10 10 0 0 1 20 {T+10}V{B}") + path(f"M20 {T+10}A10 10 0 0 1 40 {T+10}V{B}"), 40
|
||||
if ch == "r": return path(f"M0 {T}V{B}") + path(f"M0 {T+13}A13 13 0 0 1 13 {T}"), 13
|
||||
if ch == "i": return path(f"M0 {T}V{B}") + f'<path class="dot" d="M0 {T-10}v0.01"/>', 0
|
||||
if ch == "y": return path(f"M0 {T}L12.6 {B}") + path(f"M24 {T}L6 {B+14}"), 24
|
||||
if ch == "s":
|
||||
d = (f"M18 {T+5}C15 {T+1} 11 {T} 9 {T}C4 {T} 2 {T+3.5} 2 {T+6.5}C2 {T+11} 7 {T+12} 10.5 {T+13}"
|
||||
f"C15 {T+14} 18 {T+16} 18 {T+19}C18 {T+22.5} 14 {B} 9 {B}C5 {B} 2 {B-1.5} 1 {B-5}")
|
||||
return path(d), 18
|
||||
raise ValueError(ch)
|
||||
|
||||
def wordmark(text="parsimony"):
|
||||
kern = {("r", "s"): -4.0, ("p", "a"): -0.5}
|
||||
x = 0; out = []; prev = None
|
||||
for ch in text:
|
||||
x += kern.get((prev, ch), 0)
|
||||
svg, w = letter(ch)
|
||||
out.append(f'<g transform="translate({x:.1f} 0)">{svg}</g>')
|
||||
x += w + GAP
|
||||
prev = ch
|
||||
return "".join(out), x - GAP
|
||||
|
||||
def envelope(cx, cy, w=30, h=22, sw=4):
|
||||
"""envelope outline with its flap, centred on cx,cy"""
|
||||
x0, y0, x1, y1 = cx-w/2, cy-h/2, cx+w/2, cy+h/2
|
||||
f = lambda v: f"{v:g}"
|
||||
return (f'<path d="M{f(x0)} {f(y0)}H{f(x1)}V{f(y1)}H{f(x0)}Z"/>'
|
||||
f'<path d="M{f(x0)} {f(y0)}L{f(cx)} {f(cy+h*0.18)}L{f(x1)} {f(y0)}"/>')
|
||||
|
||||
def mark_svg(bg=True, fg=WHITE, bgc=TEAL):
|
||||
s = f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="Parsimony">'
|
||||
if bg: s += f'<rect width="64" height="64" rx="15" fill="{bgc}"/>'
|
||||
s += f'<g fill="none" stroke="{fg}" stroke-width="4.2" stroke-linecap="round" stroke-linejoin="round">{envelope(32, 33, 32, 24)}</g>'
|
||||
s += '</svg>'
|
||||
return s
|
||||
|
||||
def logo_svg(dark=False):
|
||||
ink = WHITE if dark else INK
|
||||
teal = "#2dd4bf" if dark else TEAL
|
||||
wm, ww = wordmark()
|
||||
gap = 22
|
||||
scale = 1.0
|
||||
mark = 56
|
||||
total_w = mark + gap + ww + 4
|
||||
# vertical: mark centred on the x-height band of the wordmark
|
||||
top = T - 14 - 2.2 # i-dot top
|
||||
bottom = B + 14 + 2.2 # descenders
|
||||
h = bottom - top
|
||||
my = (h - mark) / 2
|
||||
tx = mark + gap + 2.2
|
||||
ty = -top
|
||||
s = (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {total_w+2.2:.1f} {h:.1f}" role="img" aria-label="Parsimony">'
|
||||
f'<g transform="translate(0 {my:.1f})">'
|
||||
f'<g transform="scale({mark/64})"><rect width="64" height="64" rx="15" fill="{teal}"/>'
|
||||
f'<g fill="none" stroke="#fff" stroke-width="4.2" stroke-linecap="round" stroke-linejoin="round">{envelope(32,33,32,24)}</g></g></g>'
|
||||
f'<g transform="translate({tx:.1f} {ty:.1f})" fill="none" stroke="{ink}" stroke-width="{SW}" stroke-linecap="round" stroke-linejoin="round">{wm}</g>'
|
||||
f'</svg>')
|
||||
s = s.replace('class="dot"', f'stroke="{teal}"')
|
||||
return s
|
||||
|
||||
if __name__ == "__main__":
|
||||
out = sys.argv[1]
|
||||
open(f"{out}/logo.svg","w").write(logo_svg(False))
|
||||
open(f"{out}/logo-dark.svg","w").write(logo_svg(True))
|
||||
open(f"{out}/logo-mark.svg","w").write(mark_svg())
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||