Files
OwnCord/Server/api/invite_handler.go
T
jevb 6eba999233 feat: add Let's Encrypt ACME support, fix security issues, improve server UX
Server:
- Add Let's Encrypt (ACME) TLS mode with autocert, HTTP-01 challenges on :80,
  and automatic certificate renewal (tls.mode: "acme" in config.yaml)
- Add ASCII art startup banner with server info and endpoint URLs
- Fix CSP blocking admin panel inline styles/scripts (per-route override)
- Suppress TLS handshake error noise in console output
- Fix TOCTOU race in invite consumption (atomic UPDATE with row-count check)
- Fix sendMsg mutex race condition (hold lock for entire send)
- Fix permission override formula (deny-first, allow-wins)
- Fix voice join parsing channelID before permission check
- Add session expiry check at WebSocket auth and periodic revalidation
- Add message length limit (4000 chars) and emoji length validation (32 bytes)
- Add file size enforcement in storage after io.Copy
- Add checksum URL validation in updater
- Add backup path traversal protection (BackupToSafe)
- Add self-modification guard in admin handlePatchUser
- Fix admin ownerOnlyMiddleware to use context user instead of re-auth
- Remove redundant startup log lines (banner shows same info)
- Add periodic expired session cleanup (15-min ticker)
- Add permissions package with bitfield constants and EffectivePerms
- Add rate limiter cleanup goroutine to prevent unbounded growth
- Add auth helpers (IsEffectivelyBanned, IsSessionExpired)
- Add WebSocket origin validation

Client:
- Add TOFU certificate trust service
- Add receive loop error handling
- Fix redundant else-if in OnChatMessage
2026-03-15 07:07:59 +01:00

159 lines
4.1 KiB
Go

package api
import (
"encoding/json"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/owncord/server/db"
"github.com/owncord/server/permissions"
)
// createInviteRequest is the JSON body for POST /api/v1/invites.
type createInviteRequest struct {
MaxUses int `json:"max_uses"`
ExpiresInHours int `json:"expires_in_hours"`
}
// inviteResponse is the API shape for an invite.
type inviteResponse struct {
ID int64 `json:"id"`
Code string `json:"code"`
MaxUses *int `json:"max_uses"`
Uses int `json:"uses"`
ExpiresAt *string `json:"expires_at"`
Revoked bool `json:"revoked"`
CreatedAt string `json:"created_at"`
}
// MountInviteRoutes registers invite endpoints on the given router.
// All routes require authentication and MANAGE_INVITES permission.
func MountInviteRoutes(r chi.Router, database *db.DB) {
r.Route("/api/v1/invites", func(r chi.Router) {
r.Use(AuthMiddleware(database))
r.Use(RequirePermission(permissions.ManageInvites))
r.Post("/", handleCreateInvite(database))
r.Get("/", handleListInvites(database))
r.Delete("/{code}", handleRevokeInvite(database))
})
}
// handleCreateInvite processes POST /api/v1/invites.
func handleCreateInvite(database *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req createInviteRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
// Treat missing body as default values (all optional).
req = createInviteRequest{}
}
user, ok := r.Context().Value(UserKey).(*db.User)
if !ok || user == nil {
writeJSON(w, http.StatusUnauthorized, errorResponse{
Error: "UNAUTHORIZED",
Message: "not authenticated",
})
return
}
var expiresAt *time.Time
if req.ExpiresInHours > 0 {
t := time.Now().Add(time.Duration(req.ExpiresInHours) * time.Hour)
expiresAt = &t
}
code, err := database.CreateInvite(user.ID, req.MaxUses, expiresAt)
if err != nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{
Error: "SERVER_ERROR",
Message: "failed to create invite",
})
return
}
inv, err := database.GetInvite(code)
if err != nil || inv == nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{
Error: "SERVER_ERROR",
Message: "failed to retrieve invite",
})
return
}
writeJSON(w, http.StatusCreated, toInviteResponse(inv))
}
}
// handleListInvites processes GET /api/v1/invites.
func handleListInvites(database *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
invites, err := database.ListInvites()
if err != nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{
Error: "SERVER_ERROR",
Message: "failed to list invites",
})
return
}
resp := make([]inviteResponse, 0, len(invites))
for _, inv := range invites {
resp = append(resp, toInviteResponse(inv))
}
writeJSON(w, http.StatusOK, resp)
}
}
// handleRevokeInvite processes DELETE /api/v1/invites/:code.
func handleRevokeInvite(database *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
code := chi.URLParam(r, "code")
inv, err := database.GetInvite(code)
if err != nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{
Error: "SERVER_ERROR",
Message: "failed to look up invite",
})
return
}
if inv == nil {
writeJSON(w, http.StatusNotFound, errorResponse{
Error: "NOT_FOUND",
Message: "invite not found",
})
return
}
if err := database.RevokeInvite(code); err != nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{
Error: "SERVER_ERROR",
Message: "failed to revoke invite",
})
return
}
w.WriteHeader(http.StatusNoContent)
}
}
// toInviteResponse converts a db.Invite to the API response shape.
func toInviteResponse(inv *db.Invite) inviteResponse {
var maxUses *int
if inv.MaxUses != nil {
v := *inv.MaxUses
maxUses = &v
}
return inviteResponse{
ID: inv.ID,
Code: inv.Code,
MaxUses: maxUses,
Uses: inv.Uses,
ExpiresAt: inv.ExpiresAt,
Revoked: inv.Revoked,
CreatedAt: inv.CreatedAt,
}
}