Cap request body size (HTTP 413)

LimitRequestBodyMiddleware (pure ASGI, registered outermost) rejects request
bodies larger than PIRATS_MAX_BODY_BYTES (default 1 MiB) before they're buffered
into memory: it checks the declared Content-Length first, then counts the bytes
actually streamed so a chunked/length-omitting client can't bypass the header
check. Exposed as services.pirats.maxBodyBytes and documented in the README.

Tested in isolation (Content-Length fast path + streamed path) and through the
real app.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-15 15:59:48 -07:00
parent 122e66b817
commit 692c6d26c1
5 changed files with 140 additions and 1 deletions

View File

@@ -128,6 +128,11 @@
default = "info";
description = "Minimum severity of log messages to record.";
};
maxBodyBytes = lib.mkOption {
type = lib.types.ints.positive;
default = 1048576;
description = "Reject request bodies larger than this many bytes (HTTP 413).";
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
@@ -181,6 +186,7 @@
PIRATS_PURGE_INTERVAL_HOURS = toString cfg.purge.intervalHours;
PIRATS_PURGE_FINISHED_DAYS = toString cfg.purge.finishedDays;
PIRATS_PURGE_INACTIVE_DAYS = toString cfg.purge.inactiveDays;
PIRATS_MAX_BODY_BYTES = toString cfg.maxBodyBytes;
};
serviceConfig = {