Close the door the edge gate used to hold
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
This commit is contained in:
+71
-4
@@ -287,7 +287,14 @@ func mdBlock(n pmNode, depth int) string {
|
||||
return "---"
|
||||
case "image":
|
||||
alt := n.attrStr("alt")
|
||||
return fmt.Sprintf("", alt, n.attrStr("src"))
|
||||
src := safeURL(n.attrStr("src"))
|
||||
if src == "" {
|
||||
// Nowhere safe to point. Keep the alt text as plain prose — it is
|
||||
// the part that carries meaning — rather than emitting an image
|
||||
// whose destination was rejected. See safeURL.
|
||||
return alt
|
||||
}
|
||||
return fmt.Sprintf("", alt, src)
|
||||
case "table":
|
||||
return mdTable(n)
|
||||
case "bulletList", "orderedList":
|
||||
@@ -396,7 +403,9 @@ func applyMdMarks(n pmNode) string {
|
||||
if n.hasMark("underline") {
|
||||
t = "<u>" + t + "</u>"
|
||||
}
|
||||
if href := n.markAttr("link", "href"); href != "" {
|
||||
// Same rule as the HTML export: plenty of Markdown renderers pass a
|
||||
// `javascript:` destination straight through into an <a href>. See safeURL.
|
||||
if href := safeURL(n.markAttr("link", "href")); href != "" {
|
||||
t = "[" + t + "](" + href + ")"
|
||||
}
|
||||
return t
|
||||
@@ -518,7 +527,16 @@ func htmlBlock(n pmNode) string {
|
||||
return "<hr>\n"
|
||||
case "image":
|
||||
alt := htmlEscape(n.attrStr("alt"))
|
||||
return fmt.Sprintf("<p><img src=\"%s\" alt=\"%s\"></p>\n", htmlEscape(n.attrStr("src")), alt)
|
||||
src := safeURL(n.attrStr("src"))
|
||||
if src == "" {
|
||||
// Nowhere safe to point: keep the alt text, which is the part that
|
||||
// carries meaning, rather than emitting a broken image.
|
||||
if alt == "" {
|
||||
return ""
|
||||
}
|
||||
return "<p>" + alt + "</p>\n"
|
||||
}
|
||||
return fmt.Sprintf("<p><img src=\"%s\" alt=\"%s\"></p>\n", htmlEscape(src), alt)
|
||||
case "table":
|
||||
return htmlTable(n)
|
||||
case "bulletList", "orderedList":
|
||||
@@ -617,7 +635,9 @@ func applyHTMLMarks(n pmNode) string {
|
||||
if n.hasMark("highlight") {
|
||||
t = "<mark>" + t + "</mark>"
|
||||
}
|
||||
if href := n.markAttr("link", "href"); href != "" {
|
||||
// An unsafe href is dropped, not the link: the words stay, they just stop
|
||||
// being clickable. See safeURL.
|
||||
if href := safeURL(n.markAttr("link", "href")); href != "" {
|
||||
t = fmt.Sprintf("<a href=\"%s\">%s</a>", htmlEscape(href), t)
|
||||
}
|
||||
return t
|
||||
@@ -858,6 +878,53 @@ func htmlEscape(s string) string {
|
||||
return r.Replace(s)
|
||||
}
|
||||
|
||||
// safeURLSchemes are the schemes an exported document may point at. Escaping
|
||||
// makes a URL safe to sit inside an attribute; it says nothing about what
|
||||
// happens when the attribute is followed, and `javascript:` survives it
|
||||
// untouched.
|
||||
//
|
||||
// The toolbar can't produce one — it prefixes anything it doesn't recognise
|
||||
// with https:// — but the toolbar is not the only way in: PUT /api/docs/{id}
|
||||
// stores whatever Tiptap JSON it is given. And an export is the one artifact
|
||||
// here that is *meant* to leave: the passport and the .html backup are files a
|
||||
// writer hands to a teacher or an editor, opened on a machine that has no
|
||||
// reason to trust them. A link that runs code when clicked is not something to
|
||||
// ship inside one.
|
||||
//
|
||||
// Relative and fragment links pass through: they're how a document refers to
|
||||
// its own headings, and they can't reach anything.
|
||||
var safeURLSchemes = map[string]bool{
|
||||
"http": true, "https": true, "mailto": true, "tel": true, "ftp": true,
|
||||
}
|
||||
|
||||
// safeURL returns u if it is safe to follow from an exported file, and "" if it
|
||||
// isn't. A dropped href leaves the link text in place — the reader loses a
|
||||
// destination, never the writing.
|
||||
func safeURL(u string) string {
|
||||
trimmed := strings.TrimSpace(u)
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
// A scheme is everything before the first ':', but only when no '/', '?' or
|
||||
// '#' comes first — otherwise "notes/a:b" would read as the "notes/a" scheme.
|
||||
// Nothing before a colon means a relative or fragment link, which is fine.
|
||||
if i := strings.IndexAny(trimmed, ":/?#"); i >= 0 && trimmed[i] == ':' {
|
||||
// Control characters and whitespace are stripped by browsers *before*
|
||||
// the scheme is read, so "java\nscript:" is javascript:. Fold them out
|
||||
// before deciding rather than after.
|
||||
scheme := strings.Map(func(r rune) rune {
|
||||
if r <= ' ' || r == 0x7f {
|
||||
return -1
|
||||
}
|
||||
return r
|
||||
}, trimmed[:i])
|
||||
if !safeURLSchemes[strings.ToLower(scheme)] {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return trimmed
|
||||
}
|
||||
|
||||
func xmlEscape(s string) string {
|
||||
r := strings.NewReplacer("&", "&", "<", "<", ">", ">", `"`, """)
|
||||
return r.Replace(s)
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.parodia.dev/drwily/petal/internal/db"
|
||||
)
|
||||
|
||||
// richDocJSON is a Tiptap document exercising headings, marks, and a list —
|
||||
@@ -243,3 +245,73 @@ func TestExportUnsupportedFormat(t *testing.T) {
|
||||
t.Fatalf("expected 400 for unsupported format, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// Escaping makes a URL safe to sit inside an attribute; it says nothing about
|
||||
// what happens when the attribute is followed. An export is the one artifact
|
||||
// here meant to leave — the file handed to a teacher, opened on a machine with
|
||||
// no reason to trust it — so a destination that runs code is dropped.
|
||||
func TestExportDropsUnsafeLinkSchemes(t *testing.T) {
|
||||
unsafe := []string{
|
||||
"javascript:alert(1)",
|
||||
"JaVaScRiPt:alert(1)",
|
||||
"java\nscript:alert(1)", // browsers strip control characters first
|
||||
" javascript:alert(1)",
|
||||
"data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==",
|
||||
"vbscript:msgbox(1)",
|
||||
}
|
||||
for _, href := range unsafe {
|
||||
if got := safeURL(href); got != "" {
|
||||
t.Errorf("safeURL(%q) = %q, want it dropped", href, got)
|
||||
}
|
||||
}
|
||||
|
||||
safe := []string{
|
||||
"https://example.com/a?b=1#c",
|
||||
"http://example.com",
|
||||
"mailto:her@example.com",
|
||||
"/api/images/abc.png",
|
||||
"#a-heading",
|
||||
"notes/chapter:one.md", // a colon that isn't a scheme
|
||||
}
|
||||
for _, href := range safe {
|
||||
if got := safeURL(href); got != href {
|
||||
t.Errorf("safeURL(%q) = %q, want it kept", href, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// End to end through the renderers: an unsafe href loses its destination, never
|
||||
// its words.
|
||||
func TestRenderedExportsCarryNoScriptURLs(t *testing.T) {
|
||||
doc := db.Document{
|
||||
Title: "Notes",
|
||||
Content: `{"type":"doc","content":[{"type":"paragraph","content":[
|
||||
{"type":"text","text":"click me","marks":[{"type":"link","attrs":{"href":"javascript:alert(1)"}}]}]},
|
||||
{"type":"image","attrs":{"src":"javascript:alert(2)","alt":"a drawing"}}]}`,
|
||||
}
|
||||
|
||||
html, err := renderHTMLFile(doc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(strings.ToLower(string(html)), "javascript:") {
|
||||
t.Fatalf("html export carried a javascript: URL:\n%s", html)
|
||||
}
|
||||
if !strings.Contains(string(html), "click me") {
|
||||
t.Fatal("html export dropped the link text along with the href")
|
||||
}
|
||||
if !strings.Contains(string(html), "a drawing") {
|
||||
t.Fatal("html export dropped the alt text of the rejected image")
|
||||
}
|
||||
|
||||
md, err := renderMarkdown(doc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(strings.ToLower(string(md)), "javascript:") {
|
||||
t.Fatalf("markdown export carried a javascript: URL:\n%s", md)
|
||||
}
|
||||
if !strings.Contains(string(md), "click me") {
|
||||
t.Fatal("markdown export dropped the link text along with the href")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user