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>
This commit is contained in:
1 parent
0f9b40df9f
commit
28db4f707f
3 files changed
+51
-6
No files matched your search
@@ -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).
|
||||
|
||||
+21
-4
@@ -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
|
||||
|
||||
+25
-1
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user