Skip to content

Return 400 in case of parsing error#3515

Open
JonathanBerrew wants to merge 3 commits intoowasp-modsecurity:v2/masterfrom
JonathanBerrew:400-if-parsing-error
Open

Return 400 in case of parsing error#3515
JonathanBerrew wants to merge 3 commits intoowasp-modsecurity:v2/masterfrom
JonathanBerrew:400-if-parsing-error

Conversation

@JonathanBerrew
Copy link
Copy Markdown

This is a Marc Stern modification, I don't have much more insight on the code he made. To be reviewed with caution and check if this is still relevant

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
E Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@airween airween requested a review from Copilot April 22, 2026 15:35
@airween airween added the 2.x Related to ModSecurity version 2.x label Apr 22, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to change ModSecurity’s Apache2 request-body handling so that a request-body parsing failure results in an HTTP 400 response instead of being treated as an internal server error.

Changes:

  • Added a special-case mapping in read_request_body() to return HTTP_BAD_REQUEST when modsecurity_request_body_end() returns -2.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apache2/apache2_io.c
Comment on lines +357 to 360
if (rcbe == -2) {
return HTTP_BAD_REQUEST;
}
if (rcbe < 0) {
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

modsecurity_request_body_end() (in apache2/msc_reqbody.c) never returns -2—it returns 1 on success, -5 for reqbody no-files limit, and -1 for parser/internal errors. As a result this new rcbe == -2 branch is effectively dead code and does not achieve the PR goal of returning 400 on parsing errors (those currently surface as -1 and will still map to 500 via rcbe < 0). Consider either (a) returning a distinct status code from modsecurity_request_body_end() for parse errors and mapping that to HTTP_BAD_REQUEST here, or (b) mapping the existing parse-error signal (-1 plus the parser error state/message) to 400 while keeping true internal failures as 500.

Suggested change
if (rcbe == -2) {
return HTTP_BAD_REQUEST;
}
if (rcbe < 0) {
if (rcbe < 0) {
if ((error_msg != NULL) && (*error_msg != NULL) && (**error_msg != '\0')) {
return HTTP_BAD_REQUEST;
}

Copilot uses AI. Check for mistakes.
@JonathanBerrew
Copy link
Copy Markdown
Author

#2807
When Marc made this modification, he mentioned this issue

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
D Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

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