Files
Pulse/internal/api/demo_mode_operations_test.go
rcourtman 0180b33431 Hide pprof routes on demo-mode instances
The /debug/pprof handlers are gated on RequireAdmin plus a settings:read
scope that session logins skip. The public demo signs every visitor in
as the configured admin, so any visitor could fetch goroutine and heap
dumps and the process command line, or start CPU profiles and execution
traces that load the whole process. DEMO_MODE already hides diagnostics,
logs, admin users and manual discovery through the admin-operations
policy; pprof was missing from it.

The policy now has the demo guard answer /debug/pprof and every path
below it with 404 for every method, ahead of route authentication.
Instances outside demo mode keep the routes, and PULSE_PPROF_DISABLED
still removes them everywhere.

TestContract_DemoModeHidesPprofFromDemoAdminSession signs in as the
demo admin through the full router, shows the same session still reads
/debug/pprof/cmdline below the demo guard, and gets 404 for eight pprof
paths through the served handler. On the previous policy GET and HEAD
returned pprof output (200, or the root's 301 into the index) and POST
got the generic demo 403. A policy test also fails if a literal route
the router files register under /debug/, with or without a method
prefix, escapes the policy for any common method.
2026-10-07 20:04:07 +01:00

123 lines
4.8 KiB
Go

package api
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestPublicDemoAdminOperationsPolicyForRequest(t *testing.T) {
hiddenRoutes := []struct {
name string
method string
path string
}{
{name: "diagnostics", method: http.MethodGet, path: "/api/diagnostics"},
{name: "diagnostics token prepare", method: http.MethodPost, path: "/api/diagnostics/docker/prepare-token"},
{name: "logs stream", method: http.MethodGet, path: "/api/logs/stream"},
{name: "logs download", method: http.MethodGet, path: "/api/logs/download"},
{name: "logs level read", method: http.MethodGet, path: "/api/logs/level"},
{name: "logs level write", method: http.MethodPost, path: "/api/logs/level"},
{name: "admin users read", method: http.MethodGet, path: "/api/admin/users"},
{name: "admin users trailing slash read", method: http.MethodGet, path: "/api/admin/users/"},
{name: "admin users head", method: http.MethodHead, path: "/api/admin/users"},
{name: "manual discovery read", method: http.MethodGet, path: "/api/discover"},
{name: "manual discovery head", method: http.MethodHead, path: "/api/discover"},
{name: "pprof root", method: http.MethodGet, path: "/debug/pprof"},
{name: "pprof index", method: http.MethodGet, path: "/debug/pprof/"},
{name: "pprof heap with forced gc", method: http.MethodGet, path: "/debug/pprof/heap?gc=1"},
{name: "pprof goroutine dump", method: http.MethodGet, path: "/debug/pprof/goroutine?debug=2"},
{name: "pprof cmdline", method: http.MethodGet, path: "/debug/pprof/cmdline"},
{name: "pprof cpu profile", method: http.MethodGet, path: "/debug/pprof/profile?seconds=30"},
{name: "pprof trace", method: http.MethodGet, path: "/debug/pprof/trace?seconds=5"},
{name: "pprof symbol lookup", method: http.MethodPost, path: "/debug/pprof/symbol"},
{name: "pprof head", method: http.MethodHead, path: "/debug/pprof/allocs"},
{name: "pprof options", method: http.MethodOptions, path: "/debug/pprof/"},
}
for _, tc := range hiddenRoutes {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(tc.method, tc.path, nil)
exposure, ok := publicDemoAdminOperationsPolicyForRequest(req)
if !ok {
t.Fatalf("%s %s did not match public demo admin operations policy", tc.method, tc.path)
}
if exposure != publicDemoCommercialExposureHidden {
t.Fatalf("%s %s exposure=%q, want %q", tc.method, tc.path, exposure, publicDemoCommercialExposureHidden)
}
})
}
allowedRoutes := []struct {
name string
method string
path string
}{
{name: "health", method: http.MethodGet, path: "/api/health"},
{name: "resources", method: http.MethodGet, path: "/api/resources"},
{name: "runtime capabilities", method: http.MethodGet, path: "/api/license/runtime-capabilities"},
{name: "admin users write", method: http.MethodPost, path: "/api/admin/users"},
{name: "manual discovery write", method: http.MethodPost, path: "/api/discover"},
{name: "pprof lookalike", method: http.MethodGet, path: "/debug/pprofile"},
}
for _, tc := range allowedRoutes {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(tc.method, tc.path, nil)
if exposure, ok := publicDemoAdminOperationsPolicyForRequest(req); ok {
t.Fatalf("%s %s unexpectedly matched public demo admin operations policy with exposure=%q", tc.method, tc.path, exposure)
}
})
}
}
// Every literal route the router files register under /debug/, with or without
// a method prefix, must stay hidden on the public demo whatever the method, so
// a new debug handler cannot ship reachable to the demo admin session by
// default.
func TestPublicDemoAdminOperationsPolicyHidesEveryDebugRoute(t *testing.T) {
literalRoutes, _, _ := parseRouterRoutes(t)
methods := []string{
http.MethodGet,
http.MethodHead,
http.MethodOptions,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
}
var debugRoutes int
for _, route := range literalRoutes {
// ServeMux separates a method from its path with spaces or tabs.
pattern := route
if i := strings.IndexAny(route, " \t"); i > 0 {
if rest := strings.TrimLeft(route[i+1:], " \t"); strings.HasPrefix(rest, "/") {
pattern = rest
}
}
if !strings.HasPrefix(pattern, "/debug/") {
continue
}
debugRoutes++
paths := []string{pattern}
if strings.HasSuffix(pattern, "/") {
paths = append(paths, pattern+"goroutine")
}
for _, path := range paths {
for _, method := range methods {
req := httptest.NewRequest(method, path, nil)
exposure, ok := publicDemoAdminOperationsPolicyForRequest(req)
if !ok || exposure != publicDemoCommercialExposureHidden {
t.Errorf("%s %s is not hidden on the public demo (matched=%v exposure=%q)", method, path, ok, exposure)
}
}
}
}
if debugRoutes == 0 {
t.Fatal("no /debug/ routes found in the router inventory")
}
}