Help forum

How do you manage table access based on user profile (supabase)?

  • unicodes

    1 year ago

    Do you know what is more performant for managing access to supabase tables?

    I'm curios if having one single RLS to manage all SELECT based on each user profile (admin, manager, etc) or having separate RLS.

    I have in my user table a column with this profile type, do you recommend to do something like (SELECT profile FROM users WHERE id = auth.uid()) = "manager" or do you recommend to add at login this information in the JWT and manage the select based on the JWT select. What would be faster, selecting from JWT or from the tables?

    Do you have a better alternative?
  • Erik Beuschau

    1 year ago

    We're not storing much information in the jwt. Supabase is usually fast enough 🙂
  • unicodes

    1 year ago

    Thank you @Erik Beuschau , do you recommend one single big RLS to manager SELECT or having multiple RLS SELECT rules? Are them called sync or async?
  • Erik Beuschau

    1 year ago

    I believe Supabase's performance advisor recommends usingn fewer rules. But we often use multiple for clarity. They are called/used before the operation (GET/UPDATE/INSERT) is performed. So synchronously
    🙏1
  • unicodes

    1 year ago

    Thank you, Erik, for your input. I wrote this question hoping you would reply, and indeed you was the first one. I appreciate your feedback! 🤩
    🙌1
  • Lucas G

    1 year ago

    Adding that having a lot of info in your JWT is faster but has drawbacks.
    You can’t invalidate active JWTs if the user’s data or access ever changes
  • Lucas G

    1 year ago

    It’s not as big a deal if you have very short lived tokens but it is if you have for example a week+ long life on them
  • Lucas G

    1 year ago

    Multiple RLS policies do slow down queries eventually but it’s not an issue unless you have bigger tables. Supabase gives you recommendations on how to write them
  • Like not directly using auth.uid() etc
  • unicodes

    1 year ago

    Thank you @Lucas G . Do you have views without RLS activated? I created a view because I want to grant access based on the view’s "WHERE" select, which works like RLS. However, I can’t activate RLS because it applies the table’s RLS policy, and that policy only allows access for the owner (I don't want to change that), but that red dot is really frustrating. Should I find a solution to views with RLS only activated? I have this visual blockage 😅
  • Lucas G

    1 year ago

    No I’d never leave a table without RLS or some type of security layer
  • unicodes

    1 year ago

    No, isn't a table, is a view.
  • unicodes

    1 year ago

    Like you have users table with rls only for owners. I created a view for users that are public, and toggle public. (In this way I can give access only to a selected columns only and fetch data from other tables too, but not all columns).
  • Lucas G

    1 year ago

    Still. A WHERE clause isn’t security.
  • Is the main thing
  • If the table doesn’t have data that should only be viewed by certain people then it doesn’t matter as much
  • Lucas G

    1 year ago

    View*
  • unicodes

    1 year ago

    How would you proceed in this situation? You have users table and users_jobs, both could be selected only by owner, but in some case if owner want's to share part of the info, you have a view with a select of the users columns and users_jobs columns where users.public = true. Why that where shouldn't be secure?
  • Lucas G

    1 year ago

    I'd either add a specific role/key to the public call that has its own RLS policy (like view public items only) or, most likely, I'd use a worker to validate the call and fetch the items from there.
  • unicodes

    1 year ago

    I can't use RLS because you don't want to grant visibility on all columns, maybe just first and last name. And a worker, yes, I could use a worker, but it has the same select as I have in the view. On the other hand, a view allows me to add a "foreign key" (not a real foreign key, just a simulation), whereas you can't do that with a worker, a view is much more practical.
  • unicodes

    1 year ago

    This is why I'm stuck with that red dot for "safety" even if is very safe, but workarounds (practical ones) are very limited vs View.
  • Lucas G

    1 year ago

    It's unsafe in the sense that if the underlying table is known then somebody can just query that table directly since it has no policies. I don't know what the full setup here is obviously so I don't know how "unsafe" it really is.
    I wouldn't say a worker is restricted though. You can pull whatever data you need and return only that, the endpoint is public but it would only return limited data.
  • Lucas G

    1 year ago

    And there could still be an RLS policy to protect the table itself.
  • Lucas G

    1 year ago

    By worker I mean something like edge functions*
  • unicodes

    1 year ago

    Yes, I know.
  • Lucas G

    1 year ago

    I stopped using Supabase almost a year ago though I never used edge functions even then lol
  • unicodes

    1 year ago

    What I mean si that the table has RLS only for owners, the view doesn't have because there I use where and is a very good select. Adding rls on the view would limit the select to owners only, but without RLS gives me flexibility and help me to keep users table safe in the same time I'm sharing some data in a safe way too but without RLS using a view. @Erik Beuschau what do you think on such case, it worth using views without rls because the view select where is enoght, but the table has rls?
  • Lucas G

    1 year ago

    Views use the RLS from the underlying table no?
  • They don't have their own RLS
  • unicodes

    1 year ago

    What do you use as main backend/database?
  • Lucas G

    1 year ago

    So you do have RLS then
  • unicodes

    1 year ago

    exactly, this is why, if you activate an RLS over the views (not on table, because over tables is mandatory always on), you limit your select based on table rls.
  • unicodes

    1 year ago

    on the table yes, but not on the view.
  • Lucas G

    1 year ago

    Ok you're losing me lol. You're saying the table has RLS but then also saying the table does not have RLS.
  • enabled
  • unicodes

    1 year ago

    Here, jobs has RLS (restricted to organization) but if you taggle public and is approved, the view (jobs_published) does a select of all jobs.published = true, and shows only part of necessary tables (jobs, jobs_skills, etc..), not full data of the table (I need just title maybe).
    1441571861506035752-Screenshot_2025-11-22_at_01.30.52.png
  • unicodes

    1 year ago

    jobs is table, jobs_publiched is a view. jobs has rls, jobs_published doesn't because is using a where that is limiting access. Like where user.name = 'Lucas'. Only you can view data from jobs_published (the view, not table), and I don't get how you could trick that where from the view.
  • unicodes

    1 year ago

    I'm using views here because are very powerful and flexible, but is making me nervous seeing that "Unrestricted" even if is very safe.
  • Lucas G

    1 year ago

    Sorry, wasn't home and couldn't continue replying
  • Lucas G

    1 year ago

    Again, the "unsafe" thing is relevant. It doesn't sound like the view exposes sensitive data or data that shouldn't be access by unauthorized users so it probably doesn't matter. That Unrestricted tag is there on the UI to let people know that views don't have policies on their own
  • Lucas G

    1 year ago

    I pinged the endpoint and can see that there's a position for a "NetSuite Developer". If that's not something I should be able to do that easily then you have your answer lol
  • unicodes

    1 year ago

    Is perfectly fine if you can see that data, I’m making it public so anyone can see the published jobs, but you see only that one, in jobs there are more meaning that it works.