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>
153 lines
5.1 KiB
Go
153 lines
5.1 KiB
Go
package db_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/owncord/server/db"
|
|
)
|
|
|
|
// newBackupTestDB opens a file-backed database suitable for VACUUM INTO tests.
|
|
// VACUUM INTO requires a file-backed source database; :memory: produces an
|
|
// empty-but-valid backup file which is sufficient for validation tests.
|
|
func newBackupFileDB(t *testing.T) (*db.DB, string) {
|
|
t.Helper()
|
|
tmpDir := t.TempDir()
|
|
dbPath := filepath.Join(tmpDir, "source.db")
|
|
|
|
database, err := db.Open(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("db.Open: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = database.Close() })
|
|
|
|
migrFS := fstest.MapFS{
|
|
"001_schema.sql": {Data: adminTestSchema},
|
|
}
|
|
if err := db.MigrateFS(database, migrFS); err != nil {
|
|
t.Fatalf("MigrateFS: %v", err)
|
|
}
|
|
return database, tmpDir
|
|
}
|
|
|
|
// ─── BackupToSafe path-validation tests ─────────────────────────────────────
|
|
|
|
// TestBackupToSafe_ValidPath verifies a properly-named backup file is created.
|
|
func TestBackupToSafe_ValidPath(t *testing.T) {
|
|
database, tmpDir := newBackupFileDB(t)
|
|
|
|
backupDir := filepath.Join(tmpDir, "backups")
|
|
if err := os.MkdirAll(backupDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
backupPath := filepath.Join(backupDir, "chatserver_20260315_120000.db")
|
|
if err := database.BackupToSafe(context.Background(), backupPath, backupDir); err != nil {
|
|
t.Fatalf("BackupToSafe() with valid path returned error: %v", err)
|
|
}
|
|
|
|
info, err := os.Stat(backupPath)
|
|
if err != nil {
|
|
t.Fatalf("backup file does not exist after BackupToSafe: %v", err)
|
|
}
|
|
if info.Size() == 0 {
|
|
t.Error("backup file is empty, expected non-empty SQLite file")
|
|
}
|
|
}
|
|
|
|
// TestBackupToSafe_RejectsPathOutsideRoot ensures a path outside the safe root
|
|
// is rejected.
|
|
func TestBackupToSafe_RejectsPathOutsideRoot(t *testing.T) {
|
|
database, tmpDir := newBackupFileDB(t)
|
|
|
|
backupDir := filepath.Join(tmpDir, "backups")
|
|
if err := os.MkdirAll(backupDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
// Try to write outside backupDir
|
|
escapePath := filepath.Join(tmpDir, "escaped.db")
|
|
err := database.BackupToSafe(context.Background(), escapePath, backupDir)
|
|
if err == nil {
|
|
t.Error("BackupToSafe() should reject path outside safe root, got nil")
|
|
}
|
|
}
|
|
|
|
// TestBackupToSafe_RejectsSingleQuote ensures a path containing a single-quote
|
|
// is rejected before the SQL is executed (prevents SQL injection).
|
|
func TestBackupToSafe_RejectsSingleQuote(t *testing.T) {
|
|
database, tmpDir := newBackupFileDB(t)
|
|
|
|
backupDir := filepath.Join(tmpDir, "backups")
|
|
if err := os.MkdirAll(backupDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
malicious := filepath.Join(backupDir, "evil'.db")
|
|
err := database.BackupToSafe(context.Background(), malicious, backupDir)
|
|
if err == nil {
|
|
t.Error("BackupToSafe() with single-quote in path should return error, got nil")
|
|
}
|
|
}
|
|
|
|
// TestBackupToSafe_RejectsSemicolon ensures a semicolon in the path is rejected.
|
|
func TestBackupToSafe_RejectsSemicolon(t *testing.T) {
|
|
database, tmpDir := newBackupFileDB(t)
|
|
|
|
backupDir := filepath.Join(tmpDir, "backups")
|
|
if err := os.MkdirAll(backupDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
malicious := filepath.Join(backupDir, "evil;drop.db")
|
|
err := database.BackupToSafe(context.Background(), malicious, backupDir)
|
|
if err == nil {
|
|
t.Error("BackupToSafe() with semicolon in path should return error, got nil")
|
|
}
|
|
}
|
|
|
|
// TestBackupToSafe_RejectsSQLComment ensures a path containing "--" is rejected.
|
|
func TestBackupToSafe_RejectsSQLComment(t *testing.T) {
|
|
database, tmpDir := newBackupFileDB(t)
|
|
|
|
backupDir := filepath.Join(tmpDir, "backups")
|
|
if err := os.MkdirAll(backupDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
malicious := filepath.Join(backupDir, "evil--comment.db")
|
|
err := database.BackupToSafe(context.Background(), malicious, backupDir)
|
|
if err == nil {
|
|
t.Error("BackupToSafe() with '--' in path should return error, got nil")
|
|
}
|
|
}
|
|
|
|
// TestBackupToSafe_RejectsNullByte ensures a path containing a null byte is rejected.
|
|
func TestBackupToSafe_RejectsNullByte(t *testing.T) {
|
|
database, tmpDir := newBackupFileDB(t)
|
|
|
|
backupDir := filepath.Join(tmpDir, "backups")
|
|
if err := os.MkdirAll(backupDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
malicious := filepath.Join(backupDir, "evil\x00.db") //nolint:gocritic // intentional null byte for security test
|
|
err := database.BackupToSafe(context.Background(), malicious, backupDir)
|
|
if err == nil {
|
|
t.Error("BackupToSafe() with null byte in path should return error, got nil")
|
|
}
|
|
}
|
|
|
|
// TestBackupToSafe_RejectsDoubleQuote ensures a path containing a double-quote
|
|
// is rejected.
|
|
func TestBackupToSafe_RejectsDoubleQuote(t *testing.T) {
|
|
database, tmpDir := newBackupFileDB(t)
|
|
|
|
backupDir := filepath.Join(tmpDir, "backups")
|
|
if err := os.MkdirAll(backupDir, 0o755); err != nil {
|
|
t.Fatalf("MkdirAll: %v", err)
|
|
}
|
|
malicious := filepath.Join(backupDir, `evil".db`)
|
|
err := database.BackupToSafe(context.Background(), malicious, backupDir)
|
|
if err == nil {
|
|
t.Error("BackupToSafe() with double-quote in path should return error, got nil")
|
|
}
|
|
}
|