Skip to content

fix(live_view_hook): handle Bandit.TransportError and URI struct in connect_info#1062

Merged
solnic merged 2 commits into
getsentry:masterfrom
dl-alexandre:fix/live-view-hook-bandit-uri
May 20, 2026
Merged

fix(live_view_hook): handle Bandit.TransportError and URI struct in connect_info#1062
solnic merged 2 commits into
getsentry:masterfrom
dl-alexandre:fix/live-view-hook-bandit-uri

Conversation

@dl-alexandre
Copy link
Copy Markdown
Contributor

Summary

Fixes two related issues in Sentry.LiveViewHook that affect Bandit + LiveView production deployments:

  1. LiveViewHook.on_mount crashes with Bandit.TransportError when client disconnects during mount #1025on_mount crashes with Bandit.TransportError when the client disconnects during mount.
  2. LiveView Hook silently swallows errors #1040 — LiveView hooks silently swallow errors because %URI{} structs in connect_info fail JSON encoding.

Changes

lib/sentry/live_view_hook.ex

  • Bandit disconnect safety: Wrap get_connect_info(socket, key) in get_connect_info_if_root/2 with a try/rescue that returns nil on any transport error. This mirrors the existing defensive pattern in Sentry.PlugContext (see remote_port/1).
  • URI struct normalization: Convert socket.host_uri to a string with URI.to_string/1 before storing it in Sentry request context. Previously, when :uri was omitted from connect_info, the raw %URI{} struct remained in context and caused JSON encoding to fail later in the pipeline.

lib/sentry/client.ex

  • Defense-in-depth: Run sanitize_non_jsonable_values/2 on the :request field during event rendering, consistent with how :extra, :user, and :tags are already sanitized.

Tests

  • Added SentryTest.EndpointNoUri and Sentry.LiveViewHookNoUriTest to verify hook behavior when :uri is absent from connect_info.
  • Added regression test for Client.render_event/1 sanitizing non-JSON-encodable values in :request.

Verification

  • mix test — 813 tests, 0 failures
  • mix format --check-formatted — clean
  • mix compile --warnings-as-errors — clean

Related Issues

…onnect_info

Wrap `get_connect_info/2` calls in `try/rescue` to gracefully handle
`Bandit.TransportError` when the WebSocket transport is already closed
during mount (fixes getsentry#1025).

Convert `socket.host_uri` to a string with `URI.to_string/1` before storing
it in request context, preventing JSON encoding failures when `:uri` is
not included in `connect_info` (fixes getsentry#1040).

Also sanitize non-JSON-encodable values in `:request` during event rendering
as defense-in-depth.

Co-Authored-By: OpenCode <noreply@opencode.ai>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2e9fe8c. Configure here.

Comment thread lib/sentry/live_view_hook.ex
@dl-alexandre dl-alexandre force-pushed the fix/live-view-hook-bandit-uri branch from 77ed5a2 to 142dbdc Compare May 17, 2026 21:14
…tionale

Expand the moduledoc to explain what each connect_info key provides
and that the hook still works when keys are omitted.

Add a comment to the try/rescue in get_connect_info_if_root/2 explaining
why we rescue broadly (Bandit is optional and we must never crash the
LiveView process).

Co-Authored-By: OpenCode <noreply@opencode.ai>
@dl-alexandre dl-alexandre force-pushed the fix/live-view-hook-bandit-uri branch from 142dbdc to 06e0918 Compare May 17, 2026 21:30
Copy link
Copy Markdown
Collaborator

@solnic solnic left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this 🙇🏻

@solnic solnic merged commit b2c30d7 into getsentry:master May 20, 2026
10 of 11 checks passed
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.

LiveView Hook silently swallows errors LiveViewHook.on_mount crashes with Bandit.TransportError when client disconnects during mount

2 participants