forked from owasp-modsecurity/ModSecurity
-
Notifications
You must be signed in to change notification settings - Fork 25
Malformed JSON body results in 400 Bad Request instead of 500 #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alxyzc
wants to merge
1
commit into
waf_nginx
Choose a base branch
from
alxyzc/fix/unparsable-bodies-return-400
base: waf_nginx
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 - I thought of the same -- if we agree that this is an elegant implementation of the fix, I will update the change to all scenarios where the body parsing fails.
2 - Quoting RFC 7231 for reference:
My understanding is that 500 usually happens when the server throws an unhandled exception, which could be caused by a variety of reasons, including a missing implementation of a specific scenario. In this case, we can see the
return -1
as throwing an exception which was not handled gracefully higher in the call hierarchy, thus results in a 500 Internal Server Error, which is the main complaint of the IcM ticket.However, this code change effectively patching that missing code path, so that the body parsing error is now handled gracefully by resulting in a 400, which according to the RFC is a valid response code when we "cannot or will not process" the "malformed request syntax" due to "a client error".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is kind of tricky, in this case, if return 400, we treat this as client error, but this incomplete json is only unexpected condition for modsec to fulfilling the request.
Also we have been return 500 for parsing error for quite long time for customers, if we change to 400, not sure what's the impact in the client side. Could we pull in Avinash for more inputs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to add to Yanshu comments - if I recall , part of the problem was that there was no logging on the modsec
I don't see that being addressed, although I see that msr_log is called ,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also we might need to understand what's json_complete doing? And what error condition to trigger it. which help us to get more understanding for whether it's client error or just something specific for modsec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but the fix appears to be low risk, i m ok with it
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@azhao155 in this particular case in the IcM it is the client sending non-JSON text when the Content-Type is
application/json
. I think your point is that whether we can rely onjson_complete
to always successfully parse a correctly formatted JSON, so that when it doesn't it always implies that the JSON is malformed? Injson_complete
it delegates to the YAJL library for parsing the JSON, which like any library makes promises but a question mark could always be placed on whether we can trust those promises.@alonzop There is no firewall log entries for malformed requests because these requests will trigger no rules. There are entries, on the other hand, in the
nginx/error.log
, as it is treated by ModSecurity as an exception. However the error logs complains about not being able to "print json logs" instead of not being able to parse the JSON body. I was stepping through the ModSecurity code, and the logging gets really strange when the request does not parse -- a warning string is passed into the logger where it's expecting a structured message with very specifically defined fields (source code) 🤔 I'm okay with not changing the behavior if it has a potential of breaking customers, but the logging will probably require a deeper dive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alxyzc not sure about breaking as i am not aware of use cases, but we should get input from pm/ @asridharan
as for the logging, i understand , and ok with that, no need to add noise to the waf logs, maybe we should fix the msg in the error.log to reflect the correct problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alxyzc @asridharan Since now we start to change behavior return 400 instead of 500 for json parsing error, should we do the same thing for other format as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alxyzc I would prefer not changing the response status code explicitly for JSON errors. Can we have a discussion on this before proceeding on this?