Fix open-redirect via next parameter, remove dead code

This commit is contained in:
2026-08-22 23:45:39 -05:00
parent b4b1ba8470
commit 89e46bf539
4 changed files with 13 additions and 16 deletions
+1 -4
View File
@@ -47,10 +47,7 @@ func (h *Web) handleLogin(w http.ResponseWriter, r *http.Request) {
} }
_ = database.TouchLastLogin(h.DB, user.ID) _ = database.TouchLastLogin(h.DB, user.ID)
if next == "" || next[0] != '/' { http.Redirect(w, r, safeRedirectPath(next), http.StatusFound)
next = "/"
}
http.Redirect(w, r, next, http.StatusFound)
} }
func (h *Web) loginFailed(w http.ResponseWriter, r *http.Request, next string) { func (h *Web) loginFailed(w http.ResponseWriter, r *http.Request, next string) {
+1 -4
View File
@@ -133,10 +133,7 @@ func (h *Web) setFavorite(w http.ResponseWriter, r *http.Request, star bool) {
} }
itemType := r.FormValue("item_type") itemType := r.FormValue("item_type")
itemID := r.FormValue("item_id") itemID := r.FormValue("item_id")
next := r.FormValue("next") next := safeRedirectPath(r.FormValue("next"))
if next == "" || next[0] != '/' {
next = "/"
}
switch itemType { switch itemType {
case "song", "album", "artist": case "song", "album", "artist":
default: default:
-8
View File
@@ -2,7 +2,6 @@ package web
import ( import (
"database/sql" "database/sql"
"time"
"git.noroutes.net/NoRoutes/vibetunes/internal/database" "git.noroutes.net/NoRoutes/vibetunes/internal/database"
) )
@@ -102,10 +101,3 @@ func albumOf(db *sql.DB, s *database.Song) *database.Album {
} }
return album return album
} }
type SessionView struct {
ID string
UserAgent string
IPAddress string
CreatedAt time.Time
}
+11
View File
@@ -36,6 +36,17 @@ func (h *Web) page(w http.ResponseWriter, r *http.Request, name, active, title s
}) })
} }
// safeRedirectPath returns next if it's a same-site relative path, or
// "/" otherwise. Guards against open redirects: a bare "/foo" is fine,
// but "//evil.com" or "https://evil.com" are protocol-relative/absolute
// URLs a browser would follow off-site.
func safeRedirectPath(next string) string {
if next == "" || next[0] != '/' || len(next) > 1 && next[1] == '/' {
return "/"
}
return next
}
func (h *Web) flash(w http.ResponseWriter, r *http.Request, name, active, title, flash, kind string, data any) { func (h *Web) flash(w http.ResponseWriter, r *http.Request, name, active, title, flash, kind string, data any) {
render(w, name, PageData{ render(w, name, PageData{
Active: active, Active: active,