mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-09-11 14:00:29 +00:00
Require proxy admin for permissioned endpoints
This commit is contained in:
@@ -691,6 +691,32 @@ func RequirePermission(cfg *config.Config, authorizer auth.Authorizer, action, r
|
||||
return
|
||||
}
|
||||
|
||||
// Check if using proxy auth and if so, verify admin status
|
||||
if cfg.ProxyAuthSecret != "" {
|
||||
if valid, username, isAdmin := CheckProxyAuth(cfg, r); valid {
|
||||
if !isAdmin {
|
||||
// User is authenticated but not an admin
|
||||
log.Warn().
|
||||
Str("ip", r.RemoteAddr).
|
||||
Str("path", r.URL.Path).
|
||||
Str("action", action).
|
||||
Str("resource", resource).
|
||||
Str("username", username).
|
||||
Msg("Non-admin user attempted to access permissioned endpoint")
|
||||
|
||||
// Return forbidden error
|
||||
if strings.HasPrefix(r.URL.Path, "/api/") || strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
w.Write([]byte(`{"error":"Admin privileges required"}`))
|
||||
} else {
|
||||
http.Error(w, "Admin privileges required", http.StatusForbidden)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract user from header (set by CheckAuth) and inject into context
|
||||
username := w.Header().Get("X-Authenticated-User")
|
||||
ctx := r.Context()
|
||||
|
||||
@@ -2063,6 +2063,8 @@ func TestProxyAuthNonAdminDeniedAdminEndpoints(t *testing.T) {
|
||||
{method: http.MethodPost, path: "/api/system/settings/update", body: `{}`},
|
||||
{method: http.MethodPost, path: "/api/security/reset-lockout", body: `{}`},
|
||||
{method: http.MethodPost, path: "/api/security/apply-restart", body: `{}`},
|
||||
{method: http.MethodGet, path: "/api/security/tokens", body: ``},
|
||||
{method: http.MethodDelete, path: "/api/security/tokens/token-1", body: ``},
|
||||
{method: http.MethodPost, path: "/api/security/regenerate-token", body: `{}`},
|
||||
{method: http.MethodPost, path: "/api/security/validate-token", body: `{"token":"abc"}`},
|
||||
{method: http.MethodPost, path: "/api/security/oidc", body: `{}`},
|
||||
@@ -2813,6 +2815,33 @@ func TestPermissionProtectedEndpointsDenyWhenAuthorizerBlocks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPermissionEndpointsRejectProxyNonAdmin(t *testing.T) {
|
||||
prevAuthorizer := auth.GetAuthorizer()
|
||||
auth.SetAuthorizer(&auth.DefaultAuthorizer{})
|
||||
defer auth.SetAuthorizer(prevAuthorizer)
|
||||
|
||||
cfg := newTestConfigWithTokens(t)
|
||||
cfg.ProxyAuthSecret = "proxy-secret"
|
||||
cfg.ProxyAuthUserHeader = "X-Remote-User"
|
||||
cfg.ProxyAuthRoleHeader = "X-Remote-Roles"
|
||||
cfg.ProxyAuthAdminRole = "admin"
|
||||
|
||||
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/security/tokens", nil)
|
||||
req.Header.Set("X-Proxy-Secret", cfg.ProxyAuthSecret)
|
||||
req.Header.Set("X-Remote-User", "viewer-user")
|
||||
req.Header.Set("X-Remote-Roles", "viewer")
|
||||
rec := httptest.NewRecorder()
|
||||
router.Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("expected 403 for non-admin proxy on permissioned endpoint, got %d", rec.Code)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "Admin privileges required") {
|
||||
t.Fatalf("expected admin privilege error, got %q", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRestartRequiresAuthInAPIMode(t *testing.T) {
|
||||
record := newTokenRecord(t, "apply-restart-token-123.12345678", []string{config.ScopeSettingsWrite}, nil)
|
||||
cfg := newTestConfigWithTokens(t, record)
|
||||
|
||||
Reference in New Issue
Block a user