git » kxd » commit dd363cb

kxd: Set timeouts on the HTTP server

author Alberto Bertogli
2026-11-28 21:17:00 UTC
committer Alberto Bertogli
2026-11-28 21:17:00 UTC
parent b97d2ad38976a2cc23eab10ed3df6457c82a57b5

kxd: Set timeouts on the HTTP server

The HTTP server had no timeouts, so a client could open a connection and
hold it indefinitely (not completing the TLS handshake, slowly sending
the request, or just idle keep-alive). That could be used to exhaust the
server's file descriptors, and it requires no credentials.

This patch sets read, read header, idle, and write timeouts. The write
timeout covers the handler, so it is derived from the hook timeout, with
some margin for sending the notification email.

kxd/hook.go +4 -1
kxd/kxd.go +12 -0

diff --git a/kxd/hook.go b/kxd/hook.go
index 6b072bf..4af5dec 100644
--- a/kxd/hook.go
+++ b/kxd/hook.go
@@ -12,6 +12,9 @@ import (
 	"time"
 )
 
+// Maximum time the hook is allowed to run for.
+const hookTimeout = 1 * time.Minute
+
 // RunHook runs the hook, returns an error if the request is not allowed (or
 // there were problems with the hook; we don't make the distinction for now).
 //
@@ -27,7 +30,7 @@ func RunHook(kc *KeyConfig, req *Request, chains [][]*x509.Certificate) error {
 		return nil
 	}
 
-	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
+	ctx, cancel := context.WithTimeout(context.Background(), hookTimeout)
 	defer cancel()
 	cmd := exec.CommandContext(ctx, *hookPath)
 
diff --git a/kxd/kxd.go b/kxd/kxd.go
index a852932..06bd965 100644
--- a/kxd/kxd.go
+++ b/kxd/kxd.go
@@ -300,6 +300,18 @@ func main() {
 		Addr:      listenAddr,
 		TLSConfig: &tlsConfig,
 		ErrorLog:  logging,
+
+		// Timeouts, so clients can't hold connections open indefinitely.
+		// The TLS handshake is bounded by the smallest of the read and
+		// write timeouts.
+		ReadHeaderTimeout: 10 * time.Second,
+		ReadTimeout:       30 * time.Second,
+		IdleTimeout:       1 * time.Minute,
+
+		// The write timeout covers the time it takes the handler to run,
+		// including the hook and sending the notification email, so leave
+		// plenty of margin over the hook timeout.
+		WriteTimeout: hookTimeout + 1*time.Minute,
 	}
 
 	http.HandleFunc("/v1/", HandlerV1)