Files
Pulse/internal/api/demo_middleware.go
rcourtman 27854a6da5 Stop websocket upgrade headers exempting demo-mode writes
DemoModeMiddleware admitted any request carrying "Upgrade: websocket",
whatever its method or path, so a POST/PUT/PATCH/DELETE with that header
reached its handler on a read-only demo instance. The public demo signs
visitors in as the configured admin user, which RequireAdmin accepts and
which skips token scope checks, so the middleware was the only write
guard: adding two headers let a demo session switch the instance out of
mock mode, change system settings or create organisations.

The exemption never served a real handshake. RFC 6455 handshakes are GET
requests, gorilla's Upgrader refuses any other method, and GET already
passes the read-only branch above it, so /ws and /api/agent/ws keep
connecting. Removing the branch leaves the method alone deciding.

TestContract_DemoModeUpgradeHeadersDoNotExemptWrites signs in as the demo
admin through the full router, proves the session passes RequireAdmin,
gets 403 for a mock-mode POST with upgrade headers and still opens /ws.
On the old middleware the same POST returned 200 and turned mock mode off.
2026-10-07 13:46:17 +01:00

83 lines
2.4 KiB
Go

package api
import (
"encoding/json"
"net/http"
"strings"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rs/zerolog/log"
)
// DemoModeMiddleware blocks all modification requests in demo mode
func DemoModeMiddleware(cfg *config.Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cfg == nil || !cfg.DemoMode {
next.ServeHTTP(w, r)
return
}
// Add header so frontend knows we're in demo mode
w.Header().Set("X-Demo-Mode", "true")
if exposure, ok := publicDemoCommercialPolicyForRequest(r); ok {
if exposure == publicDemoCommercialExposureHidden {
http.NotFound(w, r)
return
}
}
if exposure, ok := publicDemoAdminOperationsPolicyForRequest(r); ok {
if exposure == publicDemoCommercialExposureHidden {
http.NotFound(w, r)
return
}
}
// Allow GET and HEAD requests (read-only). WebSocket handshakes are GET
// requests (RFC 6455 section 4.1), so /ws and /api/agent/ws connect
// through this branch. An Upgrade header never exempts another method:
// a POST carrying "Upgrade: websocket" is still a write.
if r.Method == http.MethodGet || r.Method == http.MethodHead || r.Method == http.MethodOptions {
next.ServeHTTP(w, r)
return
}
// Allow authentication endpoints (login is read-only - verifies credentials)
authPaths := []string{
"/api/login",
"/api/logout",
// Allow AI chat interaction (mocked in backend if key missing)
"/api/ai/execute",
}
for _, path := range authPaths {
if r.URL.Path == path {
next.ServeHTTP(w, r)
return
}
}
// Allow per-provider OIDC auth flow endpoints:
// /api/oidc/{providerID}/login and /api/oidc/{providerID}/callback
if strings.HasPrefix(r.URL.Path, "/api/oidc/") {
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
if len(parts) >= 4 && (parts[3] == "login" || parts[3] == "callback") {
next.ServeHTTP(w, r)
return
}
}
// Block all modification requests (POST, PUT, DELETE, PATCH)
log.Warn().
Str("method", r.Method).
Str("path", r.URL.Path).
Str("remote", r.RemoteAddr).
Msg("Demo mode: blocked modification request")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(map[string]string{
"error": "Demo mode enabled",
"message": "This is a read-only demo instance. Modifications are disabled.",
})
})
}