From 38552f80256f1bb46a4fa845cb048dcb51ec4f12 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:54:50 +0100 Subject: [PATCH] fix(app): add read, write and idle timeouts to the HTTP servers - the API + admin UI server and the public web server get ReadTimeout 30s, WriteTimeout 60s and IdleTimeout 120s next to ReadHeaderTimeout - the archive import extends its own read and write deadlines to 30 minutes with http.ResponseController, so a big mbox is not cut - tests: a stalled body and an idle connection are cut on both servers, an import longer than the default timeouts succeeds, fields are pinned - README: document the limits and timeouts --- README.md | 6 ++ internal/api/api.go | 9 +++ internal/app/app.go | 23 ++++++- internal/app/app_test.go | 112 +++++++++++++++++++++++++++++++ internal/archive/archive_test.go | 4 +- 5 files changed, 150 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 61453bc..a9c4a70 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,12 @@ lists.example.org lmtp:inet:127.0.0.1:2424 full queue answers `452` (the sender retries later), a message over 10 MiB `552`. Messages nobody moderated for 30 days are dropped. Listings (API, CLI, UI) show a summary and never read the bodies. The held view shows the From header readers would see next to the real sender and flags a difference. +- **Hostile messages and slow clients.** The archive reads at most 10 levels of nested multipart and 100 parts per + message, then uses what it found (the post is still delivered). A `Date` header more than 24 hours ahead is replaced + by the arrival time, and one before 1990 is ignored, so a message cannot pin itself to the top of the archive. The + HTTP servers (API + admin UI, and the public web) cut a request that takes more than 30 s to read, a response that + takes more than 60 s to write, and a connection idle for 120 s; the archive import is the exception and may stream + for up to 30 minutes. - **API token.** `parsimony serve` refuses a token shorter than 24 characters or containing whitespace (`openssl rand -hex 24` makes a good one). After 10 wrong or missing tokens within 5 minutes an address is locked out for 5 minutes (HTTP 429, valid token included). The address is the TCP peer: behind a reverse proxy diff --git a/internal/api/api.go b/internal/api/api.go index b40b9ab..0ad23a7 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -561,6 +561,10 @@ func (s *server) discardQueued(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } +// importTimeout is how long one archive import may read its body and write its +// summary, in place of the server's default read and write timeouts. +const importTimeout = 30 * time.Minute + // importArchive reads an mbox from the request body, streaming, and archives it. // Failed messages are reported in the summary; the request itself succeeds. func (s *server) importArchive(w http.ResponseWriter, r *http.Request) { @@ -572,6 +576,11 @@ func (s *server) importArchive(w http.ResponseWriter, r *http.Request) { storeError(w, err) return } + // A big mbox streams for longer than the server's default timeouts. Errors + // are ignored: a ResponseWriter that cannot extend deadlines has none. + rc := http.NewResponseController(w) + _ = rc.SetReadDeadline(time.Now().Add(importTimeout)) + _ = rc.SetWriteDeadline(time.Now().Add(importTimeout)) sum, err := archive.ImportMbox(s.store, id, r.Body, time.Now()) if err != nil { writeError(w, http.StatusBadRequest, fmt.Sprintf("import stopped after %d message(s): %v", sum.Imported, err)) diff --git a/internal/app/app.go b/internal/app/app.go index 8909328..270b888 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -29,6 +29,25 @@ import ( // nowFunc is the clock of the store (queue schedules, token expiry). Tests move it. var nowFunc = time.Now +// HTTP timeouts of the API + admin UI server and of the public web server, so +// a slow or silent client cannot pin a goroutine. The archive import extends +// its own deadlines (see api.importArchive). Variables so tests can shorten them. +var ( + httpReadTimeout = 30 * time.Second + httpWriteTimeout = 60 * time.Second + httpIdleTimeout = 120 * time.Second +) + +func newHTTPServer(h http.Handler) *http.Server { + return &http.Server{ + Handler: h, + ReadHeaderTimeout: 10 * time.Second, + ReadTimeout: httpReadTimeout, + WriteTimeout: httpWriteTimeout, + IdleTimeout: httpIdleTimeout, + } +} + // heloName is the EHLO name: the configured one, else this machine's host name. func heloName(configured string) string { if configured != "" { @@ -196,7 +215,7 @@ func Start(cfg config.Config, apiToken string) (*App, error) { mux := http.NewServeMux() mux.Handle("/api/", api.New(st, apiToken, releaser{rt})) mux.Handle("/", adminui.Embedded()) - a.web = &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second} + a.web = newHTTPServer(mux) } var pubLn net.Listener @@ -211,7 +230,7 @@ func Start(cfg config.Config, apiToken string) (*App, error) { } a.WebAddr = pubLn.Addr().String() a.lns = append(a.lns, pubLn) - a.pub = &http.Server{Handler: web.New(st, rt), ReadHeaderTimeout: 10 * time.Second} + a.pub = newHTTPServer(web.New(st, rt)) } var workerCtx context.Context diff --git a/internal/app/app_test.go b/internal/app/app_test.go index 832937c..a1f6cf3 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -1004,3 +1004,115 @@ func TestTheServerNameSettingReachesTheTLSCheck(t *testing.T) { t.Fatal("nothing may be sent to a relay whose certificate does not match") } } + +// shortTimeouts makes the HTTP timeouts small enough to test. +func shortTimeouts(t *testing.T) { + t.Helper() + r, w, i := httpReadTimeout, httpWriteTimeout, httpIdleTimeout + httpReadTimeout, httpWriteTimeout, httpIdleTimeout = 300*time.Millisecond, 300*time.Millisecond, 300*time.Millisecond + t.Cleanup(func() { httpReadTimeout, httpWriteTimeout, httpIdleTimeout = r, w, i }) +} + +// stalled sends a request that promises a body and never finishes it, and +// reports whether the server hung up within a few seconds. +func stalled(t *testing.T, addr, req string) bool { + t.Helper() + c, err := net.Dial("tcp", addr) + if err != nil { + t.Fatal(err) + } + defer c.Close() + if _, err := c.Write([]byte(req)); err != nil { + t.Fatal(err) + } + c.SetReadDeadline(time.Now().Add(4 * time.Second)) + _, err = io.Copy(io.Discard, c) // returns nil on EOF, an error on our deadline + return err == nil +} + +func TestSlowRequestsAreCutOnBothServers(t *testing.T) { + shortTimeouts(t) + relayAddr, _ := startSink(t) + a, err := Start(testConfig(t, relayAddr), "tok") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { a.Close() }) + hdr := "POST %s HTTP/1.1\r\nHost: x\r\nAuthorization: Bearer tok\r\nContent-Type: application/json\r\nContent-Length: 500\r\n\r\n{\"name\":" + for name, c := range map[string]struct{ addr, path string }{ + "api": {a.APIAddr, "/api/v1/lists"}, + "web": {a.WebAddr, "/unsubscribe/tok"}, + } { + if !stalled(t, c.addr, strings.Replace(hdr, "%s", c.path, 1)) { + t.Errorf("%s: a stalled body was not cut", name) + } + } + // An idle keep-alive connection is closed too. + for name, addr := range map[string]string{"api": a.APIAddr, "web": a.WebAddr} { + if !stalled(t, addr, "GET /nothing HTTP/1.1\r\nHost: x\r\n\r\n") { + t.Errorf("%s: an idle connection was kept", name) + } + } +} + +// slowReader hands out its data in two halves, a pause apart. +type slowReader struct { + parts [][]byte + pause time.Duration +} + +func (s *slowReader) Read(p []byte) (int, error) { + if len(s.parts) == 0 { + return 0, io.EOF + } + if len(s.parts) == 1 { + time.Sleep(s.pause) + } + n := copy(p, s.parts[0]) + s.parts = s.parts[1:] + return n, nil +} + +func TestArchiveImportIsNotCutByTheDefaultTimeouts(t *testing.T) { + shortTimeouts(t) + relayAddr, _ := startSink(t) + a, err := Start(testConfig(t, relayAddr), "tok") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { a.Close() }) + call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev"}`, "tok") + + m1 := "From a@example.com Tue Jan 2 15:04:05 2024\nFrom: a@example.com\nSubject: one\nMessage-ID: <1@x>\n\nbody one\n\n" + m2 := "From a@example.com Tue Jan 2 15:04:06 2024\nFrom: a@example.com\nSubject: two\nMessage-ID: <2@x>\n\nbody two\n" + body := &slowReader{parts: [][]byte{[]byte(m1), []byte(m2)}, pause: 3 * httpReadTimeout} + req, _ := http.NewRequest("POST", "http://"+a.APIAddr+"/api/v1/lists/1/archive/import", body) + req.Header.Set("Authorization", "Bearer tok") + req.ContentLength = -1 // chunked: the body is streamed + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("import: %v", err) + } + defer resp.Body.Close() + b, _ := io.ReadAll(resp.Body) + if resp.StatusCode != 200 || !strings.Contains(string(b), `"imported":2`) { + t.Fatalf("import = %d %s", resp.StatusCode, b) + } +} + +func TestHTTPServersCarryTheTimeouts(t *testing.T) { + relayAddr, _ := startSink(t) + a, err := Start(testConfig(t, relayAddr), "tok") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { a.Close() }) + if httpReadTimeout != 30*time.Second || httpWriteTimeout != 60*time.Second || httpIdleTimeout != 120*time.Second { + t.Fatalf("defaults: %v %v %v", httpReadTimeout, httpWriteTimeout, httpIdleTimeout) + } + for name, s := range map[string]*http.Server{"api": a.web, "web": a.pub} { + if s.ReadTimeout != httpReadTimeout || s.WriteTimeout != httpWriteTimeout || s.IdleTimeout != httpIdleTimeout || s.ReadHeaderTimeout == 0 { + t.Errorf("%s: timeouts %v %v %v %v", name, s.ReadHeaderTimeout, s.ReadTimeout, s.WriteTimeout, s.IdleTimeout) + } + } +} diff --git a/internal/archive/archive_test.go b/internal/archive/archive_test.go index c2b50b2..d40ddd2 100644 --- a/internal/archive/archive_test.go +++ b/internal/archive/archive_test.go @@ -297,11 +297,11 @@ func TestManyPartsAreBounded(t *testing.T) { } func TestOnlyTheTextPartsThatAreNeededAreRead(t *testing.T) { - // 60 text parts of 1 MiB each: the first wins and the rest are not read. + // 30 text parts of 1 MiB each: the first wins and the rest are not read. big := strings.Repeat("a", MaxBody) var b strings.Builder b.WriteString("Subject: big\r\nContent-Type: multipart/mixed; boundary=b\r\n\r\n") - for i := 0; i < 60; i++ { + for i := 0; i < 30; i++ { b.WriteString("--b\r\nContent-Type: text/plain\r\n\r\n" + big + "\r\n") } raw := []byte(b.String())