Skip to content

Enable business logic enforcement with security rules #4210

Open

Description

@cayblood

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.

Activity

  1. 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
  2. changed the title [-]Make it possible to embed business logic with security rules[/-] [+]Enable business logic enforcement with security rules[/+] on May 10, 2015
  3. danielmewes commented on May 11, 2015

    @danielmewes
    Member

    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?

  4. cayblood commented on May 11, 2015

    @cayblood
    Author

    @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
    • Tests for equality, lt, lte, gt, gte, number, string, regex matching
    • 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.

    Does this make sense?

  5. danielmewes commented on May 11, 2015

    @danielmewes
    Member

    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.

  6. added this to the subsequent milestone on May 11, 2015
  7. cayblood commented on May 11, 2015

    @cayblood
    Author

    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.

  8. danielmewes commented on May 11, 2015

    @danielmewes
    Member

    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?

  9. cayblood commented on May 11, 2015

    @cayblood
    Author

    @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.

  10. cayblood commented on May 11, 2015

    @cayblood
    Author

    In other words, I think that cross-table functions are nearly essential for most schemas.

  11. pagejohn commented on Jun 29, 2015

    @pagejohn

    Agree with cayblood - business transactions must be secure and must be able to span multiple documents.
    High level security requirements (for our web app):

    1. 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.
    2. Define transaction as read or write: read only may not change any data
    3. 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.
    4. 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.
    5. 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.
    6. 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.
    7. 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.
  12. danielmewes commented on Jun 30, 2015

    @danielmewes
    Member

    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.

  13. pagejohn commented on Jun 30, 2015

    @pagejohn

    @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.

  14. deontologician commented on Dec 10, 2015

    @deontologician
    Contributor

    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.

  15. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    @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?

  16. deontologician commented on Dec 10, 2015

    @deontologician
    Contributor

    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.

    I think current document only would be fine

  17. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    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

    table.get(...).update(changes)

    into

    table.get(...).replace(function (oldVal) {
      var newVal = oldVal.merge(changes);
      return r.branch(validate(newVal), newVal, r.error("Validation failed"));
    })

    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)).

  18. deontologician commented on Dec 10, 2015

    @deontologician
    Contributor

    sometimes I forget how awesome reql already is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions