Skip to content

fix(handler): handle exceptions in authorize, parse and filter steps - #146

Merged
zmstone merged 1 commit into
masterfrom
261010-catch-pre-handler-crash-master
Oct 15, 2026
Merged

zmstone merged 1 commit into
masterfrom
261010-catch-pre-handler-crash-master

Conversation

@zmstone

@zmstone zmstone commented Oct 10, 2026

Copy link
Copy Markdown
Member

Cherry-pick of #145 (release-1.4) to master.

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_clause frame 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 with function_clause. cowboy re-raises the failure as exit:{request_error, ...} with the original stacktrace. The crash report then holds the header content, for example the decoded API key and secret of a Basic header that has no :. The client gets a 400 with an empty body.

Change

  • guard/2 runs each step (authorize, parse, filter and handler callback) inside try/catch.
  • exit:{request_error, Reason, Text} becomes the response cowboy would send: 408 REQUEST_TIMEOUT, 413 PAYLOAD_TOO_LARGE or 400 BAD_REQUEST, now with a JSON body that holds cowboy's text. Nothing is logged.
  • Any other exception is logged with the scrubbed reason and stacktrace from fix(handler): keep request data out of the crash response and log #143 and becomes a 500 INTERNAL_ERROR response.
  • maybe_ignore_code_check/2 accepts 408 REQUEST_TIMEOUT and 413 PAYLOAD_TOO_LARGE, as it already accepts 400 BAD_REQUEST.

Behavior changes:

  • An exception before the handler callback now gives a 500 with a JSON body. Before, cowboy sent a 500 with an empty body.
  • A request_error raised 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 malformed Basic header. The response is 400 BAD_REQUEST, and no log event holds the header content.
  • t_crash_in_authorize: an authorize callback fails with function_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 with function_clause. The response is 500, and no log event holds the authorization header value.

Cherry-pick notes

The change to src/minirest_handler.erl and test/minirest_handler_SUITE.erl applies without conflicts. test/minirest_test_handler.erl had conflicts where master (set_cookie, set_cookies, echo_json) and this change both added entries to api_spec/0 and route definitions. All of them are kept.

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.
@zmstone
zmstone marked this pull request as ready for review October 10, 2026 12:55
@zmstone
zmstone merged commit 07b01af into master Oct 15, 2026
2 checks passed
@zmstone
zmstone deleted the 261010-catch-pre-handler-crash-master branch October 15, 2026 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant