mirror of
https://github.com/R0m1k3/FlowReader.git
synced 2026-10-11 17:28:05 +02:00
- XSS stocké (critique) : sanitisation bluemonday sur tous les endpoints d'articles (List, ListByFeed, Favorites, Search, Get), pas seulement Get - SSRF (élevé) : nouveau utils/safehttp.go (ValidateExternalURL + client durci via Dialer.Control, bloque IP privées/loopback/link-local/metadata, anti-DNS-rebinding et limite de redirections) appliqué à l'extracteur et au parser - WebSocket CSWSH (élevé) : politique same-origin + override WS_ALLOWED_ORIGINS - rate-limiting (moyen) : token-bucket en mémoire sur /auth/* - token de session retiré du corps JSON (json:"-"), livré uniquement par le cookie HttpOnly - cookie Secure correct derrière un reverse-proxy (X-Forwarded-Proto + override COOKIE_SECURE) - admin : interdiction de supprimer son propre compte - en-têtes de sécurité (nosniff, X-Frame-Options, Referrer-Policy, Permissions-Policy) Note : backend non compilé localement (pas de toolchain Go) ; à valider via Docker. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/google/uuid"
|
|
"github.com/michael/flowreader/internal/domain"
|
|
"github.com/michael/flowreader/internal/service"
|
|
)
|
|
|
|
// AdminHandler handles administrative tasks.
|
|
type AdminHandler struct {
|
|
userRepo domain.UserRepository
|
|
authService *service.AuthService
|
|
}
|
|
|
|
// NewAdminHandler creates a new admin handler.
|
|
func NewAdminHandler(userRepo domain.UserRepository, authService *service.AuthService) *AdminHandler {
|
|
return &AdminHandler{
|
|
userRepo: userRepo,
|
|
authService: authService,
|
|
}
|
|
}
|
|
|
|
// ListUsers handles GET /api/v1/admin/users
|
|
func (h *AdminHandler) ListUsers(w http.ResponseWriter, r *http.Request) {
|
|
users, err := h.userRepo.List()
|
|
if err != nil {
|
|
respondError(w, http.StatusInternalServerError, "Failed to list users")
|
|
return
|
|
}
|
|
|
|
respondJSON(w, http.StatusOK, users)
|
|
}
|
|
|
|
// DeleteUser handles DELETE /api/v1/admin/users/{id}
|
|
func (h *AdminHandler) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
|
userID, err := uuid.Parse(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
respondError(w, http.StatusBadRequest, "Invalid user ID")
|
|
return
|
|
}
|
|
|
|
// Prevent an admin from deleting their own account (lockout / accidental
|
|
// self-removal). The AdminOnly middleware has already verified admin rights.
|
|
if cookie, cErr := r.Cookie("session_id"); cErr == nil {
|
|
if current, _ := h.authService.GetUserByToken(cookie.Value); current != nil && current.ID == userID {
|
|
respondError(w, http.StatusForbidden, "You cannot delete your own account")
|
|
return
|
|
}
|
|
}
|
|
|
|
// Logic to delete user and all associated data
|
|
if err := h.userRepo.Delete(userID); err != nil {
|
|
respondError(w, http.StatusInternalServerError, "Failed to delete user")
|
|
return
|
|
}
|
|
|
|
respondJSON(w, http.StatusOK, map[string]string{"message": "User deleted successfully"})
|
|
}
|
|
|
|
// AdminOnly middleware restricts access to admins.
|
|
func (h *AdminHandler) AdminOnly(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
cookie, err := r.Cookie("session_id")
|
|
if err != nil {
|
|
respondError(w, http.StatusUnauthorized, "Not authenticated")
|
|
return
|
|
}
|
|
|
|
user, err := h.authService.GetUserByToken(cookie.Value)
|
|
if err != nil || user == nil {
|
|
respondError(w, http.StatusUnauthorized, "Invalid session")
|
|
return
|
|
}
|
|
|
|
if user.Role != domain.RoleAdmin {
|
|
respondError(w, http.StatusForbidden, "Admin access required")
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|