Skip to main content
Menu
Advisories

The body-inspection cap is enforced only through Content-Length, so a chunked request makes the scanner buffer the whole body

GHSA-C2R5-9JW9-M8Q5Published Updated GitHub

Description

detection_max_body_inspect_bytes is supposed to bound how much request body the penetration detector ever reads. The check that enforces it reads the Content-Length header, and when that header is absent it returns False, which means "under the cap, go ahead and scan". A request sent with Transfer-Encoding: chunked has no Content-Length, so the middleware calls await request.body() on an unbounded stream. One unauthenticated request took 609 MiB of server memory in my measurement against a 1 KiB cap.

Where it is

guard_core/utils.py at guard-core 3.11.0 (latest release, and unchanged from 3.8.0 where I first read it):

def _body_exceeds_inspection_cap(
    request: GuardRequest, config: "SecurityConfig | None"
) -> bool:
    if config is None:
        return False
    content_length = request.headers.get("content-length")
    if content_length is None:
        return False
    try:
        return int(content_length) > config.detection_max_body_inspect_bytes
    except ValueError:
        return False

Both early returns fail open. detect_penetration_attempt in the same file skips the scan only when that returns True, and otherwise does:

    if _body_exceeds_inspection_cap(request, config):
        return _build_detection_miss()

    try:
        raw_body = (await request.body()).decode()

guard_core/sync/utils.py carries an identical copy of both.

max_request_size is not a backstop, because it is gated on the same header. From guard_core/core/checks/implementations/request_size_content.py:

    content_length = request.headers.get("content-length")
    if not content_length or int(content_length) <= route_config.max_request_size:
        return None

A chunked request passes that check and then reaches the unbounded read.

Who the attacker is

Any client that can reach a route the middleware covers. No account, no token, nothing non-default: enable_penetration_detection defaults to True in guard_core/models.py and SuspiciousActivityCheck is in DEFAULT_CHECK_CLASSES in guard_core/core/checks/factory.py, so the body scan is in the standard pipeline. The request only has to avoid the whitelist.

The client sends a POST with Transfer-Encoding: chunked and keeps writing. The server buffers the full body and then decodes it to str, which costs a second copy on top. The memory is held for the life of the request, so a handful of concurrent connections is enough to push a container past its limit and get it killed. That is the part that reaches beyond the sender: the process that dies serves everyone.

Proof of concept

Executed against a real FastAPI app with SecurityMiddleware added, served by uvicorn, over a real TCP socket. The script writes its own app and starts it: 1165-fastapi-guard-chunked-body-cap-bypass.py, attached.

uv venv -p 3.12 .venv && . .venv/bin/activate
uv pip install "fastapi-guard==7.4.0" "uvicorn==0.35.0" psutil
python -u 1165-fastapi-guard-chunked-body-cap-bypass.py

Versions at the time of the run: fastapi-guard 7.4.0, guard-core 3.8.0, uvicorn 0.35.0, Python 3.12. The server sets detection_max_body_inspect_bytes=1024, the smallest the model accepts, plus detection_scan_body=True, and no Redis, banning or rate limiting.

Results, in the order the script runs them:

  • Liveness check first. A 50-byte body carrying an SQLi pattern is blocked, which proves the body-scan path is actually reached before any of the size work below.
  • Control, 128 MiB body with Content-Length: server RSS delta 0.0 MiB, 0.01 seconds. The cap applies and the body is never read.
  • Same 128 MiB body with Transfer-Encoding: chunked: server RSS delta 609.2 MiB, 21.0 seconds.

Same payload, same endpoint, same configuration, cap set to 1 KiB in both cases. The only difference is the framing.

I re-read the gate at 3.11.0 before sending this and it is byte-identical, in both the async and the sync copy, so the measurement above still describes the current release.

Suggested fix

Treat a missing Content-Length as "cap exceeded" in _body_exceeds_inspection_cap, in both copies, so the unbounded read never happens. That is a one-line change and it fails closed, at the cost of not scanning chunked bodies at all.

If you would rather keep scanning them, read from the stream and stop at detection_max_body_inspect_bytes, then scan that prefix, instead of calling request.body(). That preserves detection on chunked uploads and still bounds the buffer. Same for the malformed-Content-Length branch, which fails open the same way.

One thing you should know before you pick: tests/test_utils/test_body_inspection_cap.py::test_missing_content_length_still_scans and its sync twin assert the current behaviour, so either fix needs those inverted. I flag it because it means the fail-open was written deliberately at some point and you may have had a reason I cannot see from outside.

Why this is not the existing advisories

GHSA-j47q-rc62-w448 (CVE-2025-53539, fixed in 3.0.1) was about the detection regexes backtracking on crafted input, and GHSA-rrf6-pxg8-684g was the bypass of the patched regex. Both are about the patterns. The inspection cap is what bounds how much attacker data those patterns ever see, and this report is about the cap: the memory is consumed by await request.body() before any pattern runs, and the header that is supposed to trigger the cap is simply omitted.

Affected versions

guard-core: I verified the gate is present and unchanged at 3.11.0, and it was present at 3.8.0. I have not bisected the first affected release and would rather you set the lower bound than have me guess it.

fastapi-guard: 7.5.1, the current release, requires guard-core with no upper pin, so a fresh install pulls 3.11.0. The proof of concept was run through fastapi-guard 7.4.0 with guard-core 3.8.0.

Severity

Medium, CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L (5.3).

AV:N and PR:N because it is one unauthenticated HTTP request. AC:L because the whole attack is dropping a header. C:N and I:N because nothing is read or modified. I have put A:L rather than A:H because most deployments sit behind a proxy or ingress with its own body limit, which blunts this before it reaches uvicorn, and the memory is released when the request ends. If your users typically run uvicorn directly, A:H is the honest vector and it scores 7.5. Your call, and I would not argue with the higher one.

How I found it and a note on tooling

I was looking at what bounds the input to the detection patterns after the ReDoS fix, which led me to the cap, and the cap turned out to have a header-shaped hole. I used AI assistance for the audit and for drafting this. I verified by hand the cap gate and the request.body() call in guard_core/utils.py and its sync twin, the identical Content-Length gate on max_request_size in request_size_content.py, the enable_penetration_detection default and the presence of SuspiciousActivityCheck in DEFAULT_CHECK_CLASSES, all at 3.11.0, and I produced the runtime numbers above by running the proof of concept against a live server.

---

Re-filed here from `rennf93/fastapi-guard` at the maintainer's request (`GHSA-xv6g-49vj-7w9c`). The vulnerable code lives entirely in this package, in both `guard_core/utils.py` and `guard_core/sync/utils.py`; fastapi-guard was affected only transitively because it depends on guard-core without an upper pin. The patched artifact is a guard-core release, which is why the advisory belongs here.

Acknowledgments

Thank you to @arpitjain099 for reporting this privately through GitHub's vulnerability reporting, with a clean reproduction and measurement, and for giving us the room to fix it properly. The fix shipped in guard-core 3.12.0.

Affected versions

References

GitHub · Guard-Core/guard-core