From f8610d66937173ce8d5a7df0791978b49d156ab6 Mon Sep 17 00:00:00 2001 From: Abdel Date: Tue, 22 Sep 2026 10:39:28 +0000 Subject: [PATCH] fix(engine): apply ReadHeaderTimeout to the RunUnix server RunUnix created an http.Server without ReadHeaderTimeout while Run, RunTLS and RunListener already pass Engine.ReadHeaderTimeout to the http.Server they create, so a slow client on a unix socket could hold the connection open indefinitely, the same slowloris-style exposure that #4800 just fixed for the other run methods Wire engine.getReadHeaderTimeout() into the RunUnix server and extend the docs to include RunUnix, tested with a unix socket client that sends headers without the final CRLF, the server now closes the connection after the configured timeout instead of waiting forever --- gin.go | 10 +++++++--- gin_integration_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/gin.go b/gin.go index a1607275..eb09e4b9 100644 --- a/gin.go +++ b/gin.go @@ -31,7 +31,7 @@ const ( ) // defaultReadHeaderTimeout is the default ReadHeaderTimeout applied to the -// http.Server created by Run, RunTLS and RunListener. It protects against +// http.Server created by Run, RunTLS, RunUnix and RunListener. It protects against // slowloris-style resource exhaustion when the caller does not configure a // custom http.Server. const defaultReadHeaderTimeout = 10 * time.Second @@ -174,7 +174,7 @@ type Engine struct { MaxMultipartMemory int64 // ReadHeaderTimeout is the maximum duration allowed for reading the request - // headers by the http.Server created through Run, RunTLS and RunListener. + // headers by the http.Server created through Run, RunTLS, RunUnix and RunListener. // If zero or negative, a default of 10 seconds is used. Set to a positive // value to customize, or use a custom http.Server directly to disable it. ReadHeaderTimeout time.Duration @@ -606,6 +606,9 @@ func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) { // RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests // through the specified unix socket (i.e. a file). // Note: this method will block the calling goroutine indefinitely unless an error happens. +// The created http.Server is configured with a default ReadHeaderTimeout of 10 seconds +// (configurable via Engine.ReadHeaderTimeout) to mitigate slowloris attacks. +// For full control over timeouts, create a custom http.Server and use ServeHTTP. func (engine *Engine) RunUnix(file string) (err error) { debugPrint("Listening and serving HTTP on unix:/%s", file) defer func() { debugPrintError(err) }() @@ -623,7 +626,8 @@ func (engine *Engine) RunUnix(file string) (err error) { defer os.Remove(file) server := &http.Server{ // #nosec G112 - Handler: engine.Handler(), + Handler: engine.Handler(), + ReadHeaderTimeout: engine.getReadHeaderTimeout(), } err = server.Serve(listener) return diff --git a/gin_integration_test.go b/gin_integration_test.go index da408d9c..3e43c483 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -275,6 +275,36 @@ func TestUnixSocket(t *testing.T) { assert.Contains(t, response, "it worked", "resp body should match") } +func TestUnixSocketReadHeaderTimeout(t *testing.T) { + router := New() + router.ReadHeaderTimeout = 200 * time.Millisecond + + unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test_timeout") + + defer os.Remove(unixTestSocket) + + go func() { + router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) + assert.NoError(t, router.RunUnix(unixTestSocket)) + }() + // have to wait for the goroutine to start and run the server + // otherwise the main thread will complete + time.Sleep(5 * time.Millisecond) + + c, err := net.Dial("unix", unixTestSocket) + require.NoError(t, err) + defer c.Close() + + // headers without the final CRLF keep the server waiting for them + fmt.Fprint(c, "GET /example HTTP/1.1\r\nHost: gin.example\r\n") + + start := time.Now() + require.NoError(t, c.SetReadDeadline(time.Now().Add(5*time.Second))) + buf := make([]byte, 512) + _, _ = c.Read(buf) + assert.Less(t, time.Since(start), 2*time.Second, "server should close the connection after ReadHeaderTimeout") +} + func TestBadUnixSocket(t *testing.T) { router := New() require.Error(t, router.RunUnix("#/tmp/unix_unit_test"))