Repository navigation
fix(handler): handle exceptions in authorize, parse and filter steps - #146
Merged
Merged
Conversation
Only the handler callback ran inside a try/catch. An exception in the authorize callback, in request parsing or in the filter ended the cowboy request process, and proc_lib wrote a crash report with the full stacktrace at error level. A function_clause frame holds the actual arguments, so the report could hold request headers. The authorize step runs before authentication. For example, when an authorize callback parses a malformed authorization header, cowlib fails with function_clause, cowboy re-raises it as an exit with request_error, and the crash report holds the decoded header content. All four steps now run in guard/2. A request_error exit becomes the 4xx response cowboy would send (400, 408 or 413) with a JSON body and no log entry. Any other exception is logged with the scrubbed reason and stacktrace and becomes a 500 INTERNAL_ERROR response, as for the handler callback.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Cherry-pick of #145 (
release-1.4) tomaster.Summary
Follow-up to #143. #143 scrubs the reason and the stacktrace of an exception in the handler callback. The steps before the handler callback had no try/catch: the authorize callback, request parsing and the filter.
An exception in one of these steps ended the cowboy request process. proc_lib then wrote a crash report at error level with the full stacktrace. A
function_clauseframe holds the actual arguments, so the report could hold request headers.The authorize step runs before authentication. When an authorize callback calls
cowboy_req:parse_header(<<"authorization">>, Req)on a malformed header, cowlib fails withfunction_clause. cowboy re-raises the failure asexit:{request_error, ...}with the original stacktrace. The crash report then holds the header content, for example the decoded API key and secret of aBasicheader that has no:. The client gets a 400 with an empty body.Change
guard/2runs each step (authorize, parse, filter and handler callback) inside try/catch.exit:{request_error, Reason, Text}becomes the response cowboy would send: 408REQUEST_TIMEOUT, 413PAYLOAD_TOO_LARGEor 400BAD_REQUEST, now with a JSON body that holds cowboy's text. Nothing is logged.INTERNAL_ERRORresponse.maybe_ignore_code_check/2accepts 408REQUEST_TIMEOUTand 413PAYLOAD_TOO_LARGE, as it already accepts 400BAD_REQUEST.Behavior changes:
request_errorraised inside the handler callback now gives the 4xx above. Before, it gave a 500.Tests
New cases in
minirest_handler_SUITE. Each case checks every log event received within one second, including the proc_lib crash report. All three fail on the base branch.t_crash_bad_header_in_authorize: an authorize callback parses a malformedBasicheader. The response is 400BAD_REQUEST, and no log event holds the header content.t_crash_in_authorize: an authorize callback fails withfunction_clause. The response is 500, the log holds the scrubbed stacktrace, and no log event holds the authorization header value.t_crash_in_filter: a filter fails withfunction_clause. The response is 500, and no log event holds the authorization header value.Cherry-pick notes
The change to
src/minirest_handler.erlandtest/minirest_handler_SUITE.erlapplies without conflicts.test/minirest_test_handler.erlhad conflicts wheremaster(set_cookie,set_cookies,echo_json) and this change both added entries toapi_spec/0and route definitions. All of them are kept.