Repository navigation
Conversation
request_body_in_persistent_file unconditionally spools all client request bodies to disk as plaintext temporary files. This bypasses client_body_buffer_size, meaning even small bodies end up on disk. In environments handling sensitive POST data (authentication, payment, health records), this creates plaintext files readable by any process with access to client_body_temp_path, which defaults to a world- accessible directory under the nginx prefix. Add a security comment at the point of the flag assignment documenting: - The nature of the exposure (plaintext temp files regardless of buffer) - The recommended mitigation: client_body_temp_path to a tmpfs mount such as /dev/shm/nginx_body with 0700 permissions - The recommended size limits to constrain disk usage A proper fix would only spool to disk when body size exceeds client_body_buffer_size and use the configured temp path with restrictive permissions. This comment serves as a clear callout for operators reviewing the source and for future refactoring. Severity: High in multi-tenant environments, Medium in single-tenant Reported-by: Security audit 2026-05-13
|
There was a problem hiding this comment.
Pull request overview
This PR adds an in-source security comment above the r->request_body_in_persistent_file = 1; assignment in the ModSecurity-nginx access handler. The comment documents that ModSecurity-nginx forces request bodies to be spooled to disk as plaintext, and suggests operator mitigations (tmpfs-backed client_body_temp_path, restrictive permissions, and size limits).
Changes:
- Adds a multi-line
SECURITY NOTEcomment inngx_http_modsecurity_access_handlerdescribing the disk-spooling behavior and mitigations. - No functional/code changes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Confirming the behaviour this comment documents, with two corrections to the wording that I verified against the nginx source:
The mitigation advice (tmpfs-backed |
|
Closing in favor of #393, which documents request body buffering and temp-file spooling in the README. As noted in the review here, request_body_in_persistent_file does not bypass client_body_buffer_size (small bodies stay in memory), so the comment as written would be inaccurate. Thanks for the contribution and for your patience. |
|
@airween I don't have permission to close PRs in this repository. Could you close this one, per the comment above? Thanks. |
Request body: - Only "modsecurity off" avoids the buffering and the copy. The access handler reads the body whenever the module is enabled, and Transaction::appendRequestBody/appendResponseBody do not check the rule engine state; only the process* phases do. - Spill threshold: ngx_http_read_client_request_body uses size = client_body_buffer_size + (client_body_buffer_size >> 2) and allocates rest (+ preread, request_body_in_single_buf) when a non-chunked rest is below that, so HTTP/1.x bodies with a known length stay in memory up to ~1.25x. Chunked bodies get client_body_buffer_size; ngx_http_v2_read_request_body allocates min(Content-Length + 1, client_body_buffer_size). A body whose rest fits in r->header_in uses that buffer and allocates nothing. - Transaction::requestBodyFromFile reads the whole file into a local std::string before appendRequestBody copies it into m_requestBody, so the body is held twice while loading and the file read is not bounded by SecRequestBodyLimit (which only bounds m_requestBody). - request_body_in_persistent_file keeps the temp file named (ngx_open_tempfile unlinks only non-persistent files); it is opened 0600 and removed by ngx_pool_delete_file because the connector sets request_body_in_clean_file. request_body_file_group_access (0660) is set only by the dav module, after the connector has already read the body. ngx_create_paths creates a missing temp path 0700 but does not tighten an existing one. Covers what owasp-modsecurity#381 documented. - Transaction::processRequestBody runs the multipart/urlencoded/JSON/XML processors before the SecRequestBodyAccess early return; the DisabledRuleEngine return comes before them. Response body: - ngx_output_chain reads in_file buffers into at most output_buffers (default 2 32k) temp buffers and keeps in_file set for sendfile. - SecResponseBodyMimeTypesClear sets m_set with an empty set: appendResponseBody only skips when the set is non-empty, processResponseBody skips when m_set and no match. - Phase 4 runs after the header filter has passed the headers on; with r->header_sent the intervention returns -1 and the body filter calls ngx_http_filter_finalize_request, so the response is aborted (tests/modsecurity.t expects '' for /phase4). Rules loading: - ngx_http_modsecurity_create_conf calls msc_create_rules_set for every location conf and merge_conf always calls msc_rules_merge, which appends shared_ptr references per phase after a duplicate id check.



request_body_in_persistent_file unconditionally spools all client request bodies to disk as plaintext temporary files. This bypasses client_body_buffer_size, meaning even small bodies end up on disk.
In environments handling sensitive POST data (authentication, payment, health records), this creates plaintext files readable by any process with access to client_body_temp_path, which defaults to a world- accessible directory under the nginx prefix.
Add a security comment at the point of the flag assignment documenting:
A proper fix would only spool to disk when body size exceeds client_body_buffer_size and use the configured temp path with restrictive permissions. This comment serves as a clear callout for operators reviewing the source and for future refactoring.