You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enable business logic enforcement with security rules #4210
A key feature that is missing before RethinkDB can function as a full-fledged BaaS alternative is the ability to define and enforce business logic with a security rules engine. Check out Firebase's rules engine for an example.
changed the title [-]Enable fine grained access control with security rules[/-][+]Make it possible to embed business logic with security rules[/+]on May 10, 2015
changed the title [-]Make it possible to embed business logic with security rules[/-][+]Enable business logic enforcement with security rules[/+]on May 10, 2015
We've been planning to add user accounts and per-user privileges to RethinkDB (either directly or as an external proxy), see #218 . We don't have the feature scheduled yet, but it's definitely on our short list of features to add.
Which types of security rules would be most important for you?
@danielmewes I'd really like the rules to be able to be arbitrarily complex using ReQL with a few extra objects for referencing state. Be sure to study the way Firebase does it to get a flavor for what I'm talking about. Basically, you need some core concepts, including:
An object representing the currently authenticated user
An object representing the state of a document before a proposed change would occur
An object representing the state of a document after a proposed change would occur
Ability to specify boolean validation functions at any level of depth in a document that return true or false for whether or not a write/read operation should be allowed. These functions should be able to use ReQL to query the existing db state.
Thanks for the explanation, I think I got the idea.
So for example we could allow specifying a ReQL function on a table that gets the three objects you mention as its input and then returns whether this modification is permitted or not.
I actually really like this idea. It can be used for anything from access control to schema enforcement.
This covers the side of write operations. We would still need to cover read operations. Maybe that could happen in a similar way, by defining a function on a table that determines which documents can be read by a given user and which cannot.
For security, we would in addition have to restrict certain terms. Most importantly r.http and things like reconfigure.
Yes, and just to clarify, the functions would need to be able to query other tables besides just the one being written to, in order to support arbitrary business logic enforcement.
Cross-document logic enforcement would be very hard to do as far as I can tell.
This is because RethinkDB doesn't support cross-document transactions and thus we would either have to sacrifice atomicity of an update or would need to hold write locks over extended periods while looking up information from elsewhere.
@danielmewes consider a simple example. You've got a comments table and a users table. You want to make sure that users can only edit comments that they created. This requires retrieving the currently authenticated user's record from the users table and checking that its id is the same as the createdBy property on the comments table.
Here's a more complex example we're currently doing in firebase. We have an embeddable web plugin that we track usage on to determine how much to charge our customers. We want to make sure that they aren't bypassing our usage tracking by making sure that they can only read from a comments table if they have recently incremented a pageViews property on another table. This requires us to check if the time interval between the last update to one table is less than a certain amount before allowing them to read from the comments table.
Agree with cayblood - business transactions must be secure and must be able to span multiple documents.
High level security requirements (for our web app):
Apply on a "transaction type" where each transaction accesses one or more document. Essentially this translates the user transaction into one or more system transactions.
Define transaction as read or write: read only may not change any data
Business Rules. This looks at the transaction in context with the user e.g. an "Update Personal Settings" transaction would be disallowed if the requesting UID is not authed or if is not the UID in the record being updated. Must be able to apply multiple security validation steps per transaction type.
Cross Table Business Rule example. A cross table example would be "Get Friend Profile" where the rule is if the requested profile's UID is not in the requesting user's UID's Friends List then don't allow the get. The last example is an absolute must have for any web app otherwise anyone hijacking a valid client session can pull anybody's profile.
Changefeeds. Explicitly in our app, only UIDs that are members of a convo should be able to listen to it. A changefeed subscription should therefore be controlled like a read.
Nonsense request. If the transaction received has a payload that that doesn't make sense (e.g. is not JSON that contains the 'fields' that are needed to implement a security rule) then it should be denied.
All fails should be logged (somewhere).
This is our hottest topic - if anyone has or can point us to a security rules solution to front end RethinkDB would love to hear from you.
I think simple cases of cross-table rules might be possible. For example checking a user record in another table doesn't necessarily require the check to be atomic. We won't be able to guarantee atomicity for cross-table rules, but I think this will be fine for many use cases, though not all.
This would be useful for mumble security features. We need to have the db engine do the rules check where both existing and proposed document are available for the rule, but may or may not be committed atomically.
@deontologician Can you elaborate a bit? What do you mean by "may not be committed atomically"? Would the check be only on the current document, or would it need to perform cross-document checks?
So, imagine a rule that says "the a field needs to be less than the c field" and an update comes in that increases the a field, and another update comes in at the same time decreasing the value of the c field. Both may independently pass the rule, but without the db guaranteeing atomicity, the document in the database may violate the rule.
We basically need to add a place to specify a function on a table, not unlike a secondary index. Then we evaluate that function atomically before writing a given document, and let the write fail otherwise.
I can try to write up a complete proposal for this some time in the next days.
Edit: I just tried and returning r.error from an update function indeed seems to work as expected. So the above can be used as an application-level work-around for the time being (you might want to use r.do for newVal to avoid the repeated evaluation of merge(changes)).
Activity
[-]Enable fine grained access control with security rules[/-][+]Make it possible to embed business logic with security rules[/+][-]Make it possible to embed business logic with security rules[/-][+]Enable business logic enforcement with security rules[/+]danielmewes commentedon May 11, 2015
Hi @cayblood, thank you for the suggestion.
We've been planning to add user accounts and per-user privileges to RethinkDB (either directly or as an external proxy), see #218 . We don't have the feature scheduled yet, but it's definitely on our short list of features to add.
Which types of security rules would be most important for you?
cayblood commentedon May 11, 2015
@danielmewes I'd really like the rules to be able to be arbitrarily complex using ReQL with a few extra objects for referencing state. Be sure to study the way Firebase does it to get a flavor for what I'm talking about. Basically, you need some core concepts, including:
Does this make sense?
danielmewes commentedon May 11, 2015
Thanks for the explanation, I think I got the idea.
So for example we could allow specifying a ReQL function on a table that gets the three objects you mention as its input and then returns whether this modification is permitted or not.
I actually really like this idea. It can be used for anything from access control to schema enforcement.
This covers the side of write operations. We would still need to cover read operations. Maybe that could happen in a similar way, by defining a function on a table that determines which documents can be read by a given user and which cannot.
For security, we would in addition have to restrict certain terms. Most importantly
r.httpand things likereconfigure.cayblood commentedon May 11, 2015
Yes, and just to clarify, the functions would need to be able to query other tables besides just the one being written to, in order to support arbitrary business logic enforcement.
danielmewes commentedon May 11, 2015
Cross-document logic enforcement would be very hard to do as far as I can tell.
This is because RethinkDB doesn't support cross-document transactions and thus we would either have to sacrifice atomicity of an update or would need to hold write locks over extended periods while looking up information from elsewhere.
How critical are cross-table functions @cayblood?
cayblood commentedon May 11, 2015
@danielmewes consider a simple example. You've got a comments table and a users table. You want to make sure that users can only edit comments that they created. This requires retrieving the currently authenticated user's record from the users table and checking that its id is the same as the createdBy property on the comments table.
Here's a more complex example we're currently doing in firebase. We have an embeddable web plugin that we track usage on to determine how much to charge our customers. We want to make sure that they aren't bypassing our usage tracking by making sure that they can only read from a comments table if they have recently incremented a pageViews property on another table. This requires us to check if the time interval between the last update to one table is less than a certain amount before allowing them to read from the comments table.
cayblood commentedon May 11, 2015
In other words, I think that cross-table functions are nearly essential for most schemas.
pagejohn commentedon Jun 29, 2015
Agree with cayblood - business transactions must be secure and must be able to span multiple documents.
High level security requirements (for our web app):
This is our hottest topic - if anyone has or can point us to a security rules solution to front end RethinkDB would love to hear from you.
danielmewes commentedon Jun 30, 2015
Thanks for the additional feedback @pagejohn and @cayblood .
I think simple cases of cross-table rules might be possible. For example checking a user record in another table doesn't necessarily require the check to be atomic. We won't be able to guarantee atomicity for cross-table rules, but I think this will be fine for many use cases, though not all.
pagejohn commentedon Jun 30, 2015
@danielmewes & @cayblood. Each application would need to implement its own security rules... but those can be abstracted. See here: https://github.com/mikemintz/rethinkdb-websocket-server. Mike Mintz calls them validation rules but this code seems to be conceptually what is required.
deontologician commentedon Dec 10, 2015
This would be useful for mumble security features. We need to have the db engine do the rules check where both existing and proposed document are available for the rule, but may or may not be committed atomically.
danielmewes commentedon Dec 10, 2015
@deontologician Can you elaborate a bit? What do you mean by "may not be committed atomically"? Would the check be only on the current document, or would it need to perform cross-document checks?
deontologician commentedon Dec 10, 2015
So, imagine a rule that says "the
afield needs to be less than thecfield" and an update comes in that increases theafield, and another update comes in at the same time decreasing the value of thecfield. Both may independently pass the rule, but without the db guaranteeing atomicity, the document in the database may violate the rule.I think current document only would be fine
danielmewes commentedon Dec 10, 2015
Oh I see. That case should be technically easy.
We basically need to add a place to specify a function on a table, not unlike a secondary index. Then we evaluate that function atomically before writing a given document, and let the write fail otherwise.
Essentially we would translate a write
into
I can try to write up a complete proposal for this some time in the next days.
Edit: I just tried and returning
r.errorfrom an update function indeed seems to work as expected. So the above can be used as an application-level work-around for the time being (you might want to user.dofornewValto avoid the repeated evaluation ofmerge(changes)).deontologician commentedon Dec 10, 2015
sometimes I forget how awesome reql already is