Files
vibesonic/internal/web/admin_handlers.go
T
owenandClaude Sonnet 5 789bf91eed Complete Phase 4: security hardening, player/playlist/admin UI, docs
Security:
- New internal/netguard package: podcast feed/enclosure fetching now
  refuses to dial non-public IPs (loopback/RFC1918/link-local/cloud
  metadata/CGNAT), validated per-connection including across redirects,
  closing an SSRF gap where a malicious or compromised feed URL could
  probe the server's internal network. Configurable via
  VIBESONIC_PODCASTS_ALLOW_PRIVATE_NETWORKS for intentional LAN feeds.
- Podcast episode downloads capped at 4GB (was unbounded, disk-exhaustion risk)
- Cover art decoding now rejects images above a 40-megapixel budget
  before full decode (decompression-bomb guard)
- HTTP server sets ReadHeaderTimeout (slowloris hardening)
- Admin folder-scan redirect now validated as same-site instead of
  trusting the raw Referer header (open-redirect fix)
- Legacy Subsonic token/salt auth now uses a constant-time comparison

Web UI completion:
- Persistent player: shuffle and repeat (off/all/one) controls, wired
  into queue advance/prev logic and persisted like other player state
- Admin user-permissions form: adminRole toggle (with a guard against
  demoting the last remaining admin) and per-user music-folder access
  checkboxes, backed by existing schema/enforcement that was previously
  unreachable from the UI
- Playlist M3U8 import/export

Docs: added docs/client-compatibility.md (honest tracking template —
no client testing has been performed), docs/deployment.md,
docs/security.md, docs/backup.md; refreshed README and cross-links;
added a footnote in docs/subsonic-compatibility.md distinguishing
spec-standard from VibeSonic-original OpenSubsonic extensions.

Also added Go fuzz tests for the external-auth CIDR/IP parser and the
LRC lyrics parser.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014r98AtcBbevpscWLsfwVvv
2026-08-23 09:18:17 -05:00

329 lines
9.9 KiB
Go

package web
import (
"context"
"net/http"
"net/url"
"time"
"git.noroutes.net/NoRoutes/vibetunes/internal/auth"
"git.noroutes.net/NoRoutes/vibetunes/internal/database"
)
func (h *Web) handleAdminRoot(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/admin/users", http.StatusFound)
}
// --- Users ---
func (h *Web) handleAdminUsers(w http.ResponseWriter, r *http.Request) {
users, err := database.ListUsers(h.DB)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
h.page(w, r, "admin_users", "admin_users", "Users", users)
}
func (h *Web) handleAdminUserCreate(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
username := r.FormValue("username")
password := r.FormValue("password")
isAdmin := r.FormValue("is_admin") == "1"
if username == "" || len(password) < 8 {
http.Redirect(w, r, "/admin/users", http.StatusFound)
return
}
user, err := database.CreateUser(h.DB, username, "", "", isAdmin)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
if err := auth.SetUserPassword(h.DB, h.EncryptionKey, user.ID, password); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/users", http.StatusFound)
}
// userPermissionsPageData bundles the target user with the data the
// permissions form needs beyond the plain user row: every music folder
// (so the form can render a checkbox per folder) and which of those the
// user is currently restricted to.
type userPermissionsPageData struct {
*database.User
Folders []*database.MusicFolder
AllowedFolders map[string]bool
}
func (h *Web) handleAdminUserPermissions(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
target, err := database.GetUserByID(h.DB, id)
if err != nil {
http.NotFound(w, r)
return
}
folders, err := database.ListMusicFolders(h.DB)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
allowedIDs, err := database.AllowedMusicFolderIDs(h.DB, id)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
allowed := make(map[string]bool, len(allowedIDs))
for _, fid := range allowedIDs {
allowed[fid] = true
}
h.page(w, r, "admin_user_permissions", "admin_users", "Permissions: "+target.Username, userPermissionsPageData{
User: target,
Folders: folders,
AllowedFolders: allowed,
})
}
func (h *Web) handleAdminUserPermissionsUpdate(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
target, err := database.GetUserByID(h.DB, id)
if err != nil {
http.NotFound(w, r)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
get := func(name string) bool { return r.FormValue(name) == "1" }
perms := database.Permissions{
Settings: get("settings"),
Stream: get("stream"),
Download: get("download"),
Upload: get("upload"),
Playlist: get("playlist"),
CoverArt: get("cover_art"),
Comment: get("comment"),
Podcast: get("podcast"),
Share: get("share"),
Jukebox: get("jukebox"),
VideoConversion: get("video_conversion"),
}
if err := database.SetUserPermissions(h.DB, id, perms); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
wantAdmin := get("admin")
if target.IsAdmin && !wantAdmin {
// Refuse to demote the last remaining admin — otherwise the
// instance could end up with no one able to reach /admin/*.
n, err := database.CountAdmins(h.DB)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
if n <= 1 {
http.Error(w, "cannot remove admin role from the last remaining admin", http.StatusBadRequest)
return
}
}
if err := database.SetUserAdmin(h.DB, id, wantAdmin); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
folderIDs := r.Form["music_folders"]
if err := database.SetAllowedMusicFolders(h.DB, id, folderIDs); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/users", http.StatusFound)
}
func (h *Web) handleAdminUserToggleDisabled(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
target, err := database.GetUserByID(h.DB, id)
if err != nil {
http.NotFound(w, r)
return
}
if err := database.SetUserDisabled(h.DB, id, !target.Disabled); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
if !target.Disabled { // just disabled it: revoke sessions
_ = database.DeleteUserSessions(h.DB, id)
}
http.Redirect(w, r, "/admin/users", http.StatusFound)
}
func (h *Web) handleAdminUserDelete(w http.ResponseWriter, r *http.Request) {
current := auth.UserFromContext(r.Context())
id := r.PathValue("id")
if id == current.ID {
http.Error(w, "cannot delete your own account", http.StatusBadRequest)
return
}
if err := database.DeleteUser(h.DB, id); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/users", http.StatusFound)
}
// --- Music folders ---
func (h *Web) handleAdminFolders(w http.ResponseWriter, r *http.Request) {
folders, err := database.ListMusicFolders(h.DB)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
h.page(w, r, "admin_folders", "admin_folders", "Music Folders", folders)
}
func (h *Web) handleAdminFolderCreate(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
name := r.FormValue("name")
path := r.FormValue("path")
kind := r.FormValue("kind")
if kind == "" {
kind = "music"
}
if name == "" || path == "" {
http.Redirect(w, r, "/admin/folders", http.StatusFound)
return
}
folder, err := database.CreateMusicFolder(h.DB, name, path, kind)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
if h.Scanner != nil {
go func() { _ = h.Scanner.ScanFolder(context.Background(), folder.ID) }()
}
http.Redirect(w, r, "/admin/folders", http.StatusFound)
}
func (h *Web) handleAdminFolderToggle(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
folder, err := database.GetMusicFolder(h.DB, id)
if err != nil {
http.NotFound(w, r)
return
}
if err := database.SetMusicFolderEnabled(h.DB, id, !folder.Enabled); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/folders", http.StatusFound)
}
func (h *Web) handleAdminFolderDelete(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if err := database.DeleteMusicFolder(h.DB, id); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/folders", http.StatusFound)
}
func (h *Web) handleAdminFolderScan(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if _, err := database.GetMusicFolder(h.DB, id); err != nil {
http.NotFound(w, r)
return
}
if h.Scanner != nil {
go func() { _ = h.Scanner.ScanFolder(context.Background(), id) }()
}
dest := "/admin/scanner"
if referer := r.Header.Get("Referer"); referer != "" {
if u, err := url.Parse(referer); err == nil {
if safe := safeRedirectPath(u.Path); safe != "/" {
dest = safe
}
}
}
http.Redirect(w, r, dest, http.StatusFound)
}
// --- Scanner status ---
type folderScanStatus struct {
Folder *database.MusicFolder
State *database.ScanState
}
func (h *Web) handleAdminScanner(w http.ResponseWriter, r *http.Request) {
folders, err := database.ListMusicFolders(h.DB)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
var out []folderScanStatus
for _, f := range folders {
state, err := database.GetScanState(h.DB, f.ID)
if err != nil {
continue
}
out = append(out, folderScanStatus{Folder: f, State: state})
}
h.page(w, r, "admin_scanner", "admin_scanner", "Scanner", out)
}
// --- System settings ---
func (h *Web) handleAdminSettings(w http.ResponseWriter, r *http.Request) {
h.renderAdminSettings(w, r, "", "")
}
func (h *Web) renderAdminSettings(w http.ResponseWriter, r *http.Request, flash, kind string) {
trusted := ""
for i, p := range h.Config.ExtAuth.TrustedSources {
if i > 0 {
trusted += ", "
}
trusted += p.String()
}
h.flash(w, r, "admin_settings", "admin_settings", "System Settings", flash, kind, struct {
ScanInterval string
FFmpegPath string
FFprobePath string
ExtAuthEnabled bool
ExtAuthTrustedSources string
ExtAuthHeader string
}{
ScanInterval: database.GetSetting(h.DB, "scan_interval", h.Config.ScanInterval.String()),
FFmpegPath: database.GetSetting(h.DB, "ffmpeg_path", h.Config.FFmpegPath),
FFprobePath: database.GetSetting(h.DB, "ffprobe_path", h.Config.FFprobePath),
ExtAuthEnabled: h.Config.ExtAuth.Enabled,
ExtAuthTrustedSources: trusted,
ExtAuthHeader: h.Config.ExtAuth.UserHeader,
})
}
func (h *Web) handleAdminSettingsUpdate(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
interval := r.FormValue("scan_interval")
if _, err := time.ParseDuration(interval); err != nil {
h.renderAdminSettings(w, r, "Invalid scan interval: "+err.Error(), "danger")
return
}
_ = database.SetSetting(h.DB, "scan_interval", interval)
_ = database.SetSetting(h.DB, "ffmpeg_path", r.FormValue("ffmpeg_path"))
_ = database.SetSetting(h.DB, "ffprobe_path", r.FormValue("ffprobe_path"))
h.renderAdminSettings(w, r, "Settings saved. Some changes take effect after a restart.", "success")
}