Skip to content

IIS: preserve duplicate request/response headers with same name - #3690

Open
A13501350 wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
A13501350:fix-2991-iis-duplicate-headers
Open

A13501350 wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
A13501350:fix-2991-iis-duplicate-headers

Conversation

@A13501350

Copy link
Copy Markdown

Fixes #2991

Problem

On IIS, when multiple response headers share the same name (e.g. Set-Cookie), ModSecurity only records the last one. Consequently, rules using &RESPONSE_HEADERS:/Set-Cookie/ (count) or RESPONSE_HEADERS:Set-Cookie never match earlier values, and audit log part F collapses duplicates to a single entry. The same defect exists for request-side unknown headers.

Root cause

In iis/mymodule.cpp, both OnSendResponse and OnBeginRequest copy unknown headers into the corresponding APR table with apr_table_setn(), which replaces any existing entry with the same key. HTTP.SYS already exposes every duplicate as a separate HTTP_UNKNOWN_HEADER entry in pUnknownHeaders[], but each loop iteration overwrote the previous one, so only the last value survived.

Fix

Switch both unknown-header loops from apr_table_setn() to apr_table_addn(), preserving all values of identically-named headers. ModSecurity's collection generators already iterate over every table entry, so duplicate headers become fully visible to rules and the audit log.

@coderabbitai

coderabbitai Bot commented Oct 14, 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: 879ed27e-5476-4ed4-8b43-81b6d2fb6aab

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

@airween airween added the 2.x Related to ModSecurity version 2.x label Oct 15, 2026
@airween

airween commented Oct 15, 2026

Copy link
Copy Markdown
Member

Hi @A13501350,

thanks for this patch - if I'm right, this fixes a bug. Could you add a test case which shows the issue and the correct behavior? The test could be here, with a new target, for eg. 10-response-headers.t.

@A13501350

A13501350 commented Oct 15, 2026 •

Copy link
Copy Markdown
Author

Happy to add tests. However, tests/regression/target looks like the engine + Apache test suite, while this fix is IIS-connector-specific only. I think the ideal approach would be to carve out connector-specific tests first (as done in #3613, where the IIS tests live under iis/tests/) and add them there.

@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, the change looks right to me.

I reviewed it by reading the code; I haven't built or run it. Windows CI is green.

  • Switching both unknown-header loops to apr_table_addn fixes the overwrite. The keys and values are already copied into r->pool, so the no-copy variant is fine.
  • Your Set-Cookie explanation matches what the reporter saw in #2991: both Set-Cookie and a custom header only showed the last instance.
  • I checked the engine's single-value lookups (apr_table_get on request/response headers). They only use known header names (Content-Type, Content-Length, Transfer-Encoding, Referer, User-Agent, …), which still go through _TRANSHEADER. Moving from "last wins" to "first wins" for unknown names doesn't change anything there.
  • Nice side effect on the request side: a client can no longer hide a value in an earlier duplicate custom header from REQUEST_HEADERS inspection.

On tests: I agree with @airween that we want one. You're also right that tests/regression/target runs against Apache, which already keeps duplicates, so a test there would pass with or without this change. Once the iis/tests harness from #3613 lands, could you add a small FTW case? Something like two X-Test request headers (via encoded_request, since the header map can't hold duplicate names) and SecRule &REQUEST_HEADERS:X-Test "@eq 2" "id:...,phase:1,deny,status:403". I'm fine with that being a follow-up rather than blocking this.

@airween

airween commented Nov 29, 2026

Copy link
Copy Markdown
Member

Thanks for this, and sorry it has waited this long. Switching to apr_table_addn is the right direction, and the test follow-up @fzipi and I asked for can wait for #3613 as discussed. Before we merge, there is one behaviour change I would like to understand.

Duplicates of known headers. @fzipi checked that the engine's single-value lookups (apr_table_get) only use known header names, which go through _TRANSHEADER, so moving from "last wins" to "first wins" for unknown names changes nothing there. But your commit message says HTTP.SYS also pushes duplicates of a known header into the unknown list ("HTTP.SYS keeps one value in the known header slot and pushes the rest into the unknown header list ... and also clobbered the known slot value"). If that happens, the known names are affected: before this PR the copy from the unknown list overwrote the known-slot value, so apr_table_get returned the last one; after it, it returns the first one. That matters for Content-Type, Content-Length and Transfer-Encoding on the request side, which the engine uses to choose the body processor and read the body, and for Content-Encoding and Location on the response side.

Your commit message describes this for Set-Cookie in responses, which come from the application. What I do not know is whether HTTP.SYS does the same for duplicated request headers sent by a client, or whether it rejects or merges them.

Most backends take the last copy of a duplicated Content-Type. If ModSecurity now takes the first, a request with two Content-Type headers could be parsed as urlencoded by us and as JSON or multipart by the application, so the body arguments would not be inspected.

Could you check on IIS what HTTP.SYS really does with a request that has two Content-Type headers (and two Content-Length / Transfer-Encoding): does the second copy end up in pUnknownHeaders, or does HTTP.SYS reject or merge it? If it does end up there, we should handle those names explicitly rather than silently switch from last-wins to first-wins.

Not caused by this PR, just so we know what it does not fix:

  • With SecResponseBodyAccess Off, or a response without entity chunks (204/304, HEAD), OnSendResponse returns before copying any response header. So in that setup RESPONSE_HEADERS:Set-Cookie from IIS multiple response headers with the same name #2991 stays empty even after this PR.
  • With SecAuditLogFormat JSON, repeated headers become repeated keys in the same JSON object (Repeated headers cause problems in json audit logs #2777). On IIS this could not happen before, because duplicates were collapsed.
  • Both functions you touched look up "Content-Languages", which is not a real header name (it is Content-Language), so r->content_languages is never set. Feel free to fix it here or leave it.

@airween

airween commented Nov 30, 2026

Copy link
Copy Markdown
Member

One addition to my previous comment, for the test that will come after #3613: please make it check exact counts in both directions, not just "more than one", e.g. two X-Test request headers should give &REQUEST_HEADERS:X-Test = 2, and two Set-Cookie response headers should give &RESPONSE_HEADERS:Set-Cookie = 2. That way the test also shows whether HTTP.SYS keeps duplicate unknown request headers as separate entries (rather than joining them with a comma), and that the known-header value is not counted twice on the response side.

Not for this PR: standalone/main.cpp has the same apr_table_setn when it copies request headers, so duplicates collapse to the last value there as well. I will open a separate issue for it.

Unknown headers were copied into headers_in/headers_out with
apr_table_setn(), so any duplicate entries in pUnknownHeaders overwrote
the previous value and only the last survived. For example, on the
response side (such as multiple Set-Cookie headers or custom headers),
IIS/HTTP.SYS keeps one known-header value in KnownHeaders and places
additional instances in pUnknownHeaders, so previously only the last
value was visible and the KnownHeaders entry was clobbered.

Switch both unknown-header loops to apr_table_addn() so every entry
exposed in pUnknownHeaders is preserved on both request and response
sides (note that on the request side, HTTP.SYS usually rejects or
coalesces duplicate client headers before the module runs).

Fixes owasp-modsecurity#2991
@A13501350

Copy link
Copy Markdown
Author

@airween Thanks for the thorough check. I tested duplicated request and response headers directly against IIS/HTTP.SYS on windows-latest to answer your question:

Request side (HTTP.SYS -> OnBeginRequest):
HTTP.SYS never pushes a second copy of duplicate client request headers into pUnknownHeaders; it either rejects or coalesces them before the module runs:

  • Duplicate Content-Type (application/x-www-form-urlencoded + application/json) -> HTTP.SYS rejects with 400 Bad Request (never reaches ModSecurity).
  • Duplicate Content-Length: mismatched (3, 5) -> 400 Bad Request; identical (3, 3) -> deduplicated to a single Content-Length: 3.
  • Duplicate Transfer-Encoding: chunked + chunked -> 501 Not Implemented; gzip + chunked -> coalesced into a single Transfer-Encoding: gzip, chunked.
  • Similarly, duplicate User-Agent -> 400 Bad Request; duplicate Cookie -> coalesced to Cookie: a=1, b=2; duplicate unknown header X-Test -> coalesced to X-Test: one, two.

So on the request side, apr_table_get for Content-Type, Content-Length, and Transfer-Encoding never collides with pUnknownHeaders and does not shift from last-wins to first-wins.

Response side (ARR backend -> OnSendResponse):

  • Duplicate Set-Cookie and duplicate custom X-Resp-Test are both preserved in r->headers_out, audit log Part F, and &RESPONSE_HEADERS:* counts (2).
  • Note: when tested through ARR, ARR itself collapsed duplicate Content-Encoding (br) and rewritten Location to a single header before OnSendResponse.

@A13501350
A13501350 force-pushed the fix-2991-iis-duplicate-headers branch from a4df483 to 484e405 Compare December 1, 2026 12:09
@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

@A13501350

Copy link
Copy Markdown
Author

@airween @fzipi The CI/CD for IIS Module failure on run 46643442124 (curl operation failed with response code 618 while vcpkg was downloading pcre2-10.49.tar.gz) was caused by yesterday's GitHub Releases CDN incident:

The incident has since been resolved, and the failed workflow needs to be re-run.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants