Repository navigation
fix(handler): keep request data out of the crash response and log - #144
Merged
Merged
Conversation
When a handler callback crashed, apply_callback/4 formatted the
exception reason and the full stacktrace into the warning log and into
the 500 response body. On function_clause the first stack frame holds
the actual handler arguments: the parsed params and, for /3 callbacks,
the cowboy request map. Both carry the request headers, so the
authorization header (Bearer token or Basic API key and secret) and
body fields were recorded in the node log and returned in the response.
A reason such as {badmatch, Value} can carry request data the same way.
The stacktrace now keeps module, function, arity, file and line of each
frame; argument lists are replaced by their length. The reason keeps
only its atoms; any other term, or tuple element, becomes '_'. For
example {badmatch, <<"...">>} becomes {badmatch, '_'}. The response
keeps its existing "Class, Reason, Stacktrace" message format.
Replacing each argument list with its arity, and each non-atom reason element with '_', removed what a reader needs to diagnose a function_clause or badmatch: which method, which map keys, list or map. Walk the arguments and the reason instead. Atoms, pids, ports, references, funs, atom map keys and binary map keys up to 64 bytes stay as they are. Other binaries and strings become their size, for example <<"...(38 bytes)">>, so no part of a secret value is kept, not even a prefix. Numbers become '<integer>' or '<float>', since a short number can be a PIN or a one-time code. Terms nested deeper than 3 levels become '...', and lists, tuples and maps keep at most 10 elements, because the cowboy request map and a decoded body can be large.
Numbers help to diagnose a crash, for example a status code, a port or a count in a badmatch. Secrets in request data arrive as binaries or strings, which are still replaced by their size.
zmstone
marked this pull request as ready for review
October 7, 2026 09:58
savonarola
approved these changes
Oct 7, 2026
1 of 3 tasks
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 #143 (
release-1.4) tomaster.Summary
When a handler callback crashes,
apply_callback/4formats the exception reason and the stacktrace into a warning log and into the 500 response body.On
function_clause, the first stack frame holds the actual handler arguments. These are the parsed params and, for/3callbacks, the cowboy request map. Both carry the request headers. As a result, theauthorizationheader (a Bearer token, or Basic with an API key and secret) and the body fields were written to the node log and returned in the response. A reason such as{badmatch, Value}can carry request data in the same way.do_authorize/3runs beforeapply_callback/4. Only an authenticated caller can reach this code path. The log is the larger concern: it keeps the credential after the request ends, and people other than the credential holder read it.Change
Both the log and the response now get a scrubbed copy of the error reason and of the stacktrace arguments. The scrubbed copy keeps the shape of the term and drops the data:
authorizationstays visible; its value does not.<<"...(38 bytes)">>. No prefix of the value is kept.'...'. Lists, tuples and maps keep at most 10 elements.fileandlinefrom its location. Other items, for exampleerror_info, are dropped.For example,
{badmatch, <<"...">>}is logged as{badmatch, <<"...(32 bytes)">>}, and afunction_clauseframe shows[get, #{body => ..., headers => ...}, #{...}]with every value scrubbed.The response keeps its existing
"Class, Reason, Stacktrace"message format. I did not replace the message with a generic text: that is a visible API change for a patch release, and with the scrubbing the message no longer holds request data.Tests
New cases in
minirest_handler_SUITE. A logger handler captures the warning log event.t_crash_hides_request: afunction_clausecrash in a/3handler, with anauthorizationheader. The header value does not appear in the response or the log, and the logged header value is its size. This case fails on the base branch.t_crash_hides_reason_data: abadmatchon a body value. The value does not appear in the response or the log, and the logged reason is{badmatch, <<"...(N bytes)">>}. This case fails on the base branch.t_crash_trace_keeps_location: the logged stacktrace still names module, function, file and line, and shows the shape of the arguments.t_crash_trace_limits: a body with 20 nested entries is cut to 10 map entries and to the depth limit.t_crash_response_format: the response message for a plainerror(boom)keeps its format.Cherry-pick notes
The change to
src/minirest_handler.erlis identical to #143. The two test files had conflicts only wheremaster(the cookie tests from #139) and #143 both appended toall/0, the export list,api_spec/0, the route definitions, the handlers and the helpers. All of them are kept.