Files
OwnCord/Server/ws/authz_test.go
T
J3vbandClaude Fable 5 6afa9e974c refactor(server): thread context.Context through the db layer and all callers
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>
2026-07-23 17:03:52 +02:00

156 lines
4.8 KiB
Go

package ws_test
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/owncord/server/db"
"github.com/owncord/server/permissions"
"github.com/owncord/server/ws"
)
// ─── Authorization tests for WS channel_focus ───────────────────────────────
// These tests verify that the READ_MESSAGES permission check on channel_focus
// correctly blocks or allows access.
// channelFocusMsg constructs a raw channel_focus WebSocket envelope.
func channelFocusMsg(channelID int64) []byte {
raw, _ := json.Marshal(map[string]any{
"type": "channel_focus",
"payload": map[string]any{
"channel_id": channelID,
},
})
return raw
}
// denyReadOnChannel inserts a channel_override that denies READ_MESSAGES for a
// specific role on a specific channel.
func denyReadOnChannel(t *testing.T, database *db.DB, channelID, roleID int64) {
t.Helper()
_, err := database.ExecContext(context.Background(),
`INSERT INTO channel_overrides (channel_id, role_id, allow, deny) VALUES (?, ?, 0, ?)`,
channelID, roleID, permissions.ReadMessages,
)
if err != nil {
t.Fatalf("denyReadOnChannel: %v", err)
}
}
// TestChannelFocus_AllowedByDefault verifies that a member can focus a channel
// when no override denies READ_MESSAGES (members have it by default).
func TestChannelFocus_AllowedByDefault(t *testing.T) {
hub, database := newHandlerHub(t)
user := seedMemberUser(t, database, "focus-allowed")
chID := seedTestChannel(t, database, "focus-pub")
send := make(chan []byte, 16)
c := ws.NewTestClientWithUser(hub, user, 0, send)
hub.Register(c)
time.Sleep(20 * time.Millisecond)
hub.HandleMessageForTest(c, channelFocusMsg(chID))
time.Sleep(50 * time.Millisecond)
// Should NOT receive a FORBIDDEN error.
msgs := drainChan(send)
for _, m := range msgs {
var env map[string]any
if err := json.Unmarshal(m, &env); err != nil {
continue
}
if env["type"] == "error" {
if payload, ok := env["payload"].(map[string]any); ok {
if payload["code"] == "FORBIDDEN" {
t.Error("member was incorrectly denied channel_focus on accessible channel")
}
}
}
}
}
// TestChannelFocus_DeniedByOverride verifies that channel_focus is rejected
// when READ_MESSAGES is denied via a channel override.
func TestChannelFocus_DeniedByOverride(t *testing.T) {
hub, database := newHandlerHub(t)
user := seedMemberUser(t, database, "focus-denied")
chID := seedTestChannel(t, database, "focus-priv")
// Deny READ_MESSAGES for Member role on this channel.
denyReadOnChannel(t, database, chID, permissions.MemberRoleID)
send := make(chan []byte, 16)
c := ws.NewTestClientWithUser(hub, user, 0, send)
hub.Register(c)
time.Sleep(20 * time.Millisecond)
hub.HandleMessageForTest(c, channelFocusMsg(chID))
time.Sleep(50 * time.Millisecond)
code := receiveErrorCode(send, 300*time.Millisecond)
if code != "FORBIDDEN" {
t.Errorf("expected FORBIDDEN error for denied channel_focus, got %q", code)
}
}
// TestChannelFocus_AdminBypassesDeny verifies that an Owner/Admin can focus
// any channel regardless of deny overrides.
func TestChannelFocus_AdminBypassesDeny(t *testing.T) {
hub, database := newHandlerHub(t)
user := seedOwnerUser(t, database, "focus-owner")
chID := seedTestChannel(t, database, "focus-priv2")
// Deny READ_MESSAGES for all non-admin roles.
denyReadOnChannel(t, database, chID, permissions.MemberRoleID)
send := make(chan []byte, 16)
c := ws.NewTestClientWithUser(hub, user, 0, send)
hub.Register(c)
time.Sleep(20 * time.Millisecond)
hub.HandleMessageForTest(c, channelFocusMsg(chID))
time.Sleep(50 * time.Millisecond)
// Should NOT receive a FORBIDDEN error.
msgs := drainChan(send)
for _, m := range msgs {
var env map[string]any
if err := json.Unmarshal(m, &env); err != nil {
continue
}
if env["type"] == "error" {
if payload, ok := env["payload"].(map[string]any); ok {
if payload["code"] == "FORBIDDEN" {
t.Error("admin was incorrectly denied channel_focus")
}
}
}
}
}
// TestChatSend_DeniedWithoutSendMessages verifies that chat_send is rejected
// when READ_MESSAGES or SEND_MESSAGES is denied via a channel override.
func TestChatSend_DeniedWithoutSendMessages(t *testing.T) {
hub, database := newHandlerHub(t)
user := seedMemberUser(t, database, "send-denied")
chID := seedTestChannel(t, database, "send-priv")
// Deny READ_MESSAGES (which also blocks SEND as both are required).
denyReadOnChannel(t, database, chID, permissions.MemberRoleID)
send := make(chan []byte, 16)
c := ws.NewTestClientWithUser(hub, user, chID, send)
hub.Register(c)
time.Sleep(20 * time.Millisecond)
hub.HandleMessageForTest(c, chatSendMsg(chID, "should be rejected"))
time.Sleep(50 * time.Millisecond)
code := receiveErrorCode(send, 300*time.Millisecond)
if code != "FORBIDDEN" {
t.Errorf("expected FORBIDDEN error for denied chat_send, got %q", code)
}
}