fix: the backend link show and confirm POSTs ignore a code in the query string
readFields falls back to r.Form, which includes the query string, so POST /backend-link/show?code=... and /confirm?code=... worked and put the code into access logs. Both routes now read the code from the body only (JSON or a form body).
This commit is contained in:
@@ -1165,6 +1165,26 @@ func activationLink(app *backpack.App, id uint, code string) string {
|
||||
return base + "?" + q.Encode()
|
||||
}
|
||||
|
||||
// readBodyFields is readFields without the query string: a JSON body, or a
|
||||
// form-encoded body read from r.PostForm. The routes that carry a secret in
|
||||
// a field use it, so the secret is never taken from a URL that access logs
|
||||
// record.
|
||||
func readBodyFields(r *http.Request) (map[string]any, error) {
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/json") {
|
||||
return readFields(r)
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fields := map[string]any{}
|
||||
for k, v := range r.PostForm {
|
||||
if len(v) > 0 {
|
||||
fields[k] = v[0]
|
||||
}
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
func readFields(r *http.Request) (map[string]any, error) {
|
||||
ct := r.Header.Get("Content-Type")
|
||||
if strings.Contains(ct, "application/json") {
|
||||
|
||||
@@ -41,10 +41,11 @@ func BackendLinkShow(app *backpack.App) http.HandlerFunc {
|
||||
|
||||
// BackendLinkShowPost serves POST /_user/api/v1/backend-link/show: body
|
||||
// {"code": ...}, the same answers as BackendLinkShow. The code travels in
|
||||
// the body, so it stays out of the URLs servers and proxies log.
|
||||
// the body, so it stays out of the URLs servers and proxies log; a code in
|
||||
// the query string is ignored.
|
||||
func BackendLinkShowPost(app *backpack.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
fields, err := readFields(r)
|
||||
fields, err := readBodyFields(r)
|
||||
if err != nil {
|
||||
writeNoStoreJSON(w, http.StatusNotFound, map[string]any{"error": "not_found"})
|
||||
return
|
||||
@@ -85,6 +86,7 @@ func writeNoStoreJSON(w http.ResponseWriter, status int, v any) {
|
||||
// token without auth_time (minted before auth_time existed, or refreshed from
|
||||
// one), answers 409 with the stale-session error, an account already linked
|
||||
// on either side 409 already_linked, an unknown, used or expired code 404.
|
||||
// The code is read from the body only; one in the query string is ignored.
|
||||
func BackendLinkConfirm(app *backpack.App) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
principal, ok := bouncer.User(r.Context())
|
||||
@@ -108,7 +110,7 @@ func BackendLinkConfirm(app *backpack.App) http.HandlerFunc {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"error": "stale_session"})
|
||||
return
|
||||
}
|
||||
fields, err := readFields(r)
|
||||
fields, err := readBodyFields(r)
|
||||
if err != nil {
|
||||
linkNotFound(w)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user