fix(controlplane): stop exposing pprof /debug endpoints on public listeners
Merged
JI
4mo ago+139+139+012345678901234567890123456789-11-11-01234567890123456789
Summary
The controlplane exposed Go pprof /debug/pprof/* endpoints, unauthenticated, on the public API and metrics HTTP listeners regardless of the enable_profiler setting.
The cause is a combination of two behaviors:
- The
net/http/pprofblank import registers its handlers on the process-widehttp.DefaultServeMuxat init time. - go-kratos sets every HTTP server's
NotFoundHandler(andMethodNotAllowedHandler) tohttp.DefaultServeMux, so any unmatched route falls through to those pprof handlers. The kratos auth/RBAC middleware does not run on that fall-through path.
As a result /debug/pprof/* was reachable on the public API port and the metrics port even with enable_profiler: false.
Changes
- Add
hardenedRouteOptions()which overridesNotFoundHandler/MethodNotAllowedHandlerwith plain 404/405 responses so no kratos HTTP server falls through tohttp.DefaultServeMux. Applied to the main API server, the metrics server, and the profiler server. - Register the pprof handlers explicitly on the opt-in profiler server's own router (private mux) instead of relying on the default-mux fall-through.
- Remove the
net/http/pprofblank imports from the controlplane entrypoint.
With these changes pprof is unreachable when profiling is disabled, and when enabled it is served only on the dedicated profiler port.
AI assistance
This change was produced with assistance from Claude Code.