Giter VIP home page Giter VIP logo

Comments (13)

begriffs avatar begriffs commented on May 22, 2024 1

Note to self, also include example of read-only or invisible fields based on user permissions as @sqwishy requested in the chat.

I guess in my case I'd want to use different views for hiding or showing some fields. But I'm not sure how the read-only thing would work except to write a trigger to do that validation on update for every field I guess?
Although, really with read-only fields I'd prefer that requests containing them be rejected outright - even if the values match what is already on the object.

from postgrest-docs.

steve-chavez avatar steve-chavez commented on May 22, 2024 1

@sqwishy Yes, RLS rules are applied to the view owner and it's likely that you have a SUPERUSER(or a role with BYPASSRLS) as the owner and that will bypass RLS.

What you can do is to alter view api.player owner to <non-superuser> and then create policy for that role(or leave it at the default PUBLIC so it applies to all roles), then your RLS rules will work normally for that view.

There's an example in postgrest-starter-kit where a dedicated "api" role is created for this,
which has rls policies applied and then the views are owned by this role.

from postgrest-docs.

PierreRochard avatar PierreRochard commented on May 22, 2024

This may be a little tangential... I banged my head against RLS and ended up enforcing it in the API views like so: WHERE admin.table_settings.user = current_user; Is this a valid alternative or is there a vulnerability?

I'll work through 2ndQuadrant's blog post this weekend to contrast

from postgrest-docs.

begriffs avatar begriffs commented on May 22, 2024

Also @daurnimator poses another scenario we might want to model:

you can see all friends details; some friends of friends details; and anyone else visibility is determined by privacy settings

from postgrest-docs.

begriffs avatar begriffs commented on May 22, 2024

Planning to make this tutorial 2 (we've got 0 and 1 already written)

from postgrest-docs.

lidorcg avatar lidorcg commented on May 22, 2024

Hi, I'm trying to tackle authentication and I followed the example on the docs and it seems I have generated the mentioned functions and tables but:

  1. I can't reach the login function on /rpc/login
  2. The user registration part is not the clearest to me - I think it's accomplished by insertion to the user table, but the auth schema is not published so I'm not sure what is the best way to solve this.

If I could get some pointers on how to continue, I'd also be very happy to contribute to the docs.

Thanks :)

from postgrest-docs.

begriffs avatar begriffs commented on May 22, 2024

Sorry for the silence. Did you manage to make the login function work?

from postgrest-docs.

JacobEvelyn avatar JacobEvelyn commented on May 22, 2024

I too am struggling with this example. First I was getting

ERROR:  type "basic_auth.jwt_token" does not exist

when trying to create the login function. Then when I changed that to public.jwt_token I can't seem to execute the login function with /rpc/login as @lidorcg described.

It feels like I'm close but I'm pretty inexperienced with functions/stored procedures in Postgres. 😕

from postgrest-docs.

lidorcg avatar lidorcg commented on May 22, 2024

Sorry for not replying as well, I haven't been able to solve the problem myself (probably for lack of trying).
However, I have found the subzero project which has users and authentication procedures built-in.

from postgrest-docs.

sqwishy avatar sqwishy commented on May 22, 2024

I could be wrong, but it seems that row-level security policies are not enforced when a table is being accessed through a view. Instead, the table is accessed using the privilege of the view's owner, instead of the current role?

This is based on a small amount of experimentation and my poor reading of
https://www.postgresql.org/docs/10/static/sql-createpolicy.html

If this is the case, I suppose the thing to do is duplicate the security policy into the view's where clause (and maybe use with (security_barrier) for good measure?). Can anyone confirm if my understanding is correct and if there isn't a better solution?

See also:
https://www.postgresql.org/docs/10/static/rules-privileges.html

Work:

aleatory=# \d+ api.player
                             View "api.player"
   Column   |           Type           | Modifiers | Storage  | Description 
------------+--------------------------+-----------+----------+-------------
 id         | uuid                     |           | plain    | 
 email      | text                     |           | extended | 
 password   | text                     |           | extended | 
 last_login | timestamp with time zone |           | plain    | 
View definition:
 SELECT player.id,
    player.email,
    player.password,
    player.last_login
   FROM impl.player;
Options: security_barrier=true

aleatory=# \d impl.player
                             Table "impl.player"
   Column   |           Type           |              Modifiers              
------------+--------------------------+-------------------------------------
 id         | uuid                     | not null default uuid_generate_v4()
 email      | text                     | not null
 password   | text                     | not null
 last_login | timestamp with time zone | 
...
Policies:
    POLICY "player_policy" FOR ALL
      USING ((("current_user"())::text = (id)::text))
      WITH CHECK ((("current_user"())::text = (id)::text))
...
aleatory=# set role anonymous;
SET
aleatory=> select id from impl.player;
 id 
----
(0 rows)

aleatory=> select id from api.player;
                  id                  
--------------------------------------
 cc2f1706-0df7-436d-a46e-4ada21c526ae
 2fa79e43-cfc2-4452-9735-9e4495565837
(2 rows)

from postgrest-docs.

steve-chavez avatar steve-chavez commented on May 22, 2024

FYI, there's an RLS bug on VIEWs that @daurnimator reported in psql-bugs.

Basically, subqueries in the RLS policy are not checked against the privileges of the view owner but of the view caller.

I also bumped in to this bug when working on a example for the rls tutorial.

Edit: I think the most simple workaround for this would be wrapping the subquery in a security definer function.

from postgrest-docs.

steve-chavez avatar steve-chavez commented on May 22, 2024

The bug was fixed https://www.postgresql.org/message-id/CAEZATCV_yDYoptaxtjiVB4yLwxQ%3DN7OWu8Ls98rA5MvBL%2BjKiQ%40mail.gmail.com.

Haven't tried and see if it's available on pg recent releases though.

from postgrest-docs.

wolfgangwalther avatar wolfgangwalther commented on May 22, 2024

By now we have examples for app users + row level security!

from postgrest-docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.