mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
Fixes all 109 golangci-lint findings (106 contextcheck, 1 gocritic,
2 gosec) that accumulated after D2 wired dbgen (whose queries take ctx)
under ctx-less db.DB wrappers while CI lint was quota-dead. No nolint
comments added; every finding fixed by genuinely threading context.
- db: all 138 hand-written db.DB methods take ctx first; the dbCtx()
Background shim is deleted; raw Query/QueryRow/Exec/Begin use their
Context variants; the four redundant ctx-less passthroughs removed.
db.Auditor/WriteAudit gain ctx.
- Seams: permissions.Checker (DB iface, HasChannelPerm,
RequireChannelAccess) and the service.Store interface mirror the new
signatures (ws.EventStore and plugin.PluginStore already did).
- Callers: api/admin handlers use r.Context(); ws per-message paths use
the connection ctx via DispatchV2; hub loops and startup wiring use
context.Background(); service methods thread ctx where they have one
and Background where no ctx exists. Public service surface reached by
ctx-holding chains (PermissionService.HasChannelPerm/GetRoleForUser/
RequireChannelAccess, message/dm/block/invite/profile methods) is now
ctx-first.
- Detached (context.WithoutCancel) where cancellation would break an
invariant, found by a 3-lens adversarial review of the diff:
* voice-leave background retries (a dead webhook/connection ctx killed
retry 2 before it ran, leaving ghost capacity-holding voice rows)
* rollbackVoiceJoin's compensating delete (its trigger IS the cancel)
* post-2FA-change DeleteOtherSessions and logout DeleteSession (the
security tail of a committed change must not die with the request)
* all api/ws audit writes (a banned user could suppress their own
login_blocked_banned row by aborting the request mid-bcrypt)
* admin backup VACUUM INTO (an interrupt left a truncated .db that
the backup list presented as restorable)
* post-commit message/edit refetches (a committed message must still
fan out when the sender disconnects)
* hub settings-cache refresh (one dead connection could pin stale
values for the 30s TTL)
- gocritic rangeValCopy fixed (index iteration); gosec G306 excluded in
config with justification (generated source must stay world-readable)
instead of flipping genprotocol output to 0o600.
Verified: gofmt/vet, all four build-tag variants, full suite, deadlock
pass, full -race pass, golangci-lint 0 issues uncapped.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
126 lines
4.9 KiB
Go
126 lines
4.9 KiB
Go
package permissions
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// ─── Errors ─────────────────────────────────────────────────────────────────
|
|
|
|
// ErrNotDMParticipant is returned when a user is not a participant in a DM channel.
|
|
var ErrNotDMParticipant = errors.New("not a participant in this DM")
|
|
|
|
// ErrPermissionDenied is returned when a user lacks the required permission.
|
|
var ErrPermissionDenied = errors.New("permission denied")
|
|
|
|
// ─── DB interface ───────────────────────────────────────────────────────────
|
|
|
|
// ChannelOverride holds the allow/deny permission bits for a single channel.
|
|
type ChannelOverride struct {
|
|
Allow int64
|
|
Deny int64
|
|
}
|
|
|
|
// ChannelRef is the minimal channel description VisibleChannelIDs needs.
|
|
// Declared here (not imported from db) so the permissions package stays free
|
|
// of a db dependency; callers map their []db.Channel down to []ChannelRef.
|
|
type ChannelRef struct {
|
|
ID int64
|
|
Type string
|
|
}
|
|
|
|
// DB is the minimal database interface the Checker needs.
|
|
// Defined at the consumer (per Go convention: accept interfaces, return structs).
|
|
type DB interface {
|
|
GetChannelPermissions(ctx context.Context, channelID, roleID int64) (allow, deny int64, err error)
|
|
IsDMParticipant(ctx context.Context, userID, channelID int64) (bool, error)
|
|
}
|
|
|
|
// ─── Checker ────────────────────────────────────────────────────────────────
|
|
|
|
// Checker consolidates all channel permission checks into one reusable type.
|
|
// It is safe to share across goroutines because it holds no mutable state.
|
|
type Checker struct {
|
|
db DB
|
|
}
|
|
|
|
// NewChecker creates a Checker backed by the given database interface.
|
|
func NewChecker(db DB) *Checker {
|
|
return &Checker{db: db}
|
|
}
|
|
|
|
// HasChannelPerm reports whether the role (identified by rolePerms and roleID)
|
|
// has all the given permission bits on the specified channel. Administrator
|
|
// roles bypass all checks. Channel overrides (allow/deny) are fetched from the
|
|
// database per call.
|
|
func (ck *Checker) HasChannelPerm(ctx context.Context, rolePerms int64, roleID, channelID, perm int64) bool {
|
|
if HasAdmin(rolePerms) {
|
|
return true
|
|
}
|
|
allow, deny, err := ck.db.GetChannelPermissions(ctx, channelID, roleID)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
effective := EffectivePerms(rolePerms, allow, deny)
|
|
return effective&perm == perm
|
|
}
|
|
|
|
// HasChannelPermBatch reports whether the role has the given permission on the
|
|
// channel using a pre-fetched overrides map. This avoids N+1 queries when
|
|
// filtering many channels in bulk. The zero-value ChannelOverride (no entry in
|
|
// map) is correct -- it means no override exists.
|
|
func (ck *Checker) HasChannelPermBatch(rolePerms int64, overrides map[int64]ChannelOverride, channelID, perm int64) bool {
|
|
if HasAdmin(rolePerms) {
|
|
return true
|
|
}
|
|
o := overrides[channelID] // zero-value (0, 0) when no override exists
|
|
effective := EffectivePerms(rolePerms, o.Allow, o.Deny)
|
|
return effective&perm == perm
|
|
}
|
|
|
|
// VisibleChannelIDs returns the set of non-DM channel IDs the role (identified
|
|
// by rolePerms) may READ, using a pre-fetched overrides map. It is the single
|
|
// predicate behind every "which channels does this role see" site (REST
|
|
// ListVisibleChannels, the ws ready payload, and reconnect replay filtering) so
|
|
// they can never drift apart. DM channels are skipped — their visibility is
|
|
// membership-based, layered on top by the caller. A nil/zero role yields an
|
|
// empty set naturally (no admin bypass, no base READ, and no override map
|
|
// entries to grant it).
|
|
func (ck *Checker) VisibleChannelIDs(rolePerms int64, channels []ChannelRef, overrides map[int64]ChannelOverride) map[int64]bool {
|
|
visible := make(map[int64]bool)
|
|
for _, ch := range channels {
|
|
if ch.Type == "dm" {
|
|
continue
|
|
}
|
|
if ck.HasChannelPermBatch(rolePerms, overrides, ch.ID, ReadMessages) {
|
|
visible[ch.ID] = true
|
|
}
|
|
}
|
|
return visible
|
|
}
|
|
|
|
// RequireChannelAccess checks whether the user can access the channel with the
|
|
// given permission. For DM channels (channelType == "dm"), it verifies
|
|
// participant membership via IsDMParticipant. For regular channels, it checks
|
|
// role-based permissions via HasChannelPerm.
|
|
//
|
|
// Returns nil on success, or a descriptive error on failure.
|
|
func (ck *Checker) RequireChannelAccess(ctx context.Context, userID, rolePerms, roleID int64, channelType string, channelID, perm int64) error {
|
|
if channelType == "dm" {
|
|
ok, err := ck.db.IsDMParticipant(ctx, userID, channelID)
|
|
if err != nil {
|
|
return fmt.Errorf("checking DM participation: %w", err)
|
|
}
|
|
if !ok {
|
|
return ErrNotDMParticipant
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if !ck.HasChannelPerm(ctx, rolePerms, roleID, channelID, perm) {
|
|
return ErrPermissionDenied
|
|
}
|
|
return nil
|
|
}
|