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>
141 lines
5.7 KiB
Go
141 lines
5.7 KiB
Go
package ws
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
|
|
"github.com/owncord/server/auth"
|
|
"github.com/owncord/server/db"
|
|
"github.com/owncord/server/permissions"
|
|
"github.com/owncord/server/plugin"
|
|
"github.com/owncord/server/service"
|
|
)
|
|
|
|
// ClientInfo holds a read-only snapshot of client state for V2 handlers.
|
|
// Handlers receive this instead of a mutable *Client pointer, making them
|
|
// easier to test and reason about.
|
|
type ClientInfo struct {
|
|
UserID int64
|
|
Username string
|
|
Avatar *string
|
|
RoleName string
|
|
ReqID string
|
|
VoiceChannelID int64 // 0 if not in a voice channel
|
|
VoiceJoinToken string // opaque join-instance token for the current voice session
|
|
}
|
|
|
|
// ── Per-domain dependency structs ───────────────────────────────────────────
|
|
|
|
// PingDeps holds dependencies for the ping handler.
|
|
type PingDeps struct {
|
|
Limiter *auth.RateLimiter
|
|
}
|
|
|
|
// ChatDeps holds dependencies for chat handlers.
|
|
type ChatDeps struct {
|
|
Limiter *auth.RateLimiter
|
|
MessageSvc *service.MessageService
|
|
}
|
|
|
|
// PresenceDeps holds dependencies for presence, typing, and channel focus handlers.
|
|
type PresenceDeps struct {
|
|
Limiter *auth.RateLimiter
|
|
ChannelSvc *service.ChannelService
|
|
}
|
|
|
|
// ReactionDeps holds dependencies for reaction handlers.
|
|
type ReactionDeps struct {
|
|
MessageSvc *service.MessageService
|
|
}
|
|
|
|
// VoiceTokenGenerator generates LiveKit access tokens. Abstracted so V2
|
|
// handlers can be tested without a real LiveKit server.
|
|
type VoiceTokenGenerator interface {
|
|
GenerateToken(userID int64, username string, channelID int64, voiceJoinToken string, canPublish, canSubscribe, canVideo, canScreenShare bool) (string, error)
|
|
URL() string
|
|
}
|
|
|
|
// KeyHolderChecker reports whether a user is the E2EE key holder for a voice channel.
|
|
type KeyHolderChecker interface {
|
|
IsVoiceKeyHolder(channelID, userID int64) bool
|
|
}
|
|
|
|
// PluginDeps holds dependencies for the chat_command (plugin slash-command)
|
|
// handler. Registry is a getter, not a captured value, because the plugin
|
|
// registry is wired via SetPluginRegistry AFTER NewHub builds the deps; reading
|
|
// it live at dispatch time picks up the late wiring. MessageSvc gates channel
|
|
// broadcasts through the same posting policy as a real message send.
|
|
type PluginDeps struct {
|
|
Registry func() *plugin.Registry
|
|
MessageSvc *service.MessageService
|
|
}
|
|
|
|
// VoiceDeps holds dependencies for voice handlers.
|
|
type VoiceDeps struct {
|
|
DB *db.DB
|
|
Limiter *auth.RateLimiter
|
|
Permissions *permissions.Checker
|
|
LiveKit *LiveKitClient
|
|
TokenGen VoiceTokenGenerator // used by voice_token_refresh V2
|
|
KeyHolder KeyHolderChecker // used by voice_token_refresh V2
|
|
}
|
|
|
|
// ── V2 permission helpers ───────────────────────────────────────────────────
|
|
|
|
// requirePerm checks a channel permission via DB lookups. Returns nil if
|
|
// allowed, or a Result carrying either an INTERNAL error (when the server
|
|
// is misconfigured or a DB lookup fails) or a FORBIDDEN error (when the
|
|
// permission bit is genuinely absent from the user's role). Previously
|
|
// every branch returned FORBIDDEN, which hid operator-visible failures
|
|
// behind a user-facing permission denial.
|
|
func requirePerm(ctx context.Context, database *db.DB, perms *permissions.Checker, userID, channelID, perm int64, label string) *Result {
|
|
if database == nil || perms == nil {
|
|
// Missing dependency is a server bug, not a user ACL outcome. Log
|
|
// here so operators see something even when the client surfaces a
|
|
// generic error.
|
|
slog.Error("ws: requirePerm called with nil dependency",
|
|
"have_database", database != nil, "have_perms", perms != nil, "label", label)
|
|
r := Result{Error: ClientError{Code: ErrCodeInternal, Message: "permission check unavailable"}}
|
|
return &r
|
|
}
|
|
role, err := database.GetRoleForUser(ctx, userID)
|
|
if err != nil {
|
|
slog.Error("ws: requirePerm GetRoleForUser failed",
|
|
"user_id", userID, "channel_id", channelID, "err", err)
|
|
r := Result{Error: ClientError{Code: ErrCodeInternal, Message: "permission check failed"}}
|
|
return &r
|
|
}
|
|
if role == nil {
|
|
// No role row is a genuine ACL outcome (no role == no perms).
|
|
r := Result{Error: ClientError{Code: ErrCodeForbidden, Message: "missing " + label + " permission"}}
|
|
return &r
|
|
}
|
|
if !perms.HasChannelPerm(ctx, role.Permissions, role.ID, channelID, perm) {
|
|
r := Result{Error: ClientError{Code: ErrCodeForbidden, Message: "missing " + label + " permission"}}
|
|
return &r
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// hasPerm checks a channel permission via DB lookups. Returns true if allowed.
|
|
func hasPerm(ctx context.Context, database *db.DB, perms *permissions.Checker, userID, channelID, perm int64) bool {
|
|
if database == nil || perms == nil {
|
|
return false
|
|
}
|
|
role, err := database.GetRoleForUser(ctx, userID)
|
|
if err != nil || role == nil {
|
|
return false
|
|
}
|
|
return perms.HasChannelPerm(ctx, role.Permissions, role.ID, channelID, perm)
|
|
}
|
|
|
|
// ── V2 handler type ─────────────────────────────────────────────────────────
|
|
|
|
// HandlerV2 is the function signature for new-style (pure-ish) handlers.
|
|
// They receive a typed Command, a read-only ClientInfo snapshot, and a
|
|
// domain-specific deps struct (passed as any; handler asserts the concrete type).
|
|
// They return a Result describing what events to emit and any error.
|
|
// TODO: consider replacing `deps any` with generics (HandlerV2[D any]) to get
|
|
// compile-time type safety on deps wiring. Requires reworking the registry map.
|
|
type HandlerV2 func(ctx context.Context, cmd Command, info ClientInfo, deps any) Result
|