Skip to content

ModSecurity IIS module add content-length when chunked transfer encoding is enabled #1364

Open

Description

@maverick64

Setup:
Modsecurity + Application Request Routing(ARR) + IIS

ModSecurity is adding content length header even when chunked encoding is enabled. According to RFC (https://www.ietf.org/rfc/rfc2616.txt)
"Messages MUST NOT include both a Content-Length header field and a
non-identity transfer-coding. If the message does include a non-
identity transfer-coding, the Content-Length MUST be ignored."

It is violating the first line of the above and ARR fails it down the pipeline.
The code is in:
iis/mymodule.cpp

apr_status_t WriteBodyCallback(request_rec *r, char *buf, unsigned int length)
{

CHAR szLength[21]; //Max length for a 64 bit int is 20

ZeroMemory(szLength, sizeof(szLength));

HRESULT hr = StringCchPrintfA(
        szLength, 
        sizeof(szLength) / sizeof(CHAR) - 1, "%d", 
        length);

if(FAILED(hr))
{
	// not possible
}

hr = pHttpRequest->SetHeader(
        HttpHeaderContentLength, 
        szLength, 
        (USHORT)strlen(szLength),
        TRUE);

Was this SetHeader Content-lenght intentional?

Activity

  1. self-assigned this
    on May 5, 2017
  2. added
    2.x
    on Nov 9, 2018
  3. victorhora commented on Nov 9, 2018

    @victorhora
    Contributor

    Hi @maverick64

    Yes, It seems like this was on purpose. See mymodule.cpp#L625-L627

    // If there's no content-length set, we need to set it to avoid chunked transfer mode
    // We can only do it if there is it's the only response to be sent.

    Processing of chunked might not be completely implemented / handled by the IIS module. See: mymodule.cpp#L450-L452

  4. added this to the v2.9.4 milestone on Nov 14, 2018
  5. victorhora commented on Nov 14, 2018

    @victorhora
    Contributor

    It seems like this commit from Microsoft / @allanbomsft does a workaround on this behaviour on IIS: 2bf0300

  6. fzipi commented on Oct 12, 2026

    @fzipi
    Collaborator

    Revisiting this one. The IIS module still sets Content-Length to avoid chunked mode (iis/mymodule.cpp). Two open PRs address exactly this area: #3618 (drop chunked Transfer-Encoding when Content-Length is set) and #3620 (Content-Length formatting). Reviewing and merging those should resolve this issue.

  7. fzipi commented on Oct 12, 2026

    @fzipi
    Collaborator

    Follow-up: labelling as pr available for #3618 and #3620.

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

Metadata

Metadata

Assignees

Labels

2.xPlatform - IISenhancementpr availablewindows

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions