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
{{ message }}
Repository navigation
Bug: Premature EOF in JSON leads to status 500 instead of REQBODY_ERROR #2807
At first glance I would probably agree that it makes more sense for this use case to signal REQBODY_ERROR instead of triggering a status code of 500. Indeed, that is the behaviour in ModSecurity v3.
A slight clarification: with SecRuleEngine On, the outcome is as you describe. But with DetectionOnly REQBODY_ERROR does get set (with appropriate rule 200002 output in the audit log).
Note, however, that this status 500 outcome that you have observed is not unique to failures with JSON parsing. For example, a comparable case with XML produces the same result. Indeed the software uses 500 as a general response to request body parsing failures that are not otherwise categorized. If we were to consider making a change to the JSON-parsing-failure result, we would probably need to map out all other cases where comparable considerations apply, so as to avoid exacerbating inconsistencies within ModSecurity v2.
Also: Just curious if you have some specific danger in mind when you say "Instead of failing safely"? Although the current v2 behaviour is likely not ideal, I don't off hand see anything that I would describe as a safety issue.
Looking over the various payloads, all with different error messages, lets the last one stand out. So I'd argue it's at least inconsistent and if ModSec3 is doing this better, then that's great.
Failing safely: I think it's always better to make the reaction configurable. With the status 500 you can no longer influence the behaviour while the REQBODY_ERROR allows you do come up with a more nuanced behaviour. Redirect to special error page depending on the context for example. I agree that 500 is not really unsafe here, but it's misleading since the server did identify the client error it just chose to abort instead of triggering the appropriate status 400 with the help of a ModSec rule.
And as you can see above, nothing in the error message gives away the fact, that EOF would be different from the rest of the messages.
My guess is that, when this was originally programmed, the distinction was that the first 5 cases are all consistent with at least some successful parsing.
The 6th example, however, has nothing that can be successfully parsed (because even outermost JSON requirement has not been satisfied.
By way of comparison, most multipart parsing errors are signalled through individual variables. However if a supposedly multipart request body contains only the single character "a" (i.e. with no boundaries specified at all), then ModSecurity v2 will return 500.
While that distinction between the use cases makes some sense to me, that doesn't mean that it's the ideal choice. Indeed, partly for the reasons you stated, I do lean towards the conclusion that the ModSecurity v3 behaviour (signalling through variables rather than a response of 500, for the cases under discussion) is preferable.
I think I've run into this same problem before, but in a different context. I think this may be related to a bug in request body handling that was introduced in ModSecurity 2.9.3. (Apologies if this is not related, but it seems like it may be the same root issue.)
I've found that older versions of ModSecurity respond differently to multipart parsing errors. This also seems to have broken the option ProcessPartial, I believe due to a change in the read_request_body function.
If I deliberately send a request with malformed multipart boundaries, ModSecurity 2.9.1 responds with 400 Bad Request.
ModSecurity 2.9.5 instead responds with 500 Internal Server Error. I saw a lot of those internal server errors in testing after updating from 2.9.1 to 2.9.5. I never had this problem on 2.9.1, it always worked as expected.
That could be a problem in a scenario where you've changed this rule:
The effect of that change seems to be that if reading the request body fails, e.g. a boundary parsing error, then the request body is not processed at all. Phase 2 is not executed and it skips straight to phase 3.
So, ProcessPartial no longer allows partial request body content to be inspected from ModSecurity 2.9.3 onwards.
I've successfully tested restoring/reverting the functionality of ProcessPartial by reverting return rcbe; back to return 1; in 2.9.5. But I don't know what other effects that would have, so I haven't considered opening a PR for that 🙂
Returning 1 instead of rcbe allows more flexibility, as you can decide yourself what to do (ant to log), but put more responsibility on the configurator.
I personally prefer this, but I have to admit that returning rcbe is a "default-deny" approach.
A side-remark:
In modsecurity_request_body_end(), we return -1 (which leads to a status 500) when there's a parsing problem. Returning -2 would lead to a status 400, which is better for me as it's not an internal error.
Same in modsecurity_request_body_end_urlencoded()
Thank you for the insight, Marc. I agree: it is preferable to have choice and control over whether to send a 400 response, a 500, or something else.
@dune73 I tested your six payloads. Specifically looking at the interesting one { "id" : "123"
I confirmed that this behaviour has changed in recent versions. I observed the following:
Revisiting this one. The open PR #3629 ("v2: Return HTTP 400 for JSON completion errors without relaxing rejection") addresses exactly this: an incomplete JSON body currently ends in a 500 from the completion path instead of setting REQBODY_ERROR and letting rule 200002 return 400. Reviewing and merging that PR resolves this issue.
Activity
martinhsv commentedon Oct 5, 2022
Hi @dune73 ,
At first glance I would probably agree that it makes more sense for this use case to signal REQBODY_ERROR instead of triggering a status code of 500. Indeed, that is the behaviour in ModSecurity v3.
A slight clarification: with
SecRuleEngine On, the outcome is as you describe. But withDetectionOnlyREQBODY_ERROR does get set (with appropriate rule 200002 output in the audit log).Note, however, that this status 500 outcome that you have observed is not unique to failures with JSON parsing. For example, a comparable case with XML produces the same result. Indeed the software uses 500 as a general response to request body parsing failures that are not otherwise categorized. If we were to consider making a change to the JSON-parsing-failure result, we would probably need to map out all other cases where comparable considerations apply, so as to avoid exacerbating inconsistencies within ModSecurity v2.
Also: Just curious if you have some specific danger in mind when you say "Instead of failing safely"? Although the current v2 behaviour is likely not ideal, I don't off hand see anything that I would describe as a safety issue.
dune73 commentedon Oct 5, 2022
Thank you for your swift response Martin. I thought I had seen status 500 with XML before but it was new with JSON for me.
Then playing around I made the following discoveries:
Looking over the various payloads, all with different error messages, lets the last one stand out. So I'd argue it's at least inconsistent and if ModSec3 is doing this better, then that's great.
Failing safely: I think it's always better to make the reaction configurable. With the status 500 you can no longer influence the behaviour while the REQBODY_ERROR allows you do come up with a more nuanced behaviour. Redirect to special error page depending on the context for example. I agree that 500 is not really unsafe here, but it's misleading since the server did identify the client error it just chose to abort instead of triggering the appropriate status 400 with the help of a ModSec rule.
And as you can see above, nothing in the error message gives away the fact, that EOF would be different from the rest of the messages.
martinhsv commentedon Oct 5, 2022
My guess is that, when this was originally programmed, the distinction was that the first 5 cases are all consistent with at least some successful parsing.
The 6th example, however, has nothing that can be successfully parsed (because even outermost JSON requirement has not been satisfied.
By way of comparison, most multipart parsing errors are signalled through individual variables. However if a supposedly multipart request body contains only the single character "a" (i.e. with no boundaries specified at all), then ModSecurity v2 will return 500.
While that distinction between the use cases makes some sense to me, that doesn't mean that it's the ideal choice. Indeed, partly for the reasons you stated, I do lean towards the conclusion that the ModSecurity v3 behaviour (signalling through variables rather than a response of 500, for the cases under discussion) is preferable.
dune73 commentedon Oct 5, 2022
+1 on the interpretation why this might be the case. That makes sense.
As for the behaviour that is preferable, I agree. Providing a PR is probably outside my capability here. But I would still welcome a fix.
RedXanadu commentedon Oct 5, 2022
Hello,
I think I've run into this same problem before, but in a different context. I think this may be related to a bug in request body handling that was introduced in ModSecurity 2.9.3. (Apologies if this is not related, but it seems like it may be the same root issue.)
I've found that older versions of ModSecurity respond differently to multipart parsing errors. This also seems to have broken the option
ProcessPartial, I believe due to a change in theread_request_bodyfunction.If I deliberately send a request with malformed multipart boundaries, ModSecurity 2.9.1 responds with 400 Bad Request.
ModSecurity 2.9.5 instead responds with 500 Internal Server Error. I saw a lot of those internal server errors in testing after updating from 2.9.1 to 2.9.5. I never had this problem on 2.9.1, it always worked as expected.
That could be a problem in a scenario where you've changed this rule:
to have a specific status code, i.e. anything you set (400, 403, etc.) would be ignored and a 500 would be served.
ProcessPartialworks in ModSecurity 2.9.2 because theread_request_bodyfunction would always return 1, unconditionally.In 2017, the return code was changed so that it would indicate the outcome at the end of reading the request body:
https://github.com/SpiderLabs/ModSecurity/pull/1613/files#diff-db36ab5d2f2d3ea8001e77a6f92035890a9729ef947c5dd7ba3ba6dbfff57030R347
The effect of that change seems to be that if reading the request body fails, e.g. a boundary parsing error, then the request body is not processed at all. Phase 2 is not executed and it skips straight to phase 3.
So,
ProcessPartialno longer allows partial request body content to be inspected from ModSecurity 2.9.3 onwards.I've successfully tested restoring/reverting the functionality of
ProcessPartialby revertingreturn rcbe;back toreturn 1;in 2.9.5. But I don't know what other effects that would have, so I haven't considered opening a PR for that 🙂dune73 commentedon Oct 5, 2022
Wow, this is interesting. Maybe worth a separate feature!
Did you or can you try out my curl call from above on 2.9.2?
marcstern commentedon Oct 7, 2022
RedXanadu commentedon Oct 10, 2022
Thank you for the insight, Marc. I agree: it is preferable to have choice and control over whether to send a 400 response, a 500, or something else.
@dune73 I tested your six payloads. Specifically looking at the interesting one
{ "id" : "123"I confirmed that this behaviour has changed in recent versions. I observed the following:
ModSecurity 2.9.2
ModSecurity 2.9.6 with
read_request_bodydefined toreturn 1;ModSecurity 2.9.6 with
read_request_bodydefined toreturn rcbe;(the default)dune73 commentedon Oct 10, 2022
Thank you very much for checking this for me, @RedXanadu.
dune73 commentedon May 1, 2024
I have just met this bug again and tried to fight it with
SecRequestBodyLimitAction RejectBut no effect.
Debug Log:
Return HTTP 400 for JSON completion errors without relaxing rejection
fzipi commentedon Oct 12, 2026
Revisiting this one. The open PR #3629 ("v2: Return HTTP 400 for JSON completion errors without relaxing rejection") addresses exactly this: an incomplete JSON body currently ends in a 500 from the completion path instead of setting
REQBODY_ERRORand letting rule 200002 return 400. Reviewing and merging that PR resolves this issue.