fix: replace http.DefaultServeMux fallback with safe defaults#3814
Open
Yanhu007 wants to merge 1 commit into
Open
fix: replace http.DefaultServeMux fallback with safe defaults#3814Yanhu007 wants to merge 1 commit into
Yanhu007 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3810
Security Issue
The HTTP server sets
http.DefaultServeMuxas the fallback handler for unmatched routes and disallowed methods. SinceDefaultServeMuxis a global shared instance, packages that register handlers ininit()(most notablynet/http/pprof) are inadvertently exposed to the network.Any request to an unregistered route (e.g.
/debug/pprof/) falls through toDefaultServeMux, potentially exposing profiling data, goroutine dumps, and heap profiles.Fix
Replace with safe defaults:
http.NotFoundHandler()forNotFoundHandler(returns 404)MethodNotAllowedHandlerUsers who need the previous behavior can explicitly opt-in using the existing
NotFoundHandler()andMethodNotAllowedHandler()server options added in #3131.Breaking Change
This is technically a breaking change for anyone relying on the
DefaultServeMuxfallback behavior, but the previous default was a security risk. TheNotFoundHandler()andMethodNotAllowedHandler()options provide a migration path.