A security review of the whole repo. The queries were already scoped, the
OIDC flow already did state and nonce and PKCE, the session tokens were
already stored as hashes. What it found was mostly the seam between the
code and the deployment — and one place where the deployment quietly
undid the code.
The one that matters: with any AUTHENTIK_* variable missing, Petal fell
back to resolving every request to the single `local` user. That is right
on a laptop and a catastrophe on a public host, and Phase 16 removed the
Traefik basic-auth gate that used to stand behind the mistake. A typo in
the client secret would have served her journals to the open internet and
said so only in a log line nobody reads. It now refuses to start, guarded
by default for any BASE_URL that isn't loopback.
Then the one that would have been fixed and wasn't: stored images now
serve under `default-src 'none'; sandbox`, so an SVG pasted into a
document can't run as a page on Petal's own origin. Traefik's
customresponseheaders *overwrites*, so the CSP declared in the compose
labels would have silently replaced that per-route policy in production.
The whole header block moved into the binary, where a route can tighten
its own and a test can prove it; only HSTS stays at the edge, where TLS
actually terminates.
The rest, smaller:
- PETAL_ALLOWED_SUBS empty means everyone authentik authenticates, and
authentik here fronts half a dozen applications. Still legal, now
said out loud every boot, and set in both env examples.
- LLM failures relayed err.Error() to the browser, which carries the
address of the inference box on the far side of the VPN. Logged
instead; the client only ever rendered "the helper is resting".
- Exports scheme-check their links. Escaping makes a URL safe to sit
in an attribute and says nothing about following it, and an export
is the one artifact here meant to leave. Writing the test found the
markdown image src, which I'd missed reading it.
- The draft rescue is namespaced per account and cleared on sign-out.
Everything else in localStorage is a preference; this is her unsaved
writing, sitting in a profile two people share.
- /auth/logout is POST-only. With SameSite=Lax a GET route lets any
page on the internet sign her out mid-draft.
- Image uploads get a per-account allowance and the TTS cache a size
cap. Both share the encrypted volume the database is on, and a full
disk is SQLite failing to write, not a feature degrading.
- The session cookie takes the __Host- prefix over https, so nothing
else under parodia.dev can plant one. Old cookies still resolve;
nobody is signed out to get there.
- npm audit: linkify-it and postcss.
Verified: go build, go vet, the full Go suite, tsc, 195 frontend tests,
npm audit clean. The startup guard and both CSPs checked against a
running server rather than only asserted.
Claude-Session: https://claude.ai/code/session_016y6gyuHkQXPiEuW8RGQyua
137 lines
4.9 KiB
Go
137 lines
4.9 KiB
Go
package config
|
|
|
|
import "testing"
|
|
|
|
// The Piper instances are discovered from the environment rather than named in
|
|
// code, so that a new pair costs a compose service and two .env lines. These
|
|
// assert the discovery rule, including the two shapes that already exist in the
|
|
// wild (millenia's systemd unit and the VPS compose file).
|
|
func TestTTSVoicesDiscovery(t *testing.T) {
|
|
voices := ttsVoices([]string{
|
|
"TTS_ENDPOINT=http://piper-en:5000",
|
|
"TTS_VOICE_EN=en_US-amy-medium",
|
|
"TTS_ENDPOINT_ZH=http://piper-zh:5000/",
|
|
"TTS_VOICE_ZH=zh_CN-huayan-medium",
|
|
"TTS_ENDPOINT_PT=http://piper-pt:5000",
|
|
"TTS_VOICE_PT=pt_PT-tugão-medium",
|
|
"TTS_ENDPOINT_FR=http://piper-fr:5000",
|
|
"TTS_VOICE_FR=fr_FR-siwis-medium",
|
|
// Noise that must not become a language.
|
|
"TTS_PATH=/synthesize",
|
|
"PATH=/usr/bin",
|
|
})
|
|
|
|
want := map[string]TTSVoice{
|
|
"en": {"http://piper-en:5000", "en_US-amy-medium"},
|
|
// The trailing slash is trimmed here so the synthesis path concatenates
|
|
// cleanly rather than producing a double slash at every call site.
|
|
"zh": {"http://piper-zh:5000", "zh_CN-huayan-medium"},
|
|
"pt": {"http://piper-pt:5000", "pt_PT-tugão-medium"},
|
|
// Phase 24's whole TTS change: a fourth language costs two lines here
|
|
// and a compose service, and no Go at all.
|
|
"fr": {"http://piper-fr:5000", "fr_FR-siwis-medium"},
|
|
}
|
|
if len(voices) != len(want) {
|
|
t.Fatalf("discovered %v, want %v", voices, want)
|
|
}
|
|
for lang, w := range want {
|
|
if voices[lang] != w {
|
|
t.Errorf("%s = %+v, want %+v", lang, voices[lang], w)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Half a configuration is not a language. An endpoint with no voice (or the
|
|
// reverse) must read as "no voice for this language" — a 404 the client answers
|
|
// by falling back to Web Speech — rather than as an instance that exists and
|
|
// errors on every request.
|
|
func TestTTSVoicesIgnoresHalfConfiguredLanguages(t *testing.T) {
|
|
voices := ttsVoices([]string{
|
|
"TTS_ENDPOINT=http://piper-en:5000",
|
|
"TTS_VOICE_EN=en_US-amy-medium",
|
|
"TTS_ENDPOINT_FR=http://piper-fr:5000", // no TTS_VOICE_FR
|
|
"TTS_VOICE_ES=es_ES-davefx-medium", // no TTS_ENDPOINT_ES
|
|
})
|
|
if _, ok := voices["fr"]; ok {
|
|
t.Errorf("fr routed with no voice configured")
|
|
}
|
|
if _, ok := voices["es"]; ok {
|
|
t.Errorf("es routed with no endpoint configured")
|
|
}
|
|
if len(voices) != 1 {
|
|
t.Errorf("discovered %v, want English only", voices)
|
|
}
|
|
}
|
|
|
|
// A deployment that predates the map names only the endpoints and relies on the
|
|
// voice defaults; it must sound exactly as it did.
|
|
func TestTTSVoicesKeepsTheOriginalDefaults(t *testing.T) {
|
|
voices := ttsVoices([]string{
|
|
"TTS_ENDPOINT=http://127.0.0.1:5005",
|
|
"TTS_ENDPOINT_ZH=http://127.0.0.1:5006",
|
|
})
|
|
if got := voices["en"].Voice; got != "en_US-amy-medium" {
|
|
t.Errorf("en voice = %q, want the default", got)
|
|
}
|
|
if got := voices["zh"].Voice; got != "zh_CN-huayan-medium" {
|
|
t.Errorf("zh voice = %q, want the default", got)
|
|
}
|
|
}
|
|
|
|
// Read-aloud is off when no English instance is configured; nothing else may
|
|
// switch it on. (tts.New gates on TTSEndpoint, so a stray TTS_ENDPOINT_PT with
|
|
// no English sibling must not produce a routable map that outlives that gate.)
|
|
func TestTTSVoicesEmptyWithoutEndpoints(t *testing.T) {
|
|
if voices := ttsVoices([]string{"TTS_VOICE_EN=en_US-amy-medium"}); len(voices) != 0 {
|
|
t.Errorf("discovered %v, want none", voices)
|
|
}
|
|
}
|
|
|
|
// The fallback to the single local user is right on a laptop and a catastrophe
|
|
// on a public host, so it is defaulted from the origin Petal knows itself by
|
|
// rather than left to be remembered.
|
|
func TestRequireAuthDefaultsFromBaseURL(t *testing.T) {
|
|
cases := []struct {
|
|
baseURL string
|
|
want bool
|
|
}{
|
|
{"http://localhost:8080", false},
|
|
{"http://127.0.0.1:8080", false},
|
|
{"http://[::1]:8080", false},
|
|
{"", false}, // no BASE_URL set at all: the local-dev default
|
|
{"https://petal.parodia.dev", true},
|
|
{"http://petal.parodia.dev", true},
|
|
{"https://petal.example.com/", true},
|
|
}
|
|
for _, c := range cases {
|
|
t.Setenv("BASE_URL", c.baseURL)
|
|
t.Setenv("PETAL_REQUIRE_AUTH", "")
|
|
if got := Load().RequireAuth; got != c.want {
|
|
t.Errorf("BASE_URL=%q: RequireAuth=%v, want %v", c.baseURL, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The default is a default, not a rule: a trusted private network is a real
|
|
// deployment shape, and so is wanting the guard on locally.
|
|
func TestRequireAuthExplicitOverride(t *testing.T) {
|
|
t.Setenv("BASE_URL", "https://petal.parodia.dev")
|
|
t.Setenv("PETAL_REQUIRE_AUTH", "false")
|
|
if Load().RequireAuth {
|
|
t.Error("an explicit false must be honoured on a public origin")
|
|
}
|
|
|
|
t.Setenv("BASE_URL", "http://localhost:8080")
|
|
t.Setenv("PETAL_REQUIRE_AUTH", "true")
|
|
if !Load().RequireAuth {
|
|
t.Error("an explicit true must be honoured on localhost")
|
|
}
|
|
|
|
// A typo must not be the thing that disables the guard.
|
|
t.Setenv("BASE_URL", "https://petal.parodia.dev")
|
|
t.Setenv("PETAL_REQUIRE_AUTH", "nope")
|
|
if !Load().RequireAuth {
|
|
t.Error("an unparseable value must keep the default, not read as false")
|
|
}
|
|
}
|