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:
2026-11-20 21:35:38 +01:00
parent 0ebc531124
commit 609dbff7c1
16 changed files with 668 additions and 35 deletions

View File

@ -5,33 +5,102 @@ import (
"sync"
)
// ReportManager tracks in-flight report goroutines so they can be cancelled.
type operationKey struct{ owner, id string }
type reportOperation struct {
cancel context.CancelFunc
report string
}
type reportGate struct {
mu sync.Mutex
refs int
}
type ReportManager struct {
mu sync.Mutex
cancels map[string]context.CancelFunc
cancels map[operationKey]reportOperation
gates map[operationKey]*reportGate
}
func NewReportManager() *ReportManager {
return &ReportManager{cancels: make(map[string]context.CancelFunc)}
return &ReportManager{cancels: map[operationKey]reportOperation{}, gates: map[operationKey]*reportGate{}}
}
func (m *ReportManager) Register(id string, cancel context.CancelFunc) {
// WithReport serializes existence checks/registration with confirmed deletion.
func (m *ReportManager) WithReport(owner, report string, fn func() error) error {
key := operationKey{owner, report}
m.mu.Lock()
defer m.mu.Unlock()
m.cancels[id] = cancel
g := m.gates[key]
if g == nil {
g = &reportGate{}
m.gates[key] = g
}
g.refs++
m.mu.Unlock()
g.mu.Lock()
defer func() {
g.mu.Unlock()
m.mu.Lock()
g.refs--
if g.refs == 0 {
delete(m.gates, key)
}
m.mu.Unlock()
}()
return fn()
}
func (m *ReportManager) Cancel(id string) {
func (m *ReportManager) Register(owner, operation string, cancel context.CancelFunc) {
m.register(owner, operation, operation, cancel)
}
func (m *ReportManager) register(owner, report, operation string, cancel context.CancelFunc) {
m.mu.Lock()
defer m.mu.Unlock()
if cancel, ok := m.cancels[id]; ok {
cancel()
delete(m.cancels, id)
old, exists := m.cancels[operationKey{owner, operation}]
m.cancels[operationKey{owner, operation}] = reportOperation{cancel, report}
m.mu.Unlock()
if exists {
old.cancel()
}
}
func (m *ReportManager) Remove(id string) {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.cancels, id)
func (m *ReportManager) RegisterForReport(owner, report, operation string, cancel context.CancelFunc, exists func() (bool, error)) error {
err := m.WithReport(owner, report, func() error {
ok, err := exists()
if err != nil {
return err
}
if !ok {
return context.Canceled
}
m.register(owner, report, operation, cancel)
return nil
})
if err != nil {
cancel()
}
return err
}
func (m *ReportManager) Cancel(owner, operation string) {
m.mu.Lock()
work, ok := m.cancels[operationKey{owner, operation}]
delete(m.cancels, operationKey{owner, operation})
m.mu.Unlock()
if ok {
work.cancel()
}
}
func (m *ReportManager) CancelReport(owner, report string) {
callbacks := []context.CancelFunc{}
m.mu.Lock()
for key, work := range m.cancels {
if key.owner == owner && work.report == report {
callbacks = append(callbacks, work.cancel)
delete(m.cancels, key)
}
}
m.mu.Unlock()
for _, cancel := range callbacks {
cancel()
}
}
func (m *ReportManager) Remove(owner, operation string) {
m.mu.Lock()
delete(m.cancels, operationKey{owner, operation})
m.mu.Unlock()
}