mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
`Server/go.mod` declared `github.com/owncord/server` while the public repository is `github.com/J3vb/OwnCord`. Nothing resolves that path — there is no `owncord` GitHub org and no vanity-import host serving go-import metadata for it — so every import line in the tree named a location that does not exist. It compiles because a main module's own path is never fetched, which is exactly why it went unnoticed. The obvious fix — an AST-aware import rewriter (`gomvpkg`, `go mod edit`) — is wrong here, and provably so. Six of the 722 occurrences are not imports at all: `api/main_test.go:20` (a goleak `IgnoreTopFunction` pattern), `telemetry/metrics.go:17-19` (three OTel instrumentation-scope names), `invariants/syncutil_locks.go:73` (a diagnostic message), and `invariants/syncutil_locks_test.go:56` (an import line inside a raw-string Go fixture). An import rewriter touches none of them, and the compiler cannot see any of them either. Done as one scripted substitution over `git ls-files`, anchored on the full `github.com/owncord/server` string. The anchor matters: `owncord-server` is a different identifier — the OTel `service.name` (`config/config.go`, `telemetry/telemetry_otel.go`) and the GHCR image name (`.github/workflows/release.yml`, `docker-compose.yml`) — and a looser pattern would have moved it. It is untouched: 10 occurrences across 9 files, before and after. 350 files, 728 insertions, 728 deletions. 722 occurrences in 344 Go files, plus `go.mod:1`, the `sed` at `Makefile:67`, `Server/CLAUDE.md:3`, `docs/architecture/server.md:5`, and the ledger pair (`findings-ledger.json:3758` plus a `render-ledger.mjs` re-render of `FINDINGS.md`). Zero in any workflow, zero in the Dockerfile, zero in `Server/.golangci.yml` (no `local-prefixes`, `gci`, `importas` or `depguard` rule keys on the module path, so import grouping is not configured anywhere). The plan's blast-radius estimate missed one thing, and it is the one that would have gone red: **gofmt**. `J` (0x4A) sorts before every lowercase letter, so in the 36 files where a module-local import shares a contiguous group with a third-party one, the module's imports must move above `github.com/go-chi/...`. `gofmt -l` was clean before the substitution and listed exactly 36 files after it; `gofmt -w` on those 36 restores it to clean. `gofmt` is an enforced gate — the `formatters` block in `Server/.golangci.yml`, which is S-05 — so a substitution-only commit fails Lint. Verified: both directions, and the line accounting is exact. Every added line in this diff contains the new module path (728) and every removed line contains the old one (728); the count of changed lines containing neither is **zero**, so the gofmt re-sort moved module-path lines only and touched no third-party import. The residual check (`git ls-files -z | xargs -0 grep -n 'github\.com/owncord/server'`) returns exactly two hits, both deliberately out of scope: the RL-13 row in `docs/audit-2026-08-23-repository-layout.md` and the measurement row in this phase's own plan. The compiler-invisible half was proven by reverting *only* `api/main_test.go:20` to the old path on the otherwise-renamed tree: `go build ./...` and `go vet ./api/` both still pass — they see nothing wrong — while `go test ./api/` FAILS, because the runtime function name now carries the new path and goleak stops ignoring `ws.(*Hub).Run.func1`. Restoring the line makes it pass. `go.sum` is byte-identical (no `go mod tidy` was run and none was needed). All four build-tag variants compile; `go vet ./...`, `go vet -tags otel,wazero ./...` and `go vet -tags deadlock ./...` pass; `go test -race ./...` is 16/16 packages green; `go test -tags deadlock ./...` passes; the tag-gated `./plugin/...` (wazero) and `./telemetry/...` (otel) runs pass. `golangci-lint` v2.11.3 — the pinned CI version, rebuilt locally against Go 1.26 because the packaged binary cannot load a 1.26 config — reports **0 issues**. `go run ./cmd/genprotocol` leaves `git diff --exit-code ws/message_types.go ../Client/src/lib/protocolTypes.ts` clean, so the rename does not reach the generated protocol constants. `npx prettier --check .` and `node .superpowers/render-ledger.mjs --check` pass. Not included: `docs/audit-2026-08-23-repository-layout.md` and `docs/plans/b1-repository-foundation-2026-08-25.md` keep the old path — they are the audit row and the measurement that motivated this change, and rewriting them would erase the record of what was measured. They are why the residual check needs a two-path allowance rather than being empty; that allowance is stated above rather than hidden in a pathspec. `telemetry/metrics.go:19` declares `scopeVoice` for a `Server/voice` package that does not exist; the substitution carried the dead path forward verbatim as `github.com/J3vb/OwnCord/Server/voice` rather than fixing it, because correcting a real observability bug inside a mechanical rename would hide it in a 350-file diff. It needs its own item. No `go.work`, no second module, and no vanity-import host was set up — the new path resolves against the real repository, but nothing imports this module as a library, so `go get` reachability was not exercised either way. Refs RL-13, L-12
343 lines
13 KiB
Go
343 lines
13 KiB
Go
package admin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"math"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/J3vb/OwnCord/Server/db"
|
|
"github.com/J3vb/OwnCord/Server/permissions"
|
|
"github.com/J3vb/OwnCord/Server/service"
|
|
)
|
|
|
|
// ─── User Handlers ───────────────────────────────────────────────────────────
|
|
|
|
func handleGetStats(database *db.DB, hub HubBroadcaster) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
stats, err := database.GetServerStats(r.Context())
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to get stats")
|
|
return
|
|
}
|
|
if hub != nil {
|
|
stats.OnlineCount = hub.ClientCount()
|
|
}
|
|
writeJSON(w, http.StatusOK, stats)
|
|
}
|
|
}
|
|
|
|
func handleListUsers(database *db.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
limit := queryInt(r, "limit", 50, 1, 500)
|
|
offset := queryInt(r, "offset", 0, 0, math.MaxInt32)
|
|
|
|
users, err := database.ListAllUsers(r.Context(), limit, offset)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to list users")
|
|
return
|
|
}
|
|
|
|
safe := make([]adminUserResponse, len(users))
|
|
for i := range users {
|
|
safe[i] = toAdminUserResponse(users[i])
|
|
}
|
|
writeJSON(w, http.StatusOK, safe)
|
|
}
|
|
}
|
|
|
|
// patchUserRequest is the JSON body for PATCH /admin/api/users/{id}.
|
|
type patchUserRequest struct {
|
|
RoleID *int64 `json:"role_id"`
|
|
Banned *bool `json:"banned"`
|
|
BanReason *string `json:"ban_reason"`
|
|
// BanDurationHours makes the ban temporary: it expires this many hours
|
|
// from now (login re-checks via IsEffectivelyBanned). Omitted or 0 =
|
|
// permanent. Only meaningful with banned=true.
|
|
BanDurationHours *int `json:"ban_duration_hours"`
|
|
}
|
|
|
|
// maxBanDurationHours caps temporary bans at one year; anything longer is
|
|
// effectively permanent and should be issued as such.
|
|
const maxBanDurationHours = 24 * 365
|
|
|
|
// memberUnbanBroadcaster is an optional capability of HubBroadcaster: tell
|
|
// every connected client a user is back in the roster after an unban, the
|
|
// mirror of BroadcastMemberBan. It is checked with a type assertion instead
|
|
// of being added to HubBroadcaster directly (admin/types.go, not owned by
|
|
// this change) so this fix does not force every HubBroadcaster
|
|
// implementation — production and test doubles alike — to gain the method
|
|
// before it compiles. See the batch report's cross_batch note: *ws.Hub needs
|
|
// BroadcastMemberUnban(userID int64) wired up for this to take effect at
|
|
// runtime; until then the assertion below simply misses and the handler's
|
|
// existing (pre-fix) behavior is unchanged.
|
|
type memberUnbanBroadcaster interface {
|
|
BroadcastMemberUnban(userID int64)
|
|
}
|
|
|
|
// writeModerationErr maps ModerationService errors onto admin API responses.
|
|
func writeModerationErr(w http.ResponseWriter, err error) {
|
|
switch {
|
|
case errors.Is(err, service.ErrForbidden):
|
|
writeErr(w, http.StatusForbidden, "FORBIDDEN", err.Error())
|
|
case errors.Is(err, service.ErrNotFound):
|
|
writeErr(w, http.StatusNotFound, "NOT_FOUND", "user not found")
|
|
case errors.Is(err, service.ErrBadRequest):
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", err.Error())
|
|
default:
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "moderation action failed")
|
|
}
|
|
}
|
|
|
|
// patchUserPrecheck resolves and validates the target of a
|
|
// PATCH /admin/api/users/{id} before any mutation is attempted. It reports
|
|
// whether the handler may continue; on false it has already written the error
|
|
// response.
|
|
func patchUserPrecheck(w http.ResponseWriter, r *http.Request, database *db.DB) (int64, patchUserRequest, int64, bool) {
|
|
var req patchUserRequest
|
|
|
|
id, err := pathInt64(r, "id")
|
|
if err != nil {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", "invalid user id")
|
|
return 0, req, 0, false
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", "invalid request body")
|
|
return 0, req, 0, false
|
|
}
|
|
|
|
user, err := database.GetUserByID(r.Context(), id)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to fetch user")
|
|
return 0, req, 0, false
|
|
}
|
|
if user == nil {
|
|
writeErr(w, http.StatusNotFound, "NOT_FOUND", "user not found")
|
|
return 0, req, 0, false
|
|
}
|
|
|
|
actor := actorFromContext(r)
|
|
|
|
// Prevent admins from modifying their own role or ban status, which
|
|
// could lock them out of the admin panel with no recovery path.
|
|
if id == actor {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", "cannot modify your own account via admin panel")
|
|
return 0, req, 0, false
|
|
}
|
|
|
|
return id, req, actor, true
|
|
}
|
|
|
|
// patchUserAuthorizeRole runs every ChangeUserRole precondition for a PATCH
|
|
// carrying role_id without committing anything; a request without role_id is
|
|
// a no-op. It reports whether the handler may continue; on false it has
|
|
// already written the error response.
|
|
func patchUserAuthorizeRole(w http.ResponseWriter, r *http.Request, mod *service.ModerationService, actor, id int64, req patchUserRequest) bool {
|
|
if req.RoleID == nil {
|
|
return true
|
|
}
|
|
if mod == nil {
|
|
// Fail closed rather than fall back to an unchecked UPDATE.
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "moderation service unavailable")
|
|
return false
|
|
}
|
|
if _, _, _, err := mod.AuthorizeRoleChange(r.Context(), actor, id, *req.RoleID); err != nil {
|
|
writeModerationErr(w, err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// patchUserApplyBan commits the ban/unban half of the PATCH and fans the
|
|
// result out to connected clients; a request without banned is a no-op. It
|
|
// reports whether the handler may continue; on false it has already written
|
|
// the error response.
|
|
func patchUserApplyBan(w http.ResponseWriter, r *http.Request, hub HubBroadcaster, mod *service.ModerationService, actor, id int64, req patchUserRequest) bool {
|
|
if req.Banned == nil {
|
|
return true
|
|
}
|
|
if mod == nil {
|
|
// Fail closed rather than fall back to an unchecked UPDATE.
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "moderation service unavailable")
|
|
return false
|
|
}
|
|
banReason := ""
|
|
if req.BanReason != nil {
|
|
banReason = *req.BanReason
|
|
}
|
|
var banExpires *time.Time
|
|
if req.BanDurationHours != nil && *req.BanDurationHours != 0 {
|
|
hours := *req.BanDurationHours
|
|
if hours < 0 || hours > maxBanDurationHours {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", "ban_duration_hours must be between 1 and 8760")
|
|
return false
|
|
}
|
|
t := time.Now().Add(time.Duration(hours) * time.Hour)
|
|
banExpires = &t
|
|
}
|
|
var actionErr error
|
|
if *req.Banned {
|
|
actionErr = mod.BanUser(r.Context(), actor, id, banReason, banExpires)
|
|
} else {
|
|
actionErr = mod.UnbanUser(r.Context(), actor, id)
|
|
}
|
|
if actionErr != nil {
|
|
writeModerationErr(w, actionErr)
|
|
return false
|
|
}
|
|
switch {
|
|
case *req.Banned && hub != nil:
|
|
hub.BroadcastMemberBan(id)
|
|
case !*req.Banned && hub != nil:
|
|
// Ban had no WS event on the way out (member_ban hard-deletes
|
|
// the row client-side); unban needs one on the way back in, or
|
|
// every already-connected client keeps the user missing from
|
|
// its member store while a freshly connecting client sees them.
|
|
if mub, ok := hub.(memberUnbanBroadcaster); ok {
|
|
mub.BroadcastMemberUnban(id)
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// patchUserApplyRole commits the role half of the PATCH and fans the result
|
|
// out to connected clients; a request without role_id is a no-op. It reports
|
|
// whether the handler may continue; on false it has already written the error
|
|
// response.
|
|
func patchUserApplyRole(w http.ResponseWriter, r *http.Request, hub HubBroadcaster, permInvalidator PermissionInvalidator, mod *service.ModerationService, actor, id int64, req patchUserRequest) bool {
|
|
if req.RoleID == nil {
|
|
return true
|
|
}
|
|
// Routed through ModerationService, which re-runs the same
|
|
// MANAGE_ROLES, actor-outranks-target, and assign-below-own-rank
|
|
// checks the AuthorizeRoleChange pre-flight above already passed
|
|
// (a second pass, not a redundant one: it catches anything that
|
|
// changed in the window between the pre-flight and here, e.g. a
|
|
// concurrent role delete), then commits and writes the audit row.
|
|
if mod == nil {
|
|
// Fail closed rather than fall back to an unchecked UPDATE.
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "moderation service unavailable")
|
|
return false
|
|
}
|
|
newRole, err := mod.ChangeUserRole(r.Context(), actor, id, *req.RoleID)
|
|
if err != nil {
|
|
writeModerationErr(w, err)
|
|
return false
|
|
}
|
|
if permInvalidator != nil {
|
|
permInvalidator.InvalidateUser(id)
|
|
}
|
|
// Use the role ChangeUserRole already loaded and validated rather
|
|
// than re-reading it: a re-read can race a concurrent role delete
|
|
// (or a transient read error) and silently skip this whole
|
|
// fan-out, leaving the demoted user's socket subscribed to
|
|
// channels it can no longer read (OC-0045). The role change
|
|
// itself already committed, so the fan-out must not be
|
|
// conditional on anything past that point.
|
|
if hub != nil {
|
|
hub.BroadcastMemberUpdate(id, newRole.Name)
|
|
// BroadcastMemberUpdate only revokes subscriptions the new
|
|
// role can no longer read (hub_broadcast.go's
|
|
// revokeUnreadableChannels); it never grants the ones the
|
|
// new role newly gained READ_MESSAGES on. Without this,
|
|
// a promoted user's sidebar is missing channels until
|
|
// their next reconnect, unlike a role permission edit or
|
|
// a role delete, which both re-derive visibility fully.
|
|
hub.RefreshAllChannelVisibility()
|
|
}
|
|
return true
|
|
}
|
|
|
|
func handlePatchUser(database *db.DB, hub HubBroadcaster, permInvalidator PermissionInvalidator, mod *service.ModerationService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id, req, actor, ok := patchUserPrecheck(w, r, database)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
// Authorize the role change before applying anything else. Without
|
|
// this pre-flight, a PATCH combining banned + role_id would commit
|
|
// and broadcast the ban first and only then attempt the role change:
|
|
// if that role change was then refused (missing MANAGE_ROLES, or the
|
|
// new role outranks the actor), the handler reported the whole
|
|
// request as failed while the target was in fact banned, audited,
|
|
// and already dropped from every connected client's member list
|
|
// (OC-0215). Running every ChangeUserRole precondition up front,
|
|
// before either mutation lands, keeps the PATCH all-or-nothing from
|
|
// the caller's perspective.
|
|
if !patchUserAuthorizeRole(w, r, mod, actor, id, req) {
|
|
return
|
|
}
|
|
|
|
// Ban/unban first: it routes through ModerationService, which enforces
|
|
// BAN_MEMBERS + role hierarchy (the admin-auth perimeter alone does
|
|
// not — any admin-panel actor could previously ban the owner). The
|
|
// role change, if requested, was already authorized above, so a ban
|
|
// committing here cannot be followed by a refused role change leaving
|
|
// a half-applied PATCH behind.
|
|
if !patchUserApplyBan(w, r, hub, mod, actor, id, req) {
|
|
return
|
|
}
|
|
|
|
if !patchUserApplyRole(w, r, hub, permInvalidator, mod, actor, id, req) {
|
|
return
|
|
}
|
|
|
|
updated, err := database.GetUserByID(r.Context(), id)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to fetch updated user")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, toAdminUserResponseFromUser(r.Context(), database, updated))
|
|
}
|
|
}
|
|
|
|
// handleForceLogout revokes every session of the target user. The route is
|
|
// gated on KICK_MEMBERS; ModerationService additionally enforces the
|
|
// actor-outranks-target hierarchy and writes the audit row.
|
|
func handleForceLogout(mod *service.ModerationService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id, err := pathInt64(r, "id")
|
|
if err != nil {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", "invalid user id")
|
|
return
|
|
}
|
|
if mod == nil {
|
|
// Fail closed rather than cut sessions without a hierarchy check.
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "moderation service unavailable")
|
|
return
|
|
}
|
|
|
|
if err := mod.ForceLogout(r.Context(), actorFromContext(r), id); err != nil {
|
|
writeModerationErr(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
// handleGetMe describes the calling principal so the admin panel can hide the
|
|
// surfaces its role cannot use. Perimeter-level: every authenticated principal
|
|
// may read its own permissions.
|
|
func handleGetMe() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
user, userOK := r.Context().Value(adminUserKey).(*db.User)
|
|
role, roleOK := r.Context().Value(adminRoleKey).(*db.Role)
|
|
if !userOK || user == nil || !roleOK || role == nil {
|
|
writeErr(w, http.StatusUnauthorized, "UNAUTHORIZED", "not authenticated")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, adminMeResponse{
|
|
ID: user.ID,
|
|
Username: user.Username,
|
|
RoleID: role.ID,
|
|
RoleName: role.Name,
|
|
RolePosition: role.Position,
|
|
Permissions: role.Permissions,
|
|
IsOwner: role.Position >= permissions.OwnerRolePosition,
|
|
})
|
|
}
|
|
}
|