fix(security): synchronize report lifecycle and current roles
Revalidate admin roles from the database and serialize report generation registration with owner-scoped deletion. Refs #9 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
44
backend/internal/auth/admin_current_role_test.go
Normal file
44
backend/internal/auth/admin_current_role_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type lookup struct {
|
||||
user *models.User
|
||||
err error
|
||||
id string
|
||||
}
|
||||
|
||||
func (l *lookup) GetUserByID(id string) (*models.User, error) { l.id = id; return l.user, l.err }
|
||||
func TestAdminOnlyUsesCurrentRoleAndStopsDownstream(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
for _, tc := range []struct {
|
||||
name, tokenRole string
|
||||
user *models.User
|
||||
err error
|
||||
want int
|
||||
}{
|
||||
{"promoted", "user", &models.User{Role: models.RoleAdmin}, nil, 204},
|
||||
{"downgraded", "admin", &models.User{Role: models.RoleUser}, nil, 403},
|
||||
{"deleted", "admin", nil, nil, 401},
|
||||
{"lookup failed", "admin", nil, errors.New("private database failure"), 500},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
repo := &lookup{user: tc.user, err: tc.err}
|
||||
called := false
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) { c.Set("userID", "signed-owner"); c.Set("role", tc.tokenRole) })
|
||||
r.GET("/", AdminOnly(repo), func(c *gin.Context) { called = true; c.Status(204) })
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest("GET", "/", nil))
|
||||
if w.Code != tc.want || called != (tc.want == 204) || repo.id != "signed-owner" {
|
||||
t.Fatalf("status=%d downstream=%v lookup=%q", w.Code, called, repo.id)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@ -26,12 +27,26 @@ func Middleware(secret string) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func AdminOnly() gin.HandlerFunc {
|
||||
type UserLookup interface {
|
||||
GetUserByID(string) (*models.User, error)
|
||||
}
|
||||
|
||||
func AdminOnly(repo UserLookup) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if c.GetString("role") != "admin" {
|
||||
user, err := repo.GetUserByID(c.GetString("userID"))
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "account lookup failed"})
|
||||
return
|
||||
}
|
||||
if user == nil {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
if user.Role != models.RoleAdmin {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "admin only"})
|
||||
return
|
||||
}
|
||||
c.Set("role", string(user.Role))
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user