Skip to content

fix: MULTIPART_UNMATCHED_BOUNDARY false positives - #3658

Open
airween wants to merge 5 commits into
owasp-modsecurity:v2/masterfrom
airween:v2/fix-multipart-unmatched-boundary
Open

airween wants to merge 5 commits into
owasp-modsecurity:v2/masterfrom
airween:v2/fix-multipart-unmatched-boundary

Conversation

@airween

@airween airween commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

what

This PR ports the v3 MULTIPART_UNMATCHED_BOUNDARY fix (#1747, #2417, #3654) to v2. It also changes how v2 handles a multipart body that cannot be completed.

1. MULTIPART_UNMATCHED_BOUNDARY can be 0, 1 or 2

Value Meaning
0 every boundary-like line matched the boundary from the Content-Type header
1 a boundary-like line (starting with --) was not followed by a valid boundary
2 a boundary-like line was followed by a valid boundary, so it was most likely part data, e.g. a PEM file (-----BEGIN ...)
  • The 1 → 2 change only happens after a boundary has been fully validated.
  • This also covers a final boundary without a trailing CRLF (multipart_complete()).
  • The value never becomes 2 unless it was 1 before. This is the bug that fix MULTIPART_UNMATCHED_BOUNDARY #2417 fixed in v3.

2. No HTTP 500 when multipart_complete() fails

Until now, a multipart body that could not be completed made read_request_body() return HTTP_INTERNAL_SERVER_ERROR. Examples are a missing final boundary, no boundary at all, or a body truncated by ProcessPartial. Phase 2 never ran, whatever rules were configured.

Now the error is reported the same way as in v3:

  • REQBODY_ERROR = 1 and REQBODY_ERROR_MSG is set, and the MULTIPART_* flags are available.
  • The phase 2 rules decide what happens. With the recommended configuration, rule 200002 returns 400.
  • The error is logged at level 2, so it still appears in the error log.
  • Arguments (ARGS) are not extracted from an incomplete body, like in v3. Variables built from parts that were already completed (FILES*, MULTIPART_NAME, MULTIPART_FILENAME, MULTIPART_PART_HEADERS) remain available, as they already were for other multipart parsing errors in v2.

3. Documentation and tests

  • modsecurity.conf-recommended now documents the possible values, including a summary table. The default rule 200004 stays strict (!@eq 0).
  • New tests: tests/regression/target/10-variable-multipart_unmatched_boundary.t (8 cases, the same scenarios as the v3 tests in fix: MULTIPART_UNMATCHED_BOUNDARY is set to 2 for well-formed multipart bodies #3654).
  • Existing tests that expected 500 now expect the rules' result (400 / 403 / 200):
    • misc/00-multipart-parser.t
    • target/10-variable-multipart_strict_error.t
    • config/10-request-directives.t

why

  • In v2, every line starting with -- that doesn't match the boundary sets MULTIPART_UNMATCHED_BOUNDARY to 1. Rule 200004 then blocks valid uploads, such as PEM certificates or text files with -- lines. This has been a long-standing false-positive source.

  • A v2 port was already submitted in Fix MULTIPART_UNMATCHED_BOUNDARY FP errors #2193. @martinhsv closed it without merging in January 2022. His reasons were that it mirrored the v3 implementation, which had known problems:

    1. the value became 2 even when no part contained a -- line;
    2. -- content in the last part was not handled;
    3. too many cases that really are unmatched no longer resulted in 1.

    He suggested revising the v3 solution first and porting it to v2 afterwards. He also added test cases in MULTIPART_UNMATCHED_BOUNDARY=1 functionality is broken #2681 and summarised the open questions in fix MULTIPART_UNMATCHED_BOUNDARY #2417.

  • That is the order followed here: the v3 logic was fixed and finalized in fix: MULTIPART_UNMATCHED_BOUNDARY is set to 2 for well-formed multipart bodies #3654, and this PR ports that result. Point by point:

    1. Fixed: the value can only become 2 if it was 1 before.
    2. Fixed: the transition also happens on the final boundary, including one without a trailing CRLF. There is a dedicated test for this.
    3. A design decision rather than a bug. A -- line followed by a valid boundary gives 2, not 1. Because the default v2 rule stays strict (!@eq 0), nothing is lost in detection: 1 and 2 are both blocked. Accepting such bodies (permissive mode, @eq 1) is an explicit opt-in.
  • The 500 response had to go for two reasons:

    • It hid the multipart state from the rules: the variables this PR introduces could never be evaluated on a broken body.
    • v3 already lets the rules decide in the same situation, and v2 and v3 should behave the same.

Behavior changes — please read

Situation Before After
Broken / incomplete multipart body, SecRuleEngine On, recommended config 500 400 (rule 200002)
Same, but no rule on REQBODY_ERROR 500 the request is passed to the backend
Multipart body truncated with SecRequestBodyLimitAction ProcessPartial 500 the rules decide (REQBODY_ERROR = 1, so 200002 → 400)
Part data line starting with --, followed by a valid boundary 1 2
Boundary-like line not followed by a valid boundary 1 1

What this means for users:

  • Make sure you have a rule on REQBODY_ERROR (rule 200002 in modsecurity.conf-recommended). Without one, a malformed multipart body is no longer rejected by the engine itself.
  • When the body is broken, rules in phase 2 and later don't see any multipart ARGS. Only REQBODY_ERROR and the MULTIPART_* flags tell you what happened.
  • MULTIPART_UNMATCHED_BOUNDARY can now be 2 in v2. Rules or log formats that expect only 0/1 (e.g. @streq 1, or parsing %{MULTIPART_UNMATCHED_BOUNDARY}) should be reviewed.
  • With the default !@eq 0, blocking does not change for this variable. If you change rule 200004 to @eq 1, PEM-like content is allowed.
  • The multipart parser behavior will be explained in more detail in a blog post accompanying the release.

references

…t errors

Port of the v3 fix (owasp-modsecurity#1747, owasp-modsecurity#2417, owasp-modsecurity#3654):
- MULTIPART_UNMATCHED_BOUNDARY is 1 when a boundary-like line is not
  followed by a valid boundary, and 2 when a valid boundary follows it.
  The 1 -> 2 transition happens only after a fully validated boundary,
  also for a final boundary without trailing CRLF.
- A multipart_complete() failure no longer aborts the transaction with
  HTTP 500: REQBODY_ERROR and the MULTIPART_* variables are set, and the
  phase 2 rules decide (same behaviour as v3). Arguments are not added
  when the body is incomplete.
- Document the variable in modsecurity.conf-recommended.
- Add tests and update the expected statuses of the tests that relied
  on the 500 response.

Co-Authored-By: Claude Opus 5.5
@airween
airween requested a balanced review from Copilot October 12, 2026 16:05
@coderabbitai

coderabbitai Bot commented Oct 12, 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
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7512a6cf-aa03-4b98-a2b0-7cd08d902329

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

Comment @coderabbitai help to get the list of available commands.

@airween
airween requested review from fzipi and hnakamur October 12, 2026 16:05
@airween airween added the 2.x Related to ModSecurity version 2.x label Oct 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Incomplete requests still expose completed file variables, and the documentation inaccurately describes value 1.

3 open findings
What changed in this PR

Ports v3 multipart boundary handling to v2 and delegates incomplete-body responses to phase-2 rules.

Changes:

  • Adds MULTIPART_UNMATCHED_BOUNDARY values 0/1/2.
  • Reports completion failures through REQBODY_ERROR instead of HTTP 500.
  • Updates configuration guidance and regression tests.
File Description
apache2/​msc_multipart.c Classifies unmatched boundaries.
apache2/​msc_reqbody.c Defers completion errors to rules.
apache2/​re_variables.c Exposes numeric boundary state.
modsecurity.conf-recommended Documents boundary behavior.
tests/​regression/​config/​10-request-directives.t Updates partial-body expectation.
tests/​regression/​misc/​00-multipart-parser.t Updates parser error responses.
tests/​regression/​target/​10-variable-multipart_strict_error.t Updates malformed-body expectations.
tests/​regression/​target/​10-variable-multipart_unmatched_boundary.t Adds boundary-state coverage.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread apache2/msc_reqbody.c Resolved
Comment thread modsecurity.conf-recommended Resolved
Comment thread modsecurity.conf-recommended Resolved
airween and others added 2 commits October 12, 2026 18:40
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@airween
airween requested a balanced review from Copilot October 12, 2026 16:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

An unmatched preamble line can be misclassified as value 2, allowing permissive rules to miss it.

0 open findings

Previously missed (1)

In code that hasn't changed since last review

Medium severity Preserve preamble boundary state until an in-part boundary appears

apache2/​msc_multipart.c:1270

A boundary-like line before the first delimiter sets flag_unmatched_boundary to 1, but the flag does not record where that line occurred. The first valid delimiter leaves it at 1; the next delimiter, including a closing one, satisfies boundary_count > 0 and changes it to 2. The same promotion happens in multipart_complete() for a closing delimiter without a trailing newline. As a result, an otherwise valid multipart body with an unmatched preamble line escapes permissive @eq 1 rules, contrary to the documented preamble behavior. Preserve whether the unmatched line occurred before the first delimiter, and promote to 2 only for a line seen inside a part; cover both closing-delimiter paths in regression tests.

🧠 Review effort: Lite

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The final-boundary logic can incorrectly promote a preamble unmatched-boundary flag from 1 to 2.

1 open finding

🧠 Review effort: Lite

Comment thread apache2/msc_multipart.c Resolved

@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, the port is faithful to #3654 and the direction is right. A few things I think should change before merge, most important first. Two of them (the dead guard and the embedded-boundary promotion) apply to #3654 as well, so it is probably best to settle them there first and re-diff this PR afterwards.

  1. The boundary_count > 0 guard is effectively dead, and the conf comment / test header document behavior the code does not have (inline).
  2. A truncated file part now leaks the temp fd and file on every request that takes the new no-500 path (inline).
  3. The embedded-boundary heuristic (foo--0000bar mid-line, msc_multipart.c ~L1332) is also promoted 1 -> 2 by the next valid delimiter. Pre-PR v2 always returned 1 there. With the documented permissive @eq 1 mode, a boundary-smuggling body now passes. If that is intended, the conf table should list that row explicitly; otherwise that path should not be promotable.
  4. Missing final boundary / no boundaries no longer feed MULTIPART_STRICT_ERROR (inline).
  5. Test coverage for the two scenarios where the guard would matter (inline).
  6. #3654 is still open but is referenced as finalized (inline).
  7. Minor: the multipart_get_arguments() branch still returns 500 (inline), and one commit bundles the value-semantics change with the no-500 change, which makes bisect and 2.9.x backports harder.

Comment thread apache2/msc_multipart.c Resolved
Comment thread apache2/msc_multipart.c Resolved
Comment thread apache2/msc_reqbody.c
}
return -1;
msr_log(msr, 2, "%s", *error_msg);
/* Do not abort the transaction here: the error is reported

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.

With the 500 gone, this becomes the designed path for every upload truncated under SecRequestBodyLimitAction ProcessPartial (and for any body missing its final boundary). When the cut lands inside a file part, multipart_complete() returns -1 without calling multipart_process_boundary(), which is the only place tmp_file_fd gets closed. multipart_cleanup() only walks msr->mpd->parts, never the in-progress msr->mpd->mpp, so the fd stays open and the file in SecTmpDir is orphaned.

Pre-existing, but previously hidden behind the 500. Now a client can exhaust fds / tmp space by repeatedly sending bodies that exceed the limit mid-file. Closing and unlinking mpp in multipart_cleanup() (or on this error path) would fix it.

Comment thread apache2/msc_reqbody.c
}

if (multipart_get_arguments(msr, "BODY", msr->arguments) < 0) {
else if (multipart_get_arguments(msr, "BODY", msr->arguments) < 0) {

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.

Minor: this is now the only multipart failure that still answers 500 instead of letting phase 2 decide, which contradicts the comment above and the "v2 matches v3" goal. REQBODY_ERROR is set but never observable. Dropping the return -1 in this branch makes it uniform.

# (not the first one) followed it, so it was most likely part data.
# * 1: an unmatched boundary-like line was seen, and no valid delimiter
# followed it (broken body, REQBODY_ERROR is also set), or the line
# was before the first delimiter (MULTIPART_STRICT_ERROR is also set).

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.

See the comment on msc_multipart.c L1269: with a complete body the value ends up 2 here, not 1. Either the code or this sentence needs to change.

# | broken body without unmatched "--" line | 0 | pass | pass |
# +-------------------------------------------+-------+--------+-------+
#
# See #1747, #1924, #3654 and #3658 for further information on the possible

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.

#3654 is still open against v3/master, and the current v3 code still promotes unconditionally. The PR description presents it as finalized. Suggest merging #3654 first and then re-diffing this PR against the merged v3 logic, especially if the guard semantics change there. Otherwise we end up with v2/v3 drift again, which is why #2193 was closed.

@sonarqubecloud

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Security-sensitive parser and request-body behavior changes warrant final human review.

0 open findings

1 resolved since last review

🧠 Review effort: Lite

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

Re-reviewed at 326f3c1. Status of the points from my previous review:

# Point Status
1 dead boundary_count > 0 guard fixed in 5687040 (flag_unmatched_boundary_preamble)
2 fd / tmp file leak for a part truncated mid-file open (inline)
3 embedded foo--0000bar heuristic still promotable to 2 open, needs a decision (inline)
4 Final boundary missing / No boundaries found do not set flag_error open (inline)
5 tests for the preamble case 500409/500410 added; the "unmatched line in last part, then truncation" case is still missing
6 v2/v3 drift with #3654 #3654 got the identical preamble commit (59e90cd), so the logic matches; #3654 is still open
7 multipart_get_arguments() branch still answers 500 open (inline)

The preamble logic itself is correct for the documented cases. Point 2 is the one I would not merge without, since the no-500 path makes it reachable on purpose now.

Comment thread apache2/msc_reqbody.c
}
return -1;
msr_log(msr, 2, "%s", *error_msg);
/* Do not abort the transaction here: the error is reported

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.

Still open: this is now the designed path for a body truncated by SecRequestBodyLimitAction ProcessPartial inside a file part. multipart_complete() returns -1 without reaching multipart_process_boundary(), which is the only place tmp_file_fd is closed, and multipart_cleanup() only walks msr->mpd->parts, never the in-progress msr->mpd->mpp. The fd stays open and the file in SecTmpDir is orphaned for every such request. Closing and unlinking mpp in multipart_cleanup() (same treatment as the loop over parts) would cover it.

Comment thread apache2/msc_reqbody.c
}

if (multipart_get_arguments(msr, "BODY", msr->arguments) < 0) {
else if (multipart_get_arguments(msr, "BODY", msr->arguments) < 0) {

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.

Still open: the return -1 in this branch is the last multipart failure that answers 500, which contradicts the comment above and leaves REQBODY_ERROR unobservable for it. Dropping the return makes the MULTIPART processor uniform.

Comment thread apache2/msc_multipart.c
msr->mpd->flag_unmatched_boundary = 2;
}

/* The payload is complete after all. */

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.

Still open: the two returns just below (Final boundary missing, line 1134, and No boundaries found in payload, line 1138) are the only error returns in multipart_complete() that do not set msr->mpd->flag_error = 1. Now that the request is no longer 500'd, MULTIPART_STRICT_ERROR stays 0 for those bodies, so a configuration relying on rule 200003 rather than 200002 forwards them. Setting flag_error = 1 on both returns keeps the variable consistent with the other multipart errors.

Comment thread apache2/msc_multipart.c
if (strncmp(p + i + 2, msr->mpd->boundary, strlen(msr->mpd->boundary)) == 0) {
msr->mpd->flag_unmatched_boundary = 1;
if (msr->mpd->boundary_count == 0) {
/* Preamble, not part data: the flag

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.

Still open (decision rather than code): this branch covers a boundary embedded mid-line (foo--0000bar). The preamble flag now protects it before the first delimiter, but inside a part it is still promoted 1 -> 2 by the next valid delimiter, where pre-PR v2 always reported 1. With the documented permissive @eq 1 mode such a body passes. If that is intended, the table in modsecurity.conf-recommended should list that row explicitly; otherwise this site should not set a promotable 1. Whichever way, #3654 should match.

# | Body | Value | !@eq 0 | @eq 1 |
# +-------------------------------------------+-------+--------+-------+
# | valid body | 0 | pass | pass |
# | part data line starting with "--" | 2 | block | pass |

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.

Small follow-up on the new preamble logic: once a preamble line has been seen, the promotion is suppressed for the whole body, so a genuine PEM-style -- line inside a later part also ends at 1. That is the strict outcome and fine, but this row is then not true for that body. A short note ("unless an unmatched line was also seen before the first delimiter") would keep the table exact.

Comment thread apache2/msc_reqbody.c
}

if (multipart_get_arguments(msr, "BODY", msr->arguments) < 0) {
else if (multipart_get_arguments(msr, "BODY", msr->arguments) < 0) {

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.

One more, and I think this one is a real divergence from v3 rather than a style point. Making this an else if means multipart_get_arguments() is skipped whenever multipart_complete() fails, so phase 2 sees no ARGS / ARGS_POST / ARGS_NAMES at all for an incomplete body. The PR text says that matches v3, but v3 sets ARGS and ARGS_POST per part as each part completes (Multipart::process_boundary, src/request_body_processor/multipart.cc around line 1314), so on a truncated body v3 still exposes every field that was fully received. msr->mpd->parts only ever holds completed parts, so calling multipart_get_arguments() unconditionally here is safe and gives the same result as v3. The interesting case is ProcessPartial: with the else if, a rule on ARGS cannot inspect the five fields that did arrive before the cut.

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.

4 participants