fix(web): inject CSRF meta on custom login pages

Site-owned GET /login no longer serves the framework form; stamp
csrf-token + session cookie on page render so JS can still POST /login.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-18 10:49:56 +08:00
1 parent fb30cd3db3
commit bfea2204eb
3 files changed
+46 -3

No files matched your search

+19 -1
View File
@@ -236,7 +236,25 @@ git push https://proxy.cflmy.top/github.com/cflmy/marqdo.git vVER
Short GETs via the proxy often work; **long `git push` may still time out**. If push hangs or drops, skip Clash retries and go to **HK SSH jump** below.
`gh` against `api.github.com` may still need a direct path or HK jump (the reverse proxy is for `github.com` git, not always the API).
`gh` / Release API can use the same path reverse-proxy:
| Path prefix | Upstream |
|-------------|----------|
| `/github.com/` | `https://github.com/` |
| `/api.github.com/` | `https://api.github.com/` |
| `/uploads.github.com/` | `https://uploads.github.com/` (Release asset POST; OpenResty on HK) |
```bash
# Upload a Release asset through proxy.cflmy.top (Content-Type: application/octet-stream)
curl -X POST \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "Content-Type: application/octet-stream" \
--data-binary @dist/FILE.zip \
"https://proxy.cflmy.top/uploads.github.com/repos/cflmy/marqdo/releases/RELEASE_ID/assets?name=FILE.zip"
```
Do **not** rewrite `Host: uploads.github.com` against `proxy.cflmy.top` without the `/uploads.github.com/` path prefix — that returns 404.
### Diagnose
+24 -2
View File
@@ -310,15 +310,37 @@ func (st *state) preparePage(page map[string]any, r *http.Request) map[string]an
return p
}
// attachCSRF stamps page._csrf and returns Set-Cookie when auth is enabled so
// custom login/register pages (that own GET /login) still expose a token for JS.
func (st *state) attachCSRF(page map[string]any, r *http.Request) *string {
if page == nil || r == nil {
return nil
}
if st.auth.users == nil && !st.auth.rbac {
return nil
}
_, csrf, setCookie := resolveSession(r)
if csrf != "" {
page["_csrf"] = csrf
}
return setCookie
}
func (st *state) writePage(w http.ResponseWriter, r *http.Request, page map[string]any) {
html := render.RenderPage(st.preparePage(page, r), st.dbURL, "")
p := st.preparePage(page, r)
setCookie := st.attachCSRF(p, r)
html := render.RenderPage(p, st.dbURL, "")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
appendSetCookie(w, setCookie)
_, _ = io.WriteString(w, html)
}
func (st *state) writePart(w http.ResponseWriter, r *http.Request, page map[string]any, id string) {
html := render.RenderPage(st.preparePage(page, r), st.dbURL, id)
p := st.preparePage(page, r)
setCookie := st.attachCSRF(p, r)
html := render.RenderPage(p, st.dbURL, id)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
appendSetCookie(w, setCookie)
_, _ = io.WriteString(w, html)
}
+3
View File
@@ -731,6 +731,9 @@ func headHTML(page map[string]any, defaultTitle string) string {
}
var s strings.Builder
s.WriteString(fmt.Sprintf("<title>%s</title>", esc(pageTitle)))
if csrf, ok := page["_csrf"].(string); ok && strings.TrimSpace(csrf) != "" {
s.WriteString(fmt.Sprintf(`<meta name="csrf-token" content="%s"/>`, esc(strings.TrimSpace(csrf))))
}
if meta, ok := page["meta"].(map[string]any); ok {
for k, v := range meta {
if k == "title" {