mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
- C-3: inject setupLimiter into NewAdminAPI instead of package-level global - H-5: generateRandomKey returns error instead of panicking - H-6: replace init() bcrypt with sync.Once lazy initialization - M-2: remove unsafe-inline from admin CSP script-src and style-src - M-3: sanitize upload filenames (strip control chars, truncate to 255) - M-5: truncate User-Agent to 512 bytes before storing as device - M-10: MaxBodySizeUnless uses prefix matching instead of exact path - M-12: wrap seedExistingDatabase in a single transaction - M-13: use errors.Is for EOF check in upload handler - M-14: log writeJSON encoding errors instead of discarding - M-16: standardize error codes to INTERNAL_ERROR across all handlers
158 lines
5.0 KiB
Go
158 lines
5.0 KiB
Go
package admin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
"github.com/owncord/server/auth"
|
|
"github.com/owncord/server/db"
|
|
)
|
|
|
|
// setupSanitizer strips all HTML from user input during setup.
|
|
var setupSanitizer = bluemonday.StrictPolicy()
|
|
|
|
// ownerRoleID is the role ID assigned to the first user (Owner).
|
|
const ownerRoleID = 1
|
|
|
|
// setupStatusResponse is the JSON shape returned by GET /api/setup/status.
|
|
type setupStatusResponse struct {
|
|
NeedsSetup bool `json:"needs_setup"`
|
|
}
|
|
|
|
// setupRequest is the JSON body for POST /api/setup.
|
|
type setupRequest struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
// setupResponse is the JSON shape returned on successful setup.
|
|
type setupResponse struct {
|
|
Token string `json:"token"`
|
|
UserID int64 `json:"user_id"`
|
|
Username string `json:"username"`
|
|
InviteCode string `json:"invite_code"`
|
|
}
|
|
|
|
// handleSetupStatus returns whether initial setup is needed (no users exist).
|
|
func handleSetupStatus(database *db.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
count, err := database.UserCount()
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to check user count")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, setupStatusResponse{NeedsSetup: count == 0})
|
|
}
|
|
}
|
|
|
|
// handleSetup creates the first owner account. It only works when no users
|
|
// exist in the database, preventing abuse after initial setup.
|
|
func handleSetup(database *db.DB, limiter *auth.RateLimiter) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Rate limit: 5 attempts per minute per IP.
|
|
// Strip the port so that different source ports from the same IP
|
|
// are correctly grouped under a single rate-limit bucket.
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
host = r.RemoteAddr
|
|
}
|
|
setupKey := "setup:" + host
|
|
if !limiter.Allow(setupKey, 5, time.Minute) {
|
|
writeErr(w, http.StatusTooManyRequests, "RATE_LIMITED", "too many setup attempts, try again later")
|
|
return
|
|
}
|
|
|
|
// Gate: only allow when no users exist.
|
|
count, err := database.UserCount()
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to check user count")
|
|
return
|
|
}
|
|
if count > 0 {
|
|
writeErr(w, http.StatusForbidden, "FORBIDDEN", "setup has already been completed")
|
|
return
|
|
}
|
|
|
|
var req setupRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", "invalid request body")
|
|
return
|
|
}
|
|
|
|
req.Username = strings.TrimSpace(setupSanitizer.Sanitize(req.Username))
|
|
if req.Username == "" || req.Password == "" {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", "username and password are required")
|
|
return
|
|
}
|
|
|
|
// Validate username format (length, no control/invisible chars).
|
|
if err := auth.ValidateUsername(req.Username); err != nil {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", err.Error())
|
|
return
|
|
}
|
|
|
|
if err := auth.ValidatePasswordStrength(req.Password); err != nil {
|
|
writeErr(w, http.StatusBadRequest, "BAD_REQUEST", err.Error())
|
|
return
|
|
}
|
|
|
|
// Hash the password.
|
|
hash, err := auth.HashPassword(req.Password)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to hash password")
|
|
return
|
|
}
|
|
|
|
// Create the owner account (role_id=1 is Owner).
|
|
uid, err := database.CreateUser(req.Username, hash, ownerRoleID)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to create user")
|
|
return
|
|
}
|
|
|
|
// Issue a session token so the user is immediately logged in.
|
|
token, err := auth.GenerateToken()
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to generate session token")
|
|
return
|
|
}
|
|
|
|
device := r.Header.Get("User-Agent")
|
|
const maxDeviceLen = 512
|
|
if len(device) > maxDeviceLen {
|
|
device = device[:maxDeviceLen]
|
|
}
|
|
if _, err := database.CreateSession(uid, auth.HashToken(token), device, host); err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to create session")
|
|
return
|
|
}
|
|
|
|
// Create default channels under canonical categories.
|
|
_, _ = database.CreateChannel("general", "text", "Text Channels", "Welcome to the server!", 0)
|
|
_, _ = database.CreateChannel("General", "voice", "Voice Channels", "", 0)
|
|
|
|
// Generate a bootstrap invite code so the owner can invite others.
|
|
inviteCode, err := database.CreateInvite(uid, 0, nil) // unlimited uses, no expiry
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "INTERNAL_ERROR", "failed to generate invite code")
|
|
return
|
|
}
|
|
|
|
slog.Info("server setup completed", "owner", req.Username, "user_id", uid)
|
|
_ = database.LogAudit(uid, "server_setup", "server", 0,
|
|
"initial setup: owner account created, default channel and invite generated")
|
|
|
|
writeJSON(w, http.StatusCreated, setupResponse{
|
|
Token: token,
|
|
UserID: uid,
|
|
Username: req.Username,
|
|
InviteCode: inviteCode,
|
|
})
|
|
}
|
|
}
|