Joining a LiveView without "url" skips its live_session on_mount hooks
Description
Incorrect Authorization vulnerability in phoenixframework phoenix_live_view allows an authenticated user to mount a router-mounted LiveView without the on_mount hooks of its live_session via a phx_join message that carries neither a "url" nor a "redirect" key.
The catch-all clause of the private Phoenix.LiveView.Channel.authorize_session/3 in lib/phoenix_live_view/channel.ex accepts a join without a URL for any session, including a root LiveView mounted at the router. The channel then mounts the LiveView with no route, and only a route adds the live_session hooks to the mount lifecycle. Checks that those hooks perform on the connected mount do not run, and event checks that they attach through attach_hook/4 are never attached. The attacker needs a valid session token for the LiveView, which only the HTTP render issues, after the hooks pass there. on_mount hooks declared in the LiveView module still run.
This issue affects phoenix_live_view: from 0.16.0 before 1.0.20, from 1.1.0-rc.0 before 1.1.34, and from 1.2.0-rc.0 before 1.2.14.
Details
1. Join authorization. The private Phoenix.LiveView.Channel.authorize_session/3 in lib/phoenix_live_view/channel.ex has three clauses. The "redirect" clause resolves the route and checks that it stays in the same live_session. The "url" clause requires, for a session where Phoenix.LiveView.Session.main?/1 is true, that the route for the URL matches the view and the live_session of the session. The third clause matches any other join parameters and returns the session with no route and no URL. Nested LiveViews need this clause because they have no URL. The clause does not call Phoenix.LiveView.Session.main?/1, so it also accepts a root LiveView that was mounted at the router.
2. Mount with no route. With no route, mount/3 receives :not_mounted_at_router as its params. The private load_lifecycle/2 adds the on_mount hooks of the live_session only when it receives a %Phoenix.LiveView.Route{}. Without a route it returns the lifecycle of the LiveView module alone. The live_session layout is skipped in the same way.
3. Result. The signed session token, the static token, the CSRF token, and the session cookie are all valid, so the join succeeds. The LiveView runs without the live_session hooks. Hooks that halt the mount do not run, and hooks attached with attach_hook/4 for :handle_event or :handle_info are never attached.
Conditions that stop the mount. A LiveView that defines handle_params/3 raises on a mount without a route. A LiveView whose mount/3 matches its params as a map raises. A LiveView that reads an assign that a skipped hook sets raises. In these cases the join fails.
The official JavaScript client always sends "url" or "redirect" for a root LiveView. Only nested views join without either key.
Proof of concept
The target application has a live_session whose on_mount hook halts the mount for a suspended account and attaches a :handle_event hook with attach_hook/4 that denies the delete_post event to users who are not admins. The LiveView at /posts does not define handle_params/3.
- Sign in as a user without the admin role and load
/postsover HTTP. Keep the session cookie, the CSRF token from the page, and theid,data-phx-session, anddata-phx-staticattributes of the element that hasdata-phx-main. - Open the LiveView websocket at
/live/websocketwith the session cookie and the CSRF token. - Send a
phx_joinmessage on the topiclv:<id>. The payload has"params"(with"_csrf_token"and"_mounts": 0),"session", and"static". It has no"url"key and no"redirect"key. - The server replies with status
ok. The LiveView is mounted without thelive_sessionhooks. - Send an
eventmessage of typeclickfor the eventdelete_post. The LiveView handles the event, and the post is deleted.
The same join with the "url" key set to the page URL runs the hooks, and the delete_post event is denied. If the account is suspended after step 1, the join with "url" is refused, and the join without "url" still succeeds.
Impact
An authenticated user can send events that the live_session hooks would deny. A user whose access was revoked after the page loaded can still mount the LiveView and send events while the LiveView session token is valid.
Workarounds
Move the security-relevant on_mount hooks from the live_session to the LiveView module with Phoenix.LiveView.on_mount/1, for example in the live_view definition that use MyAppWeb, :live_view expands. Hooks declared in the module run on every mount.
Alternatively, make mount/3 match its first argument as a map (%{} = params), or define handle_params/3 in the LiveView. In both cases a mount without a route raises, and the join fails.
Configurations
The application enforces authentication or authorization in on_mount hooks of a live_session, and the LiveView does not do the same check itself. The LiveView must also mount without a route: it does not define handle_params/3, its mount/3 accepts :not_mounted_at_router as its params, and it does not read assigns that the skipped hooks set.