skip to content

With turbo_stream_from, what does the signed stream name protect against, and why is it not a full authorization check?

level: seniorimportance: should knowfreq 30%

answer

  1. a MessageVerifier signature, not encryption
  2. Turbo.signed_stream_verifier and its key
  3. verified or reject in Turbo::StreamsChannel
  4. no expiry, no access re-check
  5. custom channel: verified_stream_name_from_params

basics

~20 s

The signature stops users from forging or editing a stream name to subscribe to someone else's updates; but it never expires and Turbo::StreamsChannel checks nothing else, so anyone who once received the page can keep listening.

solid answer

~40 s

`turbo_stream_from` signs the stream name with `Turbo.signed_stream_verifier`, an `ActiveSupport::MessageVerifier` keyed from the app's key generator, and `Turbo::StreamsChannel#subscribed` calls `stream_from` only if the signature verifies, rejecting the subscription otherwise. That defeats tampering: a cook cannot change kitchen 1's name into kitchen 2's. It is a capability, not a permission check: the name carries no expiry and no user, so a cook removed from the kitchen, or anyone who copied the attribute out of the HTML, can subscribe until the key changes. It is also signed, not encrypted. For sensitive streams, use a custom channel that reuses `Turbo::Streams::StreamName`, verifies the name and then checks the connection's `current_user` before `stream_from`, and point the view at it with `channel:`.

code

erb · 6 lines
erb
<%# app/views/kitchens/show.html.erb %>
<%= turbo_stream_from @kitchen, channel: KitchenStreamChannel %>

<div id="tickets">
  <%= render @kitchen.tickets %>
</div>

go deeper

for a junior

Recall that turbo_stream_from puts a signed stream name in the page and that Turbo::StreamsChannel rejects a name whose signature does not verify.

for a middle

Explain how the name is built and signed with Turbo.signed_stream_verifier, what tampering it stops, and that the page which rendered the name is the real gate.

for a senior

Identify the gaps (no expiry, no identity, no re-check) and close them with a custom channel that verifies the name, checks current_user and rejects, without touching the broadcasts.

for a principal

Decide which streams need channel-side checks versus page-level gating, and weigh key rotation, per-user stream names and connection teardown against the operational cost of each.

## What gets signed A page subscribes to live updates with `<%= turbo_stream_from @kitchen %>`. The rendered element carries two attributes: `channel="Turbo::StreamsChannel"` and `signed-stream-name`. turbo-rails builds the name from the streamables (each one's GlobalID param, joined with colons) and signs it: - `Turbo.signed_stream_verifier` is an `ActiveSupport::MessageVerifier` using SHA256 and a JSON serializer; - its key is `config.turbo.signed_stream_verifier_key` if set, otherwise `Rails.application.key_generator.generate_key("turbo/signed_stream_verifier_key")`, derived from `secret_key_base`. When the browser subscribes, `Turbo::StreamsChannel#subscribed` calls `verified_stream_name_from_params`. `MessageVerifier#verified` returns `nil` for a bad signature, and the channel calls `reject`; a good one leads to `stream_from` with the plain name. ## What that protects - **Tampering.** A user who edits the attribute to point at another kitchen produces an invalid signature and is rejected. - **Guessing.** Without the key nobody can mint a valid name for a record they never saw. So the only way to obtain a valid name is to be served a page that contains it. That makes the page's own authorization (the controller action that rendered the board) the effective gate. ## What it does not protect | gap | why | |---|---| | no expiry | the name is generated without `expires_in`, so it verifies for as long as the key is unchanged | | no identity | the same signed name is given to every viewer of that kitchen; it does not say who may use it | | no re-check | `Turbo::StreamsChannel` checks only the signature, never the connection's user | | not secret | `MessageVerifier` signs; the payload is encoded, not encrypted | The practical failures: 1. A cook is removed from the kitchen but leaves the board open. The tab, or a later reconnect with the same attribute, keeps receiving tickets. 2. A board page is fragment-cached or saved; the signed name in it stays valid. 3. An engineer logs the HTML of an admin page; the name works from any session. Rotating `secret_key_base` (or the explicit verifier key) invalidates every name at once, which also breaks every legitimate open page until it reloads; it is a blunt instrument, not access control. ## Closing the gap with a custom channel turbo-rails documents the pattern: write your own channel, reuse its naming helpers, and add a check before streaming. ```ruby class KitchenStreamChannel < ActionCable::Channel::Base extend Turbo::Streams::Broadcasts, Turbo::Streams::StreamName include Turbo::Streams::StreamName::ClassMethods def subscribed name = verified_stream_name_from_params kitchen = GlobalID::Locator.locate(name, only: Kitchen) if name.present? if kitchen && current_user&.kitchens&.include?(kitchen) stream_from name else reject end end end ``` - The view switches with `turbo_stream_from @kitchen, channel: KitchenStreamChannel`. - `current_user` comes from the connection's identifiers; the Rails 8 authentication generator writes an `ApplicationCable::Connection` with `identified_by :current_user`. - **Broadcasts need no change.** `broadcasts_to` and `Turbo::StreamsChannel.broadcast_*` publish to a stream **name** through `ActionCable.server.broadcast`; any channel that called `stream_from` with that name receives them. - The check runs on every subscribe, including reconnects, so a removed cook is rejected the next time the socket reconnects. An already open subscription keeps flowing until then; cutting it immediately is Action Cable's job (disconnecting the user's connections), not Turbo's. ## What an interviewer wants to hear - The signature prevents **forgery**, not **reuse**. - The page that rendered `turbo_stream_from` is the real authorization point, so it must be protected like any other page. - Removing someone's access takes effect for streams only on their next subscribe, and only if a channel checks. - Broadcasting is unaffected by the choice of channel, because Action Cable routes by stream name. ## Narrowing what a name reveals Private data should also live on narrower streams: `turbo_stream_from current_user, :tickets` gives each user a name nobody else is served. Combined with a channel check, a leaked page then exposes only what its owner could already see. ## The model to remember The signed name is a **bearer capability**: whoever holds it may listen. Treat the page that hands it out as the gate, and add a channel check wherever losing access must take effect without waiting for a key rotation.

  • After switching a board to a custom channel, do model broadcasts need to change?
    No. Broadcasts go to a stream name, not to a channel class: `broadcasts_to` ends in `ActionCable.server.broadcast(name, html)`. The custom channel verifies the same signed name and calls `stream_from` with it, so it receives exactly what `Turbo::StreamsChannel` subscribers did. `broadcasts_to` has no channel option, and none is needed.
  • Why not simply sign the stream name with an expiry?
    turbo-rails generates names without `expires_in`, and an expiry would cut off legitimate boards that stay open through a shift, since the attribute is only re-rendered when the page reloads. A channel-side check on each subscription ties access to the current user's rights instead of to the age of the HTML.

  • The signed stream name expires when the user's session does.
  • Signing encrypts the stream name, so users cannot read what it contains.
  • Turbo::StreamsChannel checks the connection's current_user before streaming.
  • With a custom channel, broadcasts must be sent with that channel's broadcast_to.
  • Anyone can watch another kitchen by editing the stream name in the HTML.