Skip to content

iis: drop chunked Transfer-Encoding when Content-Length is set - #3618

Open
A13501350 wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
A13501350:iis-fix-chunked-te
Open

A13501350 wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
A13501350:iis-fix-chunked-te

Conversation

@A13501350

@A13501350 A13501350 commented Aug 22, 2026 •

Copy link
Copy Markdown

Summary

In iis/mymodule.cpp, WriteBodyCallback copies the inbound request headers verbatim (including Transfer-Encoding) and then sets Content-Length on the request. The result can be a request that carries both Transfer-Encoding: chunked and Content-Length, which is malformed and is rejected by some backends / causes ambiguous framing.

Change

Right before setting Content-Length, delete Transfer-Encoding unconditionally via pHttpRequest->DeleteHeader(HttpHeaderTransferEncoding). Since the de-chunked body is re-inserted with an authoritative Content-Length, any prior Transfer-Encoding is stale regardless of value, and only one framing header remains on the forwarded request.

Provenance

Adapted from microsoft/ModSecurity branch waf_iis. Verified still missing on current v2/master tip (0875b19, v2.9.14).

@coderabbitai

coderabbitai Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 20ad026e-3b9f-4715-b621-8b24b059cae2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sonarqubecloud

Copy link
Copy Markdown

@fzipi fzipi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @A13501350! I traced this through modsecProcessRequest → WriteBodyCallback. This is the request side: we buffer the body, re-insert the de-chunked bytes with InsertEntityBody and set our own Content-Length. If Transfer-Encoding: chunked is left on the request, anything downstream that honors TE over CL (RFC 9112 §6.3), such as an ARR backend, would parse a body that differs from what ModSecurity inspected. So removing TE, rather than rejecting the request, is the right call: the chunked upload itself was legitimate, and only our rewrite made the headers inconsistent.

  1. (blocking) The exact stricmp(..., "chunked") match misses chunked, chunked, gzip, chunked, trailing whitespace and so on. If HTTP.SYS passes any of these through to the module, those requests would still end up with both headers. Since we set an authoritative Content-Length right after, any Transfer-Encoding is stale at that point, so I'd just drop it unconditionally: pHttpRequest->DeleteHeader(HttpHeaderTransferEncoding);. If you prefer to keep a check, please at least use a case-insensitive substring match, like the engine does in modsecurity.c (m_strcasestr(te, "chunked")).
  2. (non-blocking) Small wording tweak for the comment: something like "we re-insert the de-chunked body with an explicit Content-Length, so Transfer-Encoding no longer applies".
  3. (non-blocking) Once #3613's ARR/albedo harness is in, a chunked POST that checks the forwarded headers would be a nice regression test for this.

@sonarqubecloud

Copy link
Copy Markdown

A13501350 added a commit to A13501350/ModSecurity that referenced this pull request Nov 30, 2026
The previous fix used stricmp() to match "chunked" exactly, which
misses Transfer-Encoding values like "chunked, chunked", "gzip, chunked",
or anything with extra whitespace. Since ModSecurity always forwards
the body with an explicit Content-Length immediately after this point,
any Transfer-Encoding header copied from the original request is stale
and must be removed regardless of its value. Delete unconditionally.
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@A13501350 A13501350 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@fzipi Updated in 69dfb2e1 to drop Transfer-Encoding unconditionally via pHttpRequest->DeleteHeader(HttpHeaderTransferEncoding) and revised the inline comment.

Verified a chunked POST through IIS + ARR + albedo on a test branch: the forwarded request no longer triggers ARR's CL+TE 502 and is inspected in phase 2. The permanent FTW regression test will follow once #3613's iis/tests harness lands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.x Related to ModSecurity version 2.x Platform - IIS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants