Compare commits

..
2 Commits
Author SHA1 Message Date
terminarandClaude Opus 5.5 28db4f707f ApplyAppearance: keep a path StripPrefix already made relative
Behind http.StripPrefix the request path is relative to the base path,
so for base path /feeds the page /feeds was cut to / again. A path that
no longer matches RequestURI's is now taken as already stripped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:32:24 +01:00
terminarandClaude Opus 5.5 0f9b40df9f README: every consumer is on v0.3.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 15:11:21 +01:00
3 changed files with 53 additions and 8 deletions

No files matched your search

+7 -3
View File
@@ -98,7 +98,11 @@ the cookies `yw_theme`/`yw_palette` (path `BasePath/`, a year,
`HttpOnly`, `SameSite=Lax`) and redirects to `back`, but only to a path `HttpOnly`, `SameSite=Lax`) and redirects to `back`, but only to a path
inside the UI. Any other method gets 405. `ApplyAppearance` ignores inside the UI. Any other method gets 405. `ApplyAppearance` ignores
unknown cookie values, so whatever the consumer set before stays the unknown cookie values, so whatever the consumer set before stays the
default. A consumer whose middleware wants a CSRF token on every POST default. It works with and without `http.StripPrefix` in front of the
UI: a path that differs from the request's `RequestURI` is taken as
already relative and kept as it is (v0.3.1; up to v0.3.0 the page
`/feeds` behind StripPrefix with base path `/feeds` was wrongly cut to
`/`). A consumer whose middleware wants a CSRF token on every POST
puts the hidden field into `AppearanceFormHTML`, which is rendered puts the hidden field into `AppearanceFormHTML`, which is rendered
inside the menu's form. A consumer with its own switch can keep it and inside the menu's form. A consumer with its own switch can keep it and
just set `Theme`/`Palette` (none does today). just set `Theme`/`Palette` (none does today).
@@ -173,8 +177,8 @@ or 403.
## Migration status ## Migration status
Eight projects use it, all on `v0.2.5` and all with the built-in theme Eight projects use it, all on `v0.3.0` (2026-10-31): the built-in theme
menu (2026-10-30). menu, `httpsec` and the flash helpers.
| Project | Meta-repo | Status | | Project | Meta-repo | Status |
|---|---|---| |---|---|---|
+21 -4
View File
@@ -69,11 +69,9 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) {
if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) { if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) {
h.Palette = c.Value h.Palette = c.Value
} }
// Only a whole leading BasePath segment is cut: behind
// http.StripPrefix the path is already relative, and "/r" must not
// eat the start of "/release-notes".
path := r.URL.Path path := r.URL.Path
if h.BasePath != "" && (path == h.BasePath || strings.HasPrefix(path, h.BasePath+"/")) { if h.BasePath != "" && !strippedPath(r) &&
(path == h.BasePath || strings.HasPrefix(path, h.BasePath+"/")) {
path = strings.TrimPrefix(path, h.BasePath) path = strings.TrimPrefix(path, h.BasePath)
} }
if path == "" { if path == "" {
@@ -83,6 +81,25 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) {
h.AppearanceBack = WithoutFlash(back.String()) h.AppearanceBack = WithoutFlash(back.String())
} }
// strippedPath reports whether r.URL.Path is already relative to the
// UI's base path, i.e. a handler such as http.StripPrefix cut it down
// from what the client asked for. Those keep RequestURI as sent, so a
// path that no longer matches RequestURI's was stripped -- and must not
// be stripped again: for base path "/feeds" the relative page "/feeds"
// is a page of its own, not the start page. Without a RequestURI (a
// request built in code) only a whole leading BasePath segment is cut,
// so "/r" never eats the start of "/release-notes".
func strippedPath(r *http.Request) bool {
if r.RequestURI == "" {
return false
}
u, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
return false
}
return u.Path != r.URL.Path
}
// AppearanceHandler stores a theme and/or palette choice from a POST // AppearanceHandler stores a theme and/or palette choice from a POST
// form (fields "theme", "palette", "back") in cookies and redirects to // form (fields "theme", "palette", "back") in cookies and redirects to
// back, a BasePath-relative path inside this UI (anything else goes to // back, a BasePath-relative path inside this UI (anything else goes to
+25 -1
View File
@@ -143,7 +143,7 @@ func TestApplyAppearance(t *testing.T) {
t.Errorf("AppearanceBack = %q, want %q", h.AppearanceBack, "/tasks?f=1") t.Errorf("AppearanceBack = %q, want %q", h.AppearanceBack, "/tasks?f=1")
} }
// Behind http.StripPrefix the path is already relative to BasePath. // Without StripPrefix only a whole leading BasePath segment is cut.
for path, want := range map[string]string{"/release-notes": "/release-notes", "/r": "/", "/r/x": "/x"} { for path, want := range map[string]string{"/release-notes": "/release-notes", "/r": "/", "/r/x": "/x"} {
h := PageHeader{BasePath: "/r"} h := PageHeader{BasePath: "/r"}
h.ApplyAppearance(httptest.NewRequest(http.MethodGet, path, nil)) h.ApplyAppearance(httptest.NewRequest(http.MethodGet, path, nil))
@@ -153,6 +153,30 @@ func TestApplyAppearance(t *testing.T) {
} }
} }
// Behind http.StripPrefix the path is already relative to BasePath and
// must stay as it is, even when it looks like the base path itself.
func TestApplyAppearanceBehindStripPrefix(t *testing.T) {
cases := []struct{ base, request, want string }{
{"/feeds", "/feeds/feeds", "/feeds"},
{"/feeds", "/feeds/feeds/x?a=1", "/feeds/x?a=1"},
{"/feeds", "/feeds/", "/"},
{"/feeds", "/feeds/items", "/items"},
{"/r", "/r/release-notes", "/release-notes"},
}
for _, c := range cases {
var got string
h := http.StripPrefix(c.base, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hdr := PageHeader{BasePath: c.base}
hdr.ApplyAppearance(r)
got = hdr.AppearanceBack
}))
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, c.request, nil))
if got != c.want {
t.Errorf("base %s, request %s: AppearanceBack = %q, want %q", c.base, c.request, got, c.want)
}
}
}
func TestAppearanceHandler(t *testing.T) { func TestAppearanceHandler(t *testing.T) {
h := AppearanceHandler("/ui") h := AppearanceHandler("/ui")
post := func(form url.Values) *httptest.ResponseRecorder { post := func(form url.Values) *httptest.ResponseRecorder {