diff --git a/README.md b/README.md index b08e4e3..b92523c 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ concurrent-edit conflict during the initial rollout) and can produce a force-move an existing tag again for any future change — always cut a new one, even for a one-line fix. -## PageHeader fields (current as of v0.1.9) +## PageHeader fields (current as of v0.1.10) `Title`, `AppName` (lowercase product name for ``/footer — not `Name`, which collides with a domain type's own `Name` field once @@ -124,9 +124,13 @@ embedded in the same struct, as yakwui's viewmodel types do), `Lang` (`<html lang>`, defaults to `"en"`), `Theme` (`<html data-theme>`, see Theming), `Brand`, `BrandInitial`, `Subtitle`, `BasePath`, `NavItems`/`ActiveNav`, `Flash`/`FlashKind`, `Username`, -`LogoutPath` (a plain GET link — for a POST-form logout or any other -markup mismatch, render it yourself into `NavbarRightExtra` instead and -leave `Username`/`LogoutPath` unset), `NavbarBeforeBrand` (before the +`LogoutPath` (since v0.1.10 a `<form method="post">` with a submit +button styled like the former link, never a GET link: a logout reachable +by GET can be triggered from any foreign page via an `<img>`/link and by +link prefetchers; the consumer registers the route as `POST` only and +answers `GET` with 405 + `Allow: POST`. If the form needs a hidden CSRF +field or other markup, render it yourself into `NavbarRightExtra` and +leave `LogoutPath` unset), `NavbarBeforeBrand` (before the brand link, e.g. a mobile sidebar-toggle button), `NavbarLeftExtra`/`NavbarRightExtra` (the navbar's two groups), `ExtraHeadHTML`/`ExtraBodyHTML`, and `BodyAttrs`/`SidebarAttrs` — diff --git a/layout.go b/layout.go index f3d7068..4122b82 100644 --- a/layout.go +++ b/layout.go @@ -75,7 +75,7 @@ type PageHeader struct { Flash string FlashKind string // "ok" or "error"; meaningless if Flash == "" Username string - LogoutPath string // BasePath-relative, e.g. "/logout"; "" hides the logout link + LogoutPath string // BasePath-relative, e.g. "/logout"; rendered as a POST form (never a GET link); "" hides it NavbarBeforeBrand template.HTML // extra markup rendered before the brand link, e.g. a mobile sidebar toggle NavbarLeftExtra template.HTML // extra markup rendered after Subtitle in the navbar's left group NavbarRightExtra template.HTML // extra markup rendered after Username/Logout in the navbar's right group diff --git a/layout_test.go b/layout_test.go index 3eb95c4..d7fe596 100644 --- a/layout_test.go +++ b/layout_test.go @@ -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) + } +} diff --git a/templates/layout/navbar.html.tmpl b/templates/layout/navbar.html.tmpl index 622aee7..3eda458 100644 --- a/templates/layout/navbar.html.tmpl +++ b/templates/layout/navbar.html.tmpl @@ -9,7 +9,7 @@ </div> <div class="d-flex align-items-center gap-2"> {{if .Username}}<span class="navbar-subtitle">{{.Username}}</span>{{end}} -{{if .LogoutPath}}<a href="{{.BasePath}}{{.LogoutPath}}" class="btn btn-outline-secondary btn-sm">Logout</a>{{end}} +{{if .LogoutPath}}<form method="post" action="{{.BasePath}}{{.LogoutPath}}" class="d-inline m-0"><button type="submit" class="btn btn-outline-secondary btn-sm">Logout</button></form>{{end}} {{.NavbarRightExtra}} </div> </div>