You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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()).
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).
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.
Fixed: the value can only become 2 if it was 1 before.
Fixed: the transition also happens on the final boundary, including one without a trailing CRLF. There is a dedicated test for this.
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.
…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
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.
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
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.
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.
The boundary_count > 0 guard is effectively dead, and the conf comment / test header document behavior the code does not have (inline).
A truncated file part now leaks the temp fd and file on every request that takes the new no-500 path (inline).
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.
Missing final boundary / no boundaries no longer feed MULTIPART_STRICT_ERROR (inline).
Test coverage for the two scenarios where the guard would matter (inline).
#3654 is still open but is referenced as finalized (inline).
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.
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.
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.
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.
#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.
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.
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.
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.
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.
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.
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
what
This PR ports the v3
MULTIPART_UNMATCHED_BOUNDARYfix (#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
Content-Typeheader--) was not followed by a valid boundary-----BEGIN ...)multipart_complete()).2. No HTTP 500 when
multipart_complete()failsUntil now, a multipart body that could not be completed made
read_request_body()returnHTTP_INTERNAL_SERVER_ERROR. Examples are a missing final boundary, no boundary at all, or a body truncated byProcessPartial. Phase 2 never ran, whatever rules were configured.Now the error is reported the same way as in v3:
REQBODY_ERROR= 1 andREQBODY_ERROR_MSGis set, and theMULTIPART_*flags are available.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-recommendednow documents the possible values, including a summary table. The default rule 200004 stays strict (!@eq 0).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).misc/00-multipart-parser.ttarget/10-variable-multipart_strict_error.tconfig/10-request-directives.twhy
In v2, every line starting with
--that doesn't match the boundary setsMULTIPART_UNMATCHED_BOUNDARYto 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:
--line;--content in the last part was not handled;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:
--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:
Behavior changes — please read
SecRuleEngine On, recommended configREQBODY_ERRORSecRequestBodyLimitAction ProcessPartialREQBODY_ERROR= 1, so 200002 → 400)--, followed by a valid boundaryWhat this means for users:
REQBODY_ERROR(rule 200002 inmodsecurity.conf-recommended). Without one, a malformed multipart body is no longer rejected by the engine itself.ARGS. OnlyREQBODY_ERRORand theMULTIPART_*flags tell you what happened.MULTIPART_UNMATCHED_BOUNDARYcan now be 2 in v2. Rules or log formats that expect only0/1(e.g.@streq 1, or parsing%{MULTIPART_UNMATCHED_BOUNDARY}) should be reviewed.!@eq 0, blocking does not change for this variable. If you change rule 200004 to@eq 1, PEM-like content is allowed.references