Skip to content

fix(callbacks): wrap plug and phoenix callbacks - #1223

Merged
solnic merged 7 commits into
fix/wrap-traces-sampler-callbackfrom
fix/wrap-plug-and-phoenix-callbacks
Sep 23, 2026
Merged

solnic merged 7 commits into
fix/wrap-traces-sampler-callbackfrom
fix/wrap-plug-and-phoenix-callbacks

Conversation

@solnic

@solnic solnic commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Now the callbacks reachable from a web request - remote_address_reader, the PlugContext scrubbers (body_scrubber, header_scrubber, cookie_scrubber, url_scrubber) and the PlugCapture / LiveViewHook scrubber - run through Sentry.Callback. A crash is logged as an error with :sentry log domain and the SDK falls back to its own default scrubbing, so the request is served as if nothing happened.

Two specific fixes on top of the wrapping: while capturing, a failure inside Sentry never replaces the application's own exception — the original one keeps propagating unchanged, and the LiveView hook scrubber falls back to empty data instead of raising, since raising in a hook takes down the LiveView process.

A scrubber that returns the wrong type now falls back to the default scrubber and is logged as a warning. Previously PlugCapture and LiveViewHook raised on a bad return value, so it was reported as a crash with a stacktrace, while the equivalent PlugContext case was a warning — they all report it the same way now.

@solnic
solnic added this pull request to stack #1225 September 22, 2026 07:48
@solnic solnic changed the title Fix/wrap plug and phoenix callbacks fix(callbacks): wrap plug and phoenix callbacks Sep 22, 2026
@solnic
solnic marked this pull request as ready for review September 22, 2026 09:11
@solnic
solnic force-pushed the fix/wrap-plug-and-phoenix-callbacks branch from 32d5a7d to 80ace99 Compare September 22, 2026 10:06

@moduledoc since: "10.5.0"

import Phoenix.LiveView, only: [attach_hook: 4, get_connect_info: 2]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: An invalid :remote_address_reader option for Sentry.PlugContext causes an uncaught ArgumentError, crashing the request despite documentation promising that callback failures are handled.
Severity: MEDIUM

Suggested Fix

Ensure that exceptions raised during callback preparation are also caught. This could be achieved by moving the Sentry.Callback.to_fun/3 call inside the Sentry.Callback.run/2 function or by wrapping the call in plug_context.ex with a try/rescue block to handle potential ArgumentError exceptions gracefully, thus upholding the documented guarantee.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: lib/sentry/live_view_hook.ex#L89

Potential issue: If an invalid value is provided for the `:remote_address_reader` option
in `Sentry.PlugContext` (e.g., a string or atom instead of a function or tuple), the
call to `Sentry.Callback.to_fun/3` will raise an `ArgumentError`. This exception is not
caught because it occurs before `Sentry.Callback.run/2` is invoked, which is intended to
handle callback failures. This behavior contradicts the documentation's promise that
callback failures will not affect the request, leading to an uncaught exception that
crashes the request process upon misconfiguration.

Also affects:

  • lib/sentry/live_view_hook.ex:150~150

Did we get this right? 👍 / 👎 to inform future reviews.

@solnic
solnic force-pushed the fix/wrap-plug-and-phoenix-callbacks branch from 80ace99 to 8e0f744 Compare September 23, 2026 14:43
@solnic
solnic merged commit 3689a8b into master Sep 23, 2026
16 checks passed
@solnic
solnic deleted the fix/wrap-plug-and-phoenix-callbacks branch September 23, 2026 15:44
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