feat(detection): cap request-body inspection by Content-Length (S4)#28
Open
rennf93 wants to merge 1 commit into
Open
feat(detection): cap request-body inspection by Content-Length (S4)#28rennf93 wants to merge 1 commit into
rennf93 wants to merge 1 commit into
Conversation
Add SecurityConfig.detection_max_body_inspect_bytes (default 256 KiB). When a request's Content-Length exceeds the cap, detect_penetration_attempt skips reading and scanning the body, so very large bodies (e.g. on an auth proxy) are not buffered into memory on the detection hot path. The request still proceeds; distinct from detection_max_content_length (regex scan window) and max_request_size (the 413 gate). Async + sync engines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Design-partner feedback S4 — on a high-traffic auth proxy that sometimes receives request bodies up to ~300MB, penetration/body inspection had no size guard at the read point, so a large body was fully buffered + decoded into memory before scanning. This bounds that read.
Change
SecurityConfig.detection_max_body_inspect_bytes(default 262144 / 256 KiB;ge=1024, le=10485760).detect_penetration_attemptnow skips reading + scanning the body when theContent-Lengthheader exceeds the cap (returns a clean detection-miss), avoiding the buffer/decode entirely on the hot path.detection_max_content_lengthonly truncates inside the regex preprocessor — after the full body is already in memory — so it doesn't help here; this bounds the read itself.The cap decision is a small pure helper (
_body_exceeds_inspection_cap) so the hot-path function stays within the project's complexity budget.Scope / safety
Config-only direct read (matches
detection_max_content_length; no route override). Async + unasync sync mirror. Backward compatible — the default (256 KiB) is well above normal bodies.Verification
Part of the coordinated guard-core 3.2.0 release.