dli

dli

Do LiveView scopes go stale if permissions change mid-session?

I’ve been reading the Phoenix Scopes guide and had a question about how scopes interact with LiveView security. From what I can tell, the scope assigned in on_mount is effectively a snapshot and the current_scope assign doesn’t get refreshed for the lifetime of the WebSocket connection.

If that’s correct, an admin could revoke a user’s organization membership while they have an organization LiveView open. This wouldn’t immediately affect the socket, and subsequent handle_event calls would still carry the original scope with the org attached.

The generated context functions filter by scope (e.g. where: org_id == ^scope.org.id), but that only checks identity, not whether membership is still valid. An “edit org” event fired after revocation would still succeed if the context function trusts the scope.

Is this a real concern or am I misreading how scopes work? And if so, is there a recommended way to handle authorization for LiveViews where permissions can change mid-session?

Most Liked

steffend

steffend

Phoenix Core Team

My approach would be to broadcast a message via PubSub whenever permissions are changed. Then you have a handle_info hook (see attach_hook in the docs) that automatically updates the current_scope assign. Of course you can add any other checks or even redirect away from the LiveView if the permissions don’t allow the user to access it any more.

FlyingNoodle

FlyingNoodle

When you navigate to another liveview you probably run the same on_mount again so depending on your use case that might already be enough.

Also, nothing in phoenix or liveview is “reactive”. If you assign something during an on_mount there isnt some kind of magic dependency tracking going on.

This might seem bothersome but it makes it MUCH simpler to reason about complex systems if they work predictably like this.

If you compare this to a typical SPA like ember.js where you have an authentication service that could change at any time and then, maybe, some reactivity will trigger on the page, but sometimes it won’t because the reactivity gods didn’t bless you. It’s horrible.

garrison

garrison

Ideally you want to check the permissions transactionally while performing the user action. This is briefly mentioned in the docs (last paragraph).

If you don’t do this you will expose yourself to weird orderings which can be a security problem. Although unfortunately many databases will expose you to weird orderings anyway (skill issue on their part).

Where Next?

Popular in Questions Top

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

We're in Beta

About us Mission Statement