Row Level Security with PostgreSQL 9.5
compose.io
compose.io
When you create a RLS policy you specify a predicate (the USING and WITH CHECK parts) that is checked for each accessed row (read or write). The predicate is not in any way restricted to refer to a DB role it can compare for example a field with a parameter variable.
EDIT: Here is a gist how to not use it without roles for the permissions: https://gist.github.com/luben/4ab60b0dbda66ecf4b6601b88c8522...
I do see an issue in that if an sql injection is found, then it's trivial for the attacker to use set_foo or set session themselves.
Do you know if it is possible to run to get the system to a point where the initial connection role doesn't have permission to 'set session' itself, but does have permission to run set_foo. Where set_foo can set the session, then set a role that does not have access to execute set_foo again.
Said differently, could this be adapted so that:
1.) at the beginning of a connection, the session is unset
2.) the only way to set the session is via function
3.) once the function has been called once, it cannot be called again on the same connection
I believe it is not possible to SET ROLE or SET SESSION AUTHORIZATION with code executed within a SECURITY DEFINER function (which is what you're asking to do), though as Tom Lane points out one shouldn't rely on that:
http://www.postgresql.org/message-id/10703.1417480773@sss.pg...
Given arbitrary SQLi, it's hard to see how one can do better than setting up an untrusted sandbox like that and executing your untrusted SQL there, but being able to prevent setting a session parameter would still be useful within that context.
Something like:
CREATE OR REPLACE FUNCTION set_foo(name text) returns void as $$
my $name = shift;
die "set_foo() has already been called" if ($_SHARED{'set_foo'});
$_SHARED{'set_foo'} = $name;
$$
LANGUAGE plperl;
CREATE OR REPLACE FUNCTION get_foo() returns text as $$
my $name = shift;
return $_SHARED{'set_foo'} || 'nobody';
$$
It would be fairly trivial to extend that to support calling set_foo() once per transaction by checking against txid_current(). But if users can create plperl functions and the worry is sql injection-- It should be easy to write the same thing in C.You can still use connection pooling.
PostgreSQL is performant with hundreds of thousands of user accounts (though not pgAdmin).
create role www noinherit login password 's3cr3t';
create role alice;
grant alice to www;
Connect to database as www (same connection string = you can use pooling)After you get your connection from the pool
set role alice;
When you release you connection back to the pool reset role;
Want per-database users instead of per-cluster users? See the db_user_namespace settingUse LDAP / AD? Sync users/groups/roles to pg: https://github.com/larskanis/pg-ldap-sync
It's kind of a pain that the configuration options for that have to go in to the host-based authentication file and that it just assumes you know this.
It's not really the use case for RLS, but its possible.
START TRANSACTION;
SET LOCAL ROLE rating_role1;
SELECT * FROM ratings2;
ROLLBACK;
Works just fine.Edit: Updated
I'm talking about something in-between having 1 database role per user and 1 per app, a configurable string/ID/etc that can be set only once per connection, right at the start. That way a SQLi could only pull records from their user and not all of them.
loki=# CREATE ROLE demo CREATEROLE LOGIN PASSWORD '123';
CREATE ROLE
loki=# CREATE DATABASE demo123 OWNER demo;
CREATE DATABASE
loki=# \q
schmitch@SHANGHAI:~$ psql -h localhost -U demo -W demo123
Password for user demo:
psql (9.5.2)
Type "help" for help.
demo123=> CREATE ROLE hase123 WITH ADMIN demo;
CREATE ROLE
demo123=> SET ROLE hase123;
SET
demo123=> SELECT CURRENT_USER, SESSION_USER;
current_user | session_user
--------------+--------------
hase123 | demo
(1 row)
demo123=> RESET ROLE;
RESET
demo123=> SELECT CURRENT_USER, SESSION_USER;
current_user | session_user
--------------+--------------
demo | demo
(1 row)
So inside a Connection / Transaction you could easily switch users and create them.Also you can't switch to a Role you aren't admin, the only odd thing is, is that they could DROP roles and that they could create roles with more permissions than they have.
This is more for scenarios such as:
- a DBA needs access to work on a database, but perhaps he/she should not have access to certain financial information which would enable them to do insider trading.
- or perhaps SaaS allows for their users to have direct read access to their database (probably not smart anyway), but want to make sure an user can access information about different users.
> Your application should be enforcing this and be written in a way that SQL injections are impossible.
I wholeheartedly agree, but it's wishful thinking. It's still the OWASP #1 critical vulnerability and it's still everywhere.
It's everywhere, because you need to be aware about the problem to mitigate it. The way to mitigate it is that you should never build SQL command in your language (concatenating, formatting etc). Instead you should use variable binding / parametrized queries.
When you do you that you ensure that there's separation between an SQL statement and the data and no untrusted data can affect the SQL statement.
If you are programming in Python and use PsycoPG2 then first section talks about it[1][2].
It looks like lack of quote_ident is a feature here because it makes you think "what the heck I'm doing?".
Maybe it'd be more useful if you're using a single DB for a multi-tenant setup, and you know each tenant's data is strictly isolated?
Stick all of your customer data in a single database. Implement RLS per customer. Now "Pepsi" can't see "Coca-Cola's" data.
Essentially creating a virtual private database per customer by using RLS.
edit:typo
Might need transactions, I'm not sure. I fiddled around with it a bit but it was too much overhead to work with.
I ask because for my next project I'd like to tackle the issue of having to keep lots of Databases up to date with their stored procedures. Kind of wanted a common library of procs that any DB can access. I've seen third party Software do DB versioning etc but too expensive for me. A few do it via package management, but keen to see how others are doing it!
(Now I just hope I don't get flammed)
That's kind of a sticking point for web apps. Wonder if there's a way around that.
But establishing a DB role for every distinct person within a department gets awkward very fast, particularly if permissioning requirements have a bit of business logic associated with them (e.g., "this row can be updated by the user designated in the row itself as "owner", and is visible to both them and anyone designated in this other table over here as their assistant...") So, you'll often still need additional permissioning logic within the webapp regardless.
Your hospital example is spot on, and in finance a department might be a trading group.
https://stackoverflow.com/questions/2998597/switch-role-afte...
I ask because we're planning on using jsonb here soon, and we were hoping it would be available in the version of Postgres available in ubuntu 16.04.
EDIT: Looks like [1] talks about what he was mentioning, there's a number of functions they added to improve the use and experience in 9.5.
[1] https://www.compose.io/articles/could-postgresql-9-5-be-your...
Prior to 9.5 there weren't commands to edit values within a JSONB column.
A better solution would set a query on the connection that constrains which rows it can touch. I know this ends up being more complicated and less performant but it splits the difference between app-level and database-enforced security.