Request size and pace safety by default (pre-auth and API body limits, route inventory, slow-body deadline) #870

Closed
opened 2026-10-10 13:20:45 +00:00 by Cordy · 1 comment
Owner

Raised by Nikola on 2026-10-10 about the "hand-kept list" in the #843 body-cap guard test. This issue holds a deep-research pass and the design options.

Today (v0.6.325)

  • The pre-auth routes cap their bodies at 64 KiB via http.MaxBytesReader: sign-in, setup, the storage wizard, the share password form and the OCM secret form. Uploads (share drop, OCM grant PUT) are marked exempt.
  • A source-scan test checks a hand-listed set of files. A new session-less route in another file is invisible to it (e.g. internal/api/wopi.go PUT and internal/peering/blob.go are not listed today).
  • Authenticated JSON APIs (/api/v1/...) have no body cap at all.
  • The server sets ReadHeaderTimeout (10s) and IdleTimeout, and deliberately no ReadTimeout (WebDAV uploads). So a client can send a capped 64 KiB body at 1 byte every 10s and hold a goroutine and connection for a week.
  • Go's ServeMux has no public API to list registered routes. Reading it by reflection breaks across Go versions; the 1.22 rewrite moved the internals (jvt.me).
  • The dogfood's public path goes through Caddy on the VPS. Caddy enforces no body limit unless request_body { max_size } is set (Caddy docs).

What broke elsewhere, and what stuck

Same bug class (unbounded body, often unauthenticated)

  • Mailpit, CVE-2026-45713 (7.5): an unauthenticated memory-exhaustion DoS. json.NewDecoder(r.Body) ran with no MaxBytesReader, the same pattern Cairn fixed in #843 (advisory).
  • Rancher, CVE-2024-58259: no body limits on public and authenticated API endpoints. The fix is a global middleware (APIBodyLimitingHandler built on http.MaxBytesHandler) with a 1 MiB default for all API routes, not per-handler caps (summary, Wiz).
  • Argo Workflows, GHSA-jcc8-g2q4-9fxq: the whole body was read before auth (advisory).
  • Kubernetes, CVE-2019-11253 ("billion laughs" via YAML/JSON): anonymous users could submit it under the old default RBAC. Lesson: a size cap alone doesn't stop small payloads that expand in a parser (Red Hat, The New Stack).

Go itself

  • Proposal Server.MaxBodyBytes (golang/go#39567): per-handler limits are "quite verbose, and easy to forget to do leading to a vulnerable endpoint". Brad Fitzpatrick argued it should be a handler wrapper instead, and Go 1.18 shipped http.MaxBytesHandler. The thread notes that HTTP/2 lacked the plumbing for a server knob, so the wrapper is the robust form.

  • Stdlib parser advisories show limits must cover parsing, not just bytes:

    • multipart CVE-2022-41724, CVE-2023-24536 (part cap 1000, header cap 10000) and CVE-2023-45290 (long lines);
    • cookies CVE-2025-58186;
    • HTTP/2 trailers CVE-2026-78659 / GO-2026-6603.

    Sources: go#65383, GO-2023-1705, go#75672, go#81857. Keeping the Go toolchain current is part of this.

  • Stack-depth bugs in decoders: encoding/xml (CVE-2022-30633), gob (CVE-2022-30635) and asn1 (CVE-2026-33818) all needed nesting limits added. encoding/json has a fixed nesting cap (go#31789 context).

Pitfalls hit while fixing

  • The 413 gets lost.
    • GitLab's artifact registry answered 400 "malformed JSON" instead of 413 for chunked over-cap bodies. Whether the typed *http.MaxBytesError survives json.Decoder depends on the decoder's buffer state at the cutoff.
    • Their fix: one shared decode helper, plus a 1-byte probe read, since a tripped MaxBytesReader replays its error (issue 394, MR 1149).
    • Gin had the same bug (gin#3841).
  • Content-Length pre-checks misfire behind proxies. GitLab removed theirs after a load balancer's added Content-Length rejected uploads at the wrong limit (MR 2186). Enforce on bytes read; treat the header only as a fast path.
  • io.LimitReader truncates silently. Partial JSON or forms can parse. MaxBytesReader errors instead. Cairn still accepts LimitReader in the guard (ocm/directory.go dirBook).
  • Multiple layers each enforce their own limit. nginx defaults to client_max_body_size 1m. Traefik's buffering default is documented as unlimited, but users hit an effective ~1 MB cap (Traefik community). A 413 from the proxy is invisible to the app, and an app cap is useless if the proxy buffers first.
  • Global caps break legitimate uploads unless the exemptions are explicit. The Gin bodylimit patterns cap mutating methods by default and pass named upload paths through (example). WebDAV PUT, TUS PATCH, share drop, OCM grant, peering blobs and WOPI PUT are Cairn's large-body routes.
  • Slow bodies. A server-wide ReadTimeout that stops slow-drip attacks also kills long WebDAV uploads. That's why Go added per-handler deadlines (http.ResponseController.SetReadDeadline, Go 1.20, go#54136). Kubernetes adopted per-request deadlines for the same reason (KEP-4460). Pitfall: deadlines can't be extended once exceeded, and they don't work under http.TimeoutHandler.

Framework defaults (what "secure by default" looks like elsewhere)

Framework Default limit
Express body-parser 100 KB
Django about 2.5 MB for form bodies (not JSON)
Spring/Tomcat about 2 MB for POST
nginx 1 MB
Rancher (since the CVE) 1 MiB on all API routes
  • OWASP API4:2023 lists a missing "maximum upload file size" and missing memory caps as vulnerabilities, for all APIs, not only pre-auth ones (OWASP API4).
  • Route-inventory tests that read the router itself, not a list, are the pattern that stuck elsewhere: Fastify route hooks, and Rails 8's opt-out allow_unauthenticated_access (Rails security guide).

Options

A. Inventory test only.

  • Keep per-handler caps.
  • Replace the file list with a registration wrapper for the top-level mux, which records every session-less route, its body policy and its reason.
  • A test fails on any route without a policy.

This still depends on each handler remembering its cap.

B. Secure by default (recommended, the Rancher/Gin pattern).

  • One middleware at the top-level handler caps every request body:
    • 64 KiB for session-less routes;
    • 1 MiB for authenticated JSON APIs.
  • Only routes registered as large-body get their own bounded policy: WebDAV PUT, TUS, share drop, OCM grant, peering blob, WOPI PUT, avatar upload. Their bounds come from quota, upload limits or a signed allowance.
  • The registration wrapper records the policy, and the inventory test checks it.
  • A forgotten new route fails safe: it gets a 413, which shows up in testing, instead of failing open.
  • The existing per-handler caps stay as a second layer.

Additions recommended with either option:

  1. Slow-body deadline. Session-less routes get ResponseController.SetReadDeadline(now + 15s). Upload routes get a progress-extended deadline, refreshed as bytes arrive, so a stalled client is cut off but a steady upload finishes.

  2. One decodeJSON(w, r, v) helper with the 1-byte probe, so an over-cap body is always 413, never 400. All JSON handlers use it.

  3. The guard rejects io.LimitReader as a cap. dirBook switches to MaxBytesReader or gets an explicit exemption.

  4. Docs (deployment.md):

    • set a proxy limit (Caddy request_body max_size, nginx client_max_body_size) above Cairn's largest non-streaming body;
    • document what a proxy-level 413 looks like;
    • keep the Go toolchain current.

    Dogfood follow-up: the VPS Caddy config.

Decisions to make

  • Option A or B.
  • Whether authenticated JSON APIs get a default cap (proposed: 1 MiB).
  • Whether to add the slow-body deadline (proposed: yes, 15 s for session-less routes).
  • Batch placement (proposed: into batch-v07e, replacing the planned "mux-derived guard" item, whose route-listing approach turns out to be impossible without a registration wrapper).
Raised by Nikola on 2026-10-10 about the "hand-kept list" in the #843 body-cap guard test. This issue holds a deep-research pass and the design options. ## Today (v0.6.325) - The pre-auth routes cap their bodies at 64 KiB via `http.MaxBytesReader`: sign-in, setup, the storage wizard, the share password form and the OCM secret form. Uploads (share drop, OCM grant PUT) are marked exempt. - A source-scan test checks a **hand-listed set of files**. A new session-less route in another file is invisible to it (e.g. `internal/api/wopi.go` PUT and `internal/peering/blob.go` are not listed today). - Authenticated JSON APIs (`/api/v1/...`) have **no body cap at all**. - The server sets `ReadHeaderTimeout` (10s) and `IdleTimeout`, and deliberately no `ReadTimeout` (WebDAV uploads). So a client can send a capped 64 KiB body at 1 byte every 10s and hold a goroutine and connection for a week. - Go's `ServeMux` has **no public API to list registered routes**. Reading it by reflection breaks across Go versions; the 1.22 rewrite moved the internals ([jvt.me](https://jvt.me/posts/2024/03/04/go-net-http-routes)). - The dogfood's public path goes through Caddy on the VPS. Caddy enforces no body limit unless `request_body { max_size }` is set ([Caddy docs](https://caddyserver.com/docs/caddyfile/directives/request_body)). ## What broke elsewhere, and what stuck ### Same bug class (unbounded body, often unauthenticated) - **Mailpit, CVE-2026-45713 (7.5):** an unauthenticated memory-exhaustion DoS. `json.NewDecoder(r.Body)` ran with no `MaxBytesReader`, the same pattern Cairn fixed in #843 ([advisory](https://advisories.gitlab.com/golang/github.com/axllent/mailpit/CVE-2026-45713/)). - **Rancher, CVE-2024-58259:** no body limits on public and authenticated API endpoints. **The fix is a global middleware** (`APIBodyLimitingHandler` built on `http.MaxBytesHandler`) with a **1 MiB default for all API routes**, not per-handler caps ([summary](https://zeropath.com/blog/cve-2024-58259-rancher-manager-dos-summary), [Wiz](https://www.wiz.io/vulnerability-database/cve/cve-2024-58259)). - **Argo Workflows, GHSA-jcc8-g2q4-9fxq:** the whole body was read before auth ([advisory](https://github.com/argoproj/argo-workflows/security/advisories/GHSA-jcc8-g2q4-9fxq)). - **Kubernetes, CVE-2019-11253 ("billion laughs" via YAML/JSON):** anonymous users could submit it under the old default RBAC. Lesson: a size cap alone doesn't stop small payloads that expand in a parser ([Red Hat](https://access.redhat.com/hydra/rest/securitydata/cve/CVE-2019-11253.json), [The New Stack](https://thenewstack.io/kubernetes-billion-laughs-vulnerability-is-no-laughing-matter/)). ### Go itself - **Proposal `Server.MaxBodyBytes`** ([golang/go#39567](https://github.com/golang/go/issues/39567)): per-handler limits are "quite verbose, and easy to forget to do leading to a vulnerable endpoint". Brad Fitzpatrick argued it should be a handler wrapper instead, and Go 1.18 shipped `http.MaxBytesHandler`. The thread notes that HTTP/2 lacked the plumbing for a server knob, so the wrapper is the robust form. - **Stdlib parser advisories show limits must cover parsing, not just bytes:** - multipart CVE-2022-41724, CVE-2023-24536 (part cap 1000, header cap 10000) and CVE-2023-45290 (long lines); - cookies CVE-2025-58186; - HTTP/2 trailers CVE-2026-78659 / GO-2026-6603. Sources: [go#65383](https://github.com/golang/go/issues/65383), [GO-2023-1705](https://pkg.go.dev/vuln/GO-2023-1705), [go#75672](https://github.com/golang/go/issues/75672), [go#81857](https://github.com/golang/go/issues/81857). Keeping the Go toolchain current is part of this. - **Stack-depth bugs in decoders:** encoding/xml (CVE-2022-30633), gob (CVE-2022-30635) and asn1 (CVE-2026-33818) all needed nesting limits added. encoding/json has a fixed nesting cap ([go#31789](https://github.com/golang/go/issues/31789) context). ### Pitfalls hit while fixing - **The 413 gets lost.** - GitLab's artifact registry answered 400 "malformed JSON" instead of 413 for chunked over-cap bodies. Whether the typed `*http.MaxBytesError` survives `json.Decoder` depends on the decoder's buffer state at the cutoff. - Their fix: one shared decode helper, plus a 1-byte probe read, since a tripped `MaxBytesReader` replays its error ([issue 394](https://gitlab.com/gitlab-org/ops/artifact-registry/-/issues/394), [MR 1149](https://gitlab.com/gitlab-org/ops/artifact-registry/-/merge_requests/1149)). - Gin had the same bug ([gin#3841](https://github.com/gin-gonic/gin/pull/3841)). - **Content-Length pre-checks misfire behind proxies.** GitLab removed theirs after a load balancer's added Content-Length rejected uploads at the wrong limit ([MR 2186](https://gitlab.com/gitlab-org/ops/artifact-registry/-/merge_requests/2186)). Enforce on bytes read; treat the header only as a fast path. - **`io.LimitReader` truncates silently.** Partial JSON or forms can parse. `MaxBytesReader` errors instead. Cairn still accepts `LimitReader` in the guard (`ocm/directory.go` `dirBook`). - **Multiple layers each enforce their own limit.** nginx defaults to `client_max_body_size 1m`. Traefik's buffering default is documented as unlimited, but users hit an effective ~1 MB cap ([Traefik community](https://community.traefik.io/t/incorrect-doc-for-maxrequestbodybytes/25418)). A 413 from the proxy is invisible to the app, and an app cap is useless if the proxy buffers first. - **Global caps break legitimate uploads unless the exemptions are explicit.** The Gin `bodylimit` patterns cap mutating methods by default and pass named upload paths through ([example](https://git.txlyre.website/txlyre/3x-ui/raw/v3.7.0/internal/web/middleware/bodylimit.go)). WebDAV PUT, TUS PATCH, share drop, OCM grant, peering blobs and WOPI PUT are Cairn's large-body routes. - **Slow bodies.** A server-wide `ReadTimeout` that stops slow-drip attacks also kills long WebDAV uploads. That's why Go added per-handler deadlines (`http.ResponseController.SetReadDeadline`, Go 1.20, [go#54136](https://golang.org/issue/54136)). Kubernetes adopted per-request deadlines for the same reason ([KEP-4460](https://www.kubernetes.dev/resources/keps/4460/)). Pitfall: deadlines can't be extended once exceeded, and they don't work under `http.TimeoutHandler`. ### Framework defaults (what "secure by default" looks like elsewhere) | Framework | Default limit | |---|---| | Express body-parser | 100 KB | | Django | about 2.5 MB for form bodies (not JSON) | | Spring/Tomcat | about 2 MB for POST | | nginx | 1 MB | | Rancher (since the CVE) | 1 MiB on all API routes | - OWASP API4:2023 lists a missing "maximum upload file size" and missing memory caps as vulnerabilities, for **all** APIs, not only pre-auth ones ([OWASP API4](https://owasp.org/API-Security/editions/2023/en/0xa4-unrestricted-resource-consumption/)). - Route-inventory tests that read the router itself, not a list, are the pattern that stuck elsewhere: Fastify route hooks, and Rails 8's opt-out `allow_unauthenticated_access` ([Rails security guide](https://guides.rubyonrails.org/security.html)). ## Options **A. Inventory test only.** - Keep per-handler caps. - Replace the file list with a registration wrapper for the top-level mux, which records every session-less route, its body policy and its reason. - A test fails on any route without a policy. This still depends on each handler remembering its cap. **B. Secure by default (recommended, the Rancher/Gin pattern).** - One middleware at the top-level handler caps **every** request body: - 64 KiB for session-less routes; - 1 MiB for authenticated JSON APIs. - Only routes registered as large-body get their own bounded policy: WebDAV PUT, TUS, share drop, OCM grant, peering blob, WOPI PUT, avatar upload. Their bounds come from quota, upload limits or a signed allowance. - The registration wrapper records the policy, and the inventory test checks it. - A forgotten new route fails **safe**: it gets a 413, which shows up in testing, instead of failing open. - The existing per-handler caps stay as a second layer. **Additions recommended with either option:** 1. **Slow-body deadline.** Session-less routes get `ResponseController.SetReadDeadline(now + 15s)`. Upload routes get a progress-extended deadline, refreshed as bytes arrive, so a stalled client is cut off but a steady upload finishes. 2. **One `decodeJSON(w, r, v)` helper** with the 1-byte probe, so an over-cap body is always 413, never 400. All JSON handlers use it. 3. **The guard rejects `io.LimitReader`** as a cap. `dirBook` switches to `MaxBytesReader` or gets an explicit exemption. 4. **Docs** (deployment.md): - set a proxy limit (Caddy `request_body max_size`, nginx `client_max_body_size`) above Cairn's largest non-streaming body; - document what a proxy-level 413 looks like; - keep the Go toolchain current. Dogfood follow-up: the VPS Caddy config. ## Decisions to make - Option A or B. - Whether authenticated JSON APIs get a default cap (proposed: 1 MiB). - Whether to add the slow-body deadline (proposed: yes, 15 s for session-less routes). - Batch placement (proposed: into batch-v07e, replacing the planned "mux-derived guard" item, whose route-listing approach turns out to be impossible without a registration wrapper).
Author
Owner

Decisions (Nikola, 2026-10-10)

  • Option B, secure by default:
    • One body-limit middleware in front of every request: 64 KiB for session-less routes and 1 MiB for the authenticated API.
    • Routes registered as large-body get their own bounded policy: WebDAV, TUS, share drop, OCM grant, peering blob, WOPI PUT, avatar.
    • A registration wrapper records every route's policy, and an inventory test fails on any route that has none.
    • The existing per-handler caps stay as a second layer.
  • Slow-body deadline: 15 s for session-less routes. Upload routes get a deadline that extends as long as data keeps arriving.
  • Also in scope:
    • one decodeJSON helper with the 1-byte probe, so an over-cap body is always 413, never 400;
    • the guard no longer accepts LimitReader as a cap;
    • deployment.md covers proxy limits.
  • Batch: batch-v07e (v0.6.326). This replaces the planned "mux-derived guard" item.
## Decisions (Nikola, 2026-10-10) - **Option B, secure by default:** - One body-limit middleware in front of every request: **64 KiB for session-less routes and 1 MiB for the authenticated API.** - Routes registered as large-body get their own bounded policy: WebDAV, TUS, share drop, OCM grant, peering blob, WOPI PUT, avatar. - A registration wrapper records every route's policy, and an inventory test fails on any route that has none. - The existing per-handler caps stay as a second layer. - **Slow-body deadline:** 15 s for session-less routes. Upload routes get a deadline that extends as long as data keeps arriving. - **Also in scope:** - one `decodeJSON` helper with the 1-byte probe, so an over-cap body is always 413, never 400; - the guard no longer accepts `LimitReader` as a cap; - deployment.md covers proxy limits. - **Batch:** batch-v07e (v0.6.326). This replaces the planned "mux-derived guard" item.
Cordy closed this issue 2026-10-10 15:45:55 +00:00
Sign in to join this conversation.
No labels
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: Cordy/Cairn#870
No description provided.