Skip to content

fix(handler): keep request data out of the crash response and log - #144

Merged
zmstone merged 3 commits into
masterfrom
261007-scrub-crash-details-master
Oct 7, 2026
Merged

zmstone merged 3 commits into
masterfrom
261007-scrub-crash-details-master

Conversation

@zmstone

@zmstone zmstone commented Oct 7, 2026

Copy link
Copy Markdown
Member

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

Summary

When a handler callback crashes, apply_callback/4 formats 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 /3 callbacks, the cowboy request map. Both carry the request headers. As a result, the authorization header (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/3 runs before apply_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:

  • Atoms, numbers, pids, ports, references and funs stay as they are.
  • Map keys stay as they are when they are atoms or binaries of up to 64 bytes. A header name such as authorization stays visible; its value does not.
  • Other binaries and strings become their size, for example <<"...(38 bytes)">>. No prefix of the value is kept.
  • Terms nested deeper than 3 levels become '...'. Lists, tuples and maps keep at most 10 elements.
  • Each stack frame keeps only file and line from its location. Other items, for example error_info, are dropped.

For example, {badmatch, <<"...">>} is logged as {badmatch, <<"...(32 bytes)">>}, and a function_clause frame 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: a function_clause crash in a /3 handler, with an authorization header. 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: a badmatch on 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 plain error(boom) keeps its format.

Cherry-pick notes

The change to src/minirest_handler.erl is identical to #143. The two test files had conflicts only where master (the cookie tests from #139) and #143 both appended to all/0, the export list, api_spec/0, the route definitions, the handlers and the helpers. All of them are kept.

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
zmstone marked this pull request as ready for review October 7, 2026 09:58
@zmstone
zmstone merged commit e741cdb into master Oct 7, 2026
2 checks passed
@zmstone
zmstone deleted the 261007-scrub-crash-details-master branch October 7, 2026 10:01
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.

2 participants