Skip to content

fix: replace http.DefaultServeMux fallback with safe defaults#3814

Open
Yanhu007 wants to merge 1 commit into
go-kratos:mainfrom
Yanhu007:fix/remove-default-servemux-fallback
Open

fix: replace http.DefaultServeMux fallback with safe defaults#3814
Yanhu007 wants to merge 1 commit into
go-kratos:mainfrom
Yanhu007:fix/remove-default-servemux-fallback

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #3810

Security Issue

The HTTP server sets http.DefaultServeMux as the fallback handler for unmatched routes and disallowed methods. Since DefaultServeMux is a global shared instance, packages that register handlers in init() (most notably net/http/pprof) are inadvertently exposed to the network.

Any request to an unregistered route (e.g. /debug/pprof/) falls through to DefaultServeMux, potentially exposing profiling data, goroutine dumps, and heap profiles.

Fix

Replace with safe defaults:

  • http.NotFoundHandler() for NotFoundHandler (returns 404)
  • A simple 405 handler for MethodNotAllowedHandler

Users who need the previous behavior can explicitly opt-in using the existing NotFoundHandler() and MethodNotAllowedHandler() server options added in #3131.

Breaking Change

This is technically a breaking change for anyone relying on the DefaultServeMux fallback behavior, but the previous default was a security risk. The NotFoundHandler() and MethodNotAllowedHandler() options provide a migration path.

The HTTP server uses http.DefaultServeMux as the fallback handler
for unmatched routes and disallowed methods. Since DefaultServeMux
is a global shared instance that may have handlers registered by
init() functions (e.g. net/http/pprof), this can unintentionally
expose debug endpoints like /debug/pprof/ to the network.

Replace with:
- http.NotFoundHandler() for NotFoundHandler (returns 404)
- A simple 405 handler for MethodNotAllowedHandler

Users who need the previous behavior can still explicitly set
http.DefaultServeMux using the existing NotFoundHandler() and
MethodNotAllowedHandler() server options.

Fixes go-kratos#3810
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unintended Route Exposure via DefaultServeMux Fallback

1 participant