Files
OwnCord/Server/api/client_update.go
T
J3vbandClaude Opus 4.8 7b178ff30b fix(security): harden server against verified code-review findings
Applies fixes for 20 adversarially-verified findings from a whole-codebase
security review (server side). All Go build-tag variants build, `go vet` is
clean, and the suite passes (the sole failing test, ws TestEmitEvents, is a
pre-existing nil-harness failure unrelated to these changes).

High severity:
- auth: close TOCTOU in TOTP verify rate-limit by recording each attempt
  atomically up-front (was Check-then-Allow), restoring the per-user
  brute-force cap.
- plugin: enforce the CPU/time budget on every WASM guest call via a
  WithTimeout context (WithCloseOnContextDone interrupts runaways); the
  configured budget was previously parsed but never applied.
- api/waf: inspect request bodies for chunked (ContentLength==-1) requests
  so the SQLi/XSS/RCE body rules can no longer be bypassed.
- ws: rate-limit voice_join/voice_leave and voice_e2ee announce/offer, which
  fan out to every participant and could force mass disconnects.

Medium severity:
- api: run bcrypt on the unknown-user login path (no || short-circuit) to
  remove the timing-based username-enumeration oracle.
- ws: verify LiveKit webhooks via the SDK receiver so the signature is bound
  to the body hash (kills forgery/replay).
- authz: require READ_MESSAGES for reactions and for plugin-command
  broadcasts; route the latter through RequireChannelAccess.
- api: cache the client-update signature fetch and rate-limit the endpoint.
- service: propagate DeleteOtherSessions failure from ChangePassword instead
  of silently reporting success.
- api: trust the rightmost non-proxy X-Forwarded-For entry, not the
  client-controllable leftmost one.
- plugin: route auto-registered commands through the conflict-checked
  RegisterCommand; pin the DNS-validated IP for host_http dials
  (DNS-rebinding TOCTOU).
- api: mark access-controlled downloads private/no-cache + Vary: Origin.

Low severity:
- auth: fail closed when a fully-shaped TOTP ciphertext fails GCM auth
  (was returning the ciphertext as plaintext).
- api: apply the livekit-proxy path allowlist to WebSocket upgrades too.
- service: verify attachment ownership before linking (IDOR).
- admin: bound the bootstrap setup invite (5 uses / 24h); re-verify the
  update binary hash immediately before rename+spawn (TOCTOU).
- service: require BanMembers + role hierarchy for moderation ban/unban.

chore: stop tracking the stray Server/owncord-server.exe build artifact.

Test infra: add uploader_id to the hand-rolled ws test attachment schemas and
make MemStore.GetAttachmentByID a no-op lookup, matching production/DB behavior.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 21:08:54 +02:00

101 lines
3.0 KiB
Go

// Package api provides the HTTP router and handlers for the OwnCord server.
//
// client_update.go serves Tauri-compatible update metadata so the desktop
// client can check for new versions and self-update.
package api
import (
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/owncord/server/updater"
"golang.org/x/mod/semver"
)
// tauriPlatformResponse is the per-platform entry in the Tauri updater JSON.
type tauriPlatformResponse struct {
Signature string `json:"signature"`
URL string `json:"url"`
}
// tauriUpdateResponse is the JSON shape the Tauri updater plugin expects.
type tauriUpdateResponse struct {
Version string `json:"version"`
Notes string `json:"notes,omitempty"`
PubDate string `json:"pub_date,omitempty"`
Platforms map[string]tauriPlatformResponse `json:"platforms"`
}
// MountClientUpdateRoute adds the unauthenticated client-update endpoint.
// The route is outside the auth middleware because the client needs to check
// for updates before (or without) logging in.
func MountClientUpdateRoute(r chi.Router, u *updater.Updater) {
r.Get("/api/v1/client-update/{target}/{current_version}", handleClientUpdate(u))
}
func handleClientUpdate(u *updater.Updater) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
target := chi.URLParam(r, "target")
currentVersion := chi.URLParam(r, "current_version")
if target == "" || currentVersion == "" {
http.Error(w, "missing target or current_version", http.StatusBadRequest)
return
}
info, err := u.CheckForUpdate(r.Context())
if err != nil {
http.Error(w, "failed to check for updates", http.StatusBadGateway)
return
}
// Compare versions — return 204 if no update available.
cv := ensureV(currentVersion)
lv := ensureV(info.Latest)
if semver.Compare(cv, lv) >= 0 {
w.WriteHeader(http.StatusNoContent)
return
}
// Find the .nsis.zip and .nsis.zip.sig assets from the release.
clientAssets := u.FindClientAssets()
nsisURL := clientAssets.InstallerURL
sigURL := clientAssets.SignatureURL
if nsisURL == "" || sigURL == "" {
w.WriteHeader(http.StatusNoContent)
return
}
// Fetch the signature file content (small text file). Cached with the
// same TTL as the release info so this unauthenticated endpoint does not
// perform an outbound fetch on every request (DoS hardening).
sigContent, err := u.FetchTextAssetCached(r.Context(), sigURL)
if err != nil {
http.Error(w, "failed to fetch signature", http.StatusBadGateway)
return
}
resp := tauriUpdateResponse{
Version: strings.TrimPrefix(info.Latest, "v"),
Notes: info.ReleaseNotes,
Platforms: map[string]tauriPlatformResponse{
target: {
Signature: strings.TrimSpace(sigContent),
URL: nsisURL,
},
},
}
writeJSON(w, http.StatusOK, resp)
}
}
// ensureV returns a version string with a "v" prefix for semver comparison.
func ensureV(v string) string {
if strings.HasPrefix(v, "v") {
return v
}
return "v" + v
}