diff --git a/README.md b/README.md index d0edd79..78ae8c1 100644 --- a/README.md +++ b/README.md @@ -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 inside the UI. Any other method gets 405. `ApplyAppearance` ignores 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 inside the menu's form. A consumer with its own switch can keep it and just set `Theme`/`Palette` (none does today). diff --git a/appearance.go b/appearance.go index e6dab2c..4e846a3 100644 --- a/appearance.go +++ b/appearance.go @@ -69,11 +69,9 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) { if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, 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 - 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) } if path == "" { @@ -83,6 +81,25 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) { 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 // form (fields "theme", "palette", "back") in cookies and redirects to // back, a BasePath-relative path inside this UI (anything else goes to diff --git a/appearance_test.go b/appearance_test.go index f926a37..b291710 100644 --- a/appearance_test.go +++ b/appearance_test.go @@ -143,7 +143,7 @@ func TestApplyAppearance(t *testing.T) { 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"} { h := PageHeader{BasePath: "/r"} 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) { h := AppearanceHandler("/ui") post := func(form url.Values) *httptest.ResponseRecorder {