Skip to content

Commit 0284a5b

Browse files
committed
fix: replace http.DefaultServeMux fallback handlers with safe defaults
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
1 parent f149714 commit 0284a5b

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

transport/http/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ func NewServer(opts ...ServerOption) *Server {
189189
strictSlash: true,
190190
router: mux.NewRouter(),
191191
}
192-
srv.router.NotFoundHandler = http.DefaultServeMux
193-
srv.router.MethodNotAllowedHandler = http.DefaultServeMux
192+
srv.router.NotFoundHandler = http.NotFoundHandler()
193+
srv.router.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
194+
w.WriteHeader(http.StatusMethodNotAllowed)
195+
})
194196
for _, o := range opts {
195197
o(srv)
196198
}

0 commit comments

Comments
 (0)