Skip to content

fix: document request body plaintext disk spooling and mitigation steps - #381

Open
eilandert wants to merge 1 commit into
owasp-modsecurity:masterfrom
eilandert:fix/request-body-tempfile-security
Open

eilandert wants to merge 1 commit into
owasp-modsecurity:masterfrom
eilandert:fix/request-body-tempfile-security

Conversation

@eilandert

Copy link
Copy Markdown
Contributor

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.

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
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 NOTE comment in ngx_http_modsecurity_access_handler describing the disk-spooling behavior and mitigations.
  • No functional/code changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tomsommer

Copy link
Copy Markdown
Contributor

Confirming the behaviour this comment documents, with two corrections to the wording that I verified against the nginx source:

  • Bodies are not spooled to disk unconditionally. request_body_in_persistent_file only changes what happens to the temp file once nginx decides to create one; the decision itself is still made by client_body_buffer_size (ngx_http_request_body.c): bodies that fit the buffer stay in memory, larger ones are written to client_body_temp_path. So "bypasses client_body_buffer_size" and "even small bodies end up on disk" are not accurate.
  • The flag is required rather than optional: ngx_create_temp_file() unlinks a non-persistent temp file immediately after creating it, and the connector hands the file to libmodsecurity by name (msc_request_body_from_file), which reopens it and reads it back into memory in full. That read-back is also why the spill saves no memory in practice.

The mitigation advice (tmpfs-backed client_body_temp_path, sizing client_body_buffer_size) is sound. #393 documents the same behaviour for operators in the README, worded so that it agrees with the corrected version of this comment. Found during a performance review of the connector done with Claude Fable 5.1.

@fzipi

fzipi commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

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.

@fzipi

fzipi commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

@airween I don't have permission to close PRs in this repository. Could you close this one, per the comment above? Thanks.

tomsommer pushed a commit to tomsommer/ModSecurity-nginx that referenced this pull request Nov 13, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants