httpsec package and flash helpers

httpsec: security headers (yamase's set as default), Host gate (empty
Host rejected; active without login or with configured hosts), SafeNext,
CookiePath, SecureCookies. layout: FlashURL/RedirectFlash/ApplyFlash/
WithoutFlash over query parameters, an info kind, AppearanceBack without
the flash parameters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terminarandClaude Opus 5.5 committed 2026-10-31 14:04:45 +01:00
1 parent 7d5feb21a1
commit d6bf6f94d8
8 files changed
+489 -17

No files matched your search

+5 -15
View File
@@ -4,6 +4,8 @@ import (
"net/http"
"net/url"
"strings"
"teruna.dev/yetano/yetano-webui-template/httpsec"
)
// Values for PageHeader.Palette; each has its own block in
@@ -78,7 +80,7 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) {
path = "/"
}
back := url.URL{Path: path, RawQuery: r.URL.RawQuery}
h.AppearanceBack = back.String()
h.AppearanceBack = WithoutFlash(back.String())
}
// AppearanceHandler stores a theme and/or palette choice from a POST
@@ -103,7 +105,7 @@ func AppearanceHandler(basePath string) http.Handler {
return
}
set := func(name, value string) {
http.SetCookie(w, &http.Cookie{Name: name, Value: value, Path: basePath + "/",
http.SetCookie(w, &http.Cookie{Name: name, Value: value, Path: httpsec.CookiePath(basePath),
MaxAge: 365 * 24 * 3600, HttpOnly: true, SameSite: http.SameSiteLaxMode})
}
if theme != "" {
@@ -112,22 +114,10 @@ func AppearanceHandler(basePath string) http.Handler {
if palette != "" {
set(PaletteCookie, palette)
}
http.Redirect(w, r, basePath+localPath(r.FormValue("back")), http.StatusSeeOther)
http.Redirect(w, r, basePath+httpsec.SafeNext(r.FormValue("back"), ""), http.StatusSeeOther)
})
}
// localPath returns back if it is a path (with query) inside this UI,
// otherwise "/".
func localPath(back string) string {
u, err := url.Parse(back)
if err != nil || u.Scheme != "" || u.Host != "" || u.User != nil ||
!strings.HasPrefix(back, "/") || strings.HasPrefix(back, "//") ||
strings.ContainsAny(back, "\\\r\n") {
return "/"
}
return back
}
// AppearanceItem is one button of the rendered appearance menu.
type AppearanceItem struct {
ID, Label, Symbol string