Navbar logout as a POST form instead of a GET link
A GET logout can be triggered from any foreign page (img/link) and by link prefetchers. LogoutPath now renders <form method="post"> with a submit button styled like the former link; consumers register the route as POST only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b6a6298353
commit
d0e49b2365
4 files changed
+32
-6
No files matched your search
@@ -178,3 +178,25 @@ func TestThemeBlocksInSync(t *testing.T) {
|
||||
t.Errorf("light and prefers-color-scheme: light blocks differ:\nlight: %v\nsystem: %v", light, system)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLogoutIsPostForm pins the navbar's logout control to a POST form:
|
||||
// a GET link would let any foreign page (an <img>, a link prefetcher)
|
||||
// log the user out.
|
||||
func TestLogoutIsPostForm(t *testing.T) {
|
||||
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
||||
if err != nil {
|
||||
t.Fatalf("NewRenderer: %v", err)
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
data := PageHeader{Title: "Dashboard", BasePath: "/ui", Username: "alice", LogoutPath: "/logout"}
|
||||
if err := r.Page(&buf, "dashboard", data); err != nil {
|
||||
t.Fatalf("Page: %v", err)
|
||||
}
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, `<form method="post" action="/ui/logout"`) {
|
||||
t.Errorf("logout is not a POST form\n---\n%s", out)
|
||||
}
|
||||
if strings.Contains(out, `href="/ui/logout"`) {
|
||||
t.Errorf("logout still rendered as a GET link\n---\n%s", out)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user