mirror of
https://github.com/charmbracelet/crush.git
synced 2025-08-02 05:20:46 +03:00
151 lines
3.9 KiB
Go
151 lines
3.9 KiB
Go
package permission
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"slices"
|
|
"sync"
|
|
|
|
"github.com/charmbracelet/crush/internal/csync"
|
|
"github.com/charmbracelet/crush/internal/pubsub"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var ErrorPermissionDenied = errors.New("permission denied")
|
|
|
|
type CreatePermissionRequest struct {
|
|
SessionID string `json:"session_id"`
|
|
ToolName string `json:"tool_name"`
|
|
Description string `json:"description"`
|
|
Action string `json:"action"`
|
|
Params any `json:"params"`
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
type PermissionRequest struct {
|
|
ID string `json:"id"`
|
|
SessionID string `json:"session_id"`
|
|
ToolName string `json:"tool_name"`
|
|
Description string `json:"description"`
|
|
Action string `json:"action"`
|
|
Params any `json:"params"`
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
type Service interface {
|
|
pubsub.Suscriber[PermissionRequest]
|
|
GrantPersistent(permission PermissionRequest)
|
|
Grant(permission PermissionRequest)
|
|
Deny(permission PermissionRequest)
|
|
Request(opts CreatePermissionRequest) bool
|
|
AutoApproveSession(sessionID string)
|
|
}
|
|
|
|
type permissionService struct {
|
|
*pubsub.Broker[PermissionRequest]
|
|
|
|
workingDir string
|
|
sessionPermissions []PermissionRequest
|
|
sessionPermissionsMu sync.RWMutex
|
|
pendingRequests *csync.Map[string, chan bool]
|
|
autoApproveSessions []string
|
|
autoApproveSessionsMu sync.RWMutex
|
|
skip bool
|
|
allowedTools []string
|
|
}
|
|
|
|
func (s *permissionService) GrantPersistent(permission PermissionRequest) {
|
|
respCh, ok := s.pendingRequests.Get(permission.ID)
|
|
if ok {
|
|
respCh <- true
|
|
}
|
|
|
|
s.sessionPermissionsMu.Lock()
|
|
s.sessionPermissions = append(s.sessionPermissions, permission)
|
|
s.sessionPermissionsMu.Unlock()
|
|
}
|
|
|
|
func (s *permissionService) Grant(permission PermissionRequest) {
|
|
respCh, ok := s.pendingRequests.Get(permission.ID)
|
|
if ok {
|
|
respCh <- true
|
|
}
|
|
}
|
|
|
|
func (s *permissionService) Deny(permission PermissionRequest) {
|
|
respCh, ok := s.pendingRequests.Get(permission.ID)
|
|
if ok {
|
|
respCh <- false
|
|
}
|
|
}
|
|
|
|
func (s *permissionService) Request(opts CreatePermissionRequest) bool {
|
|
if s.skip {
|
|
return true
|
|
}
|
|
|
|
// Check if the tool/action combination is in the allowlist
|
|
commandKey := opts.ToolName + ":" + opts.Action
|
|
if slices.Contains(s.allowedTools, commandKey) || slices.Contains(s.allowedTools, opts.ToolName) {
|
|
return true
|
|
}
|
|
|
|
s.autoApproveSessionsMu.RLock()
|
|
autoApprove := slices.Contains(s.autoApproveSessions, opts.SessionID)
|
|
s.autoApproveSessionsMu.RUnlock()
|
|
|
|
if autoApprove {
|
|
return true
|
|
}
|
|
|
|
dir := filepath.Dir(opts.Path)
|
|
if dir == "." {
|
|
dir = s.workingDir
|
|
}
|
|
permission := PermissionRequest{
|
|
ID: uuid.New().String(),
|
|
Path: dir,
|
|
SessionID: opts.SessionID,
|
|
ToolName: opts.ToolName,
|
|
Description: opts.Description,
|
|
Action: opts.Action,
|
|
Params: opts.Params,
|
|
}
|
|
|
|
s.sessionPermissionsMu.RLock()
|
|
for _, p := range s.sessionPermissions {
|
|
if p.ToolName == permission.ToolName && p.Action == permission.Action && p.SessionID == permission.SessionID && p.Path == permission.Path {
|
|
s.sessionPermissionsMu.RUnlock()
|
|
return true
|
|
}
|
|
}
|
|
s.sessionPermissionsMu.RUnlock()
|
|
|
|
respCh := make(chan bool, 1)
|
|
|
|
s.pendingRequests.Set(permission.ID, respCh)
|
|
defer s.pendingRequests.Del(permission.ID)
|
|
|
|
s.Publish(pubsub.CreatedEvent, permission)
|
|
|
|
// Wait for the response indefinitely
|
|
return <-respCh
|
|
}
|
|
|
|
func (s *permissionService) AutoApproveSession(sessionID string) {
|
|
s.autoApproveSessionsMu.Lock()
|
|
s.autoApproveSessions = append(s.autoApproveSessions, sessionID)
|
|
s.autoApproveSessionsMu.Unlock()
|
|
}
|
|
|
|
func NewPermissionService(workingDir string, skip bool, allowedTools []string) Service {
|
|
return &permissionService{
|
|
Broker: pubsub.NewBroker[PermissionRequest](),
|
|
workingDir: workingDir,
|
|
sessionPermissions: make([]PermissionRequest, 0),
|
|
skip: skip,
|
|
allowedTools: allowedTools,
|
|
pendingRequests: csync.NewMap[string, chan bool](),
|
|
}
|
|
}
|