Data Access Authorization/Security

Status
Not open for further replies.

cls33

Smack-Fu Master, in training
99
A quick question about securing data access for different user roles/types.

I have a web application (standard create/edit/view/delete) that utilizes user roles pretty heavily.

First a quick (abstracted) overview.

I have some hierarchical entity relationships:
Organizational Entities
Business Entities

Many business entities are grouped under one organizational entity

This leaves me with 3 types of users (abstracted a bit) who all have access to different data:
Admins - can see/manage everything
Organizational Admins - can see/manage everything created by any business entity below their organizational entity
Business Users - can create/see/manage anything created by their specific business entity (this is the only user type that can create new objects)

So, being a web app this data is all stored in a DB (MySQL) and the challenge is finding a clean way to put controls around the data access piece to ensure that a user is only accessing data that they have rights to see.

Much of the SQL is dynamically generated, so what we do today is a lot of "if" checking user types and dynamically altering or appending the appropriate "WHERE" clause to the query in every place where we make these calls. It works, but it's not very clean or maintainable and it makes me cringe a little when I think about it. It makes it extremely difficult to add new user types or to change the business logic of existing user types. I can't help but think that there must be a better way. I have a few ideas, but nothing that I think I could actually get to implementation without tripping over a multitude of problems, so I'm hoping someone can give me some insight into "industry standard" on how this sort of thing is handled.
 

spankalee

Ars Tribunus Militum
2,063
Sometimes it's difficult to separate security from the business logic of an application. This is almost always true when the permissions a user has for an object depend on state that's spread out over your data. I've seen two general approaches to this problem:

1) Reduce the security logic to a standard access-control-list type system (row-level permissions), and modify the ACLs as state affecting permissions changes. This lets you have a separate security layer that's independent from whatever particular rules govern access in your app, but re-writing the ACLs amounts to implementing the app-specific security logic anyway. In my experience this might be over-generalization, unless the security layer is in place already.

2) Write decoupled security and business logic layers that both depend on data. This might be pure, but I've usually seen it grow into a mess for the sake of purity. Often you need the same data in each layer and you want to avoid loading it twice, so maybe you cache it, and caching is best avoided if possible, or you pass some data from the security layer into the business logic layer, which means that the logic layer know gets data from the security layer and from the data storage. Yuck. I think I've never seen this approach not break own into ugliness at some point.

3) Just admit that security and data are intertwined and live with the if checks and combination of security and business logic. This isn't so bad to me if it's done right. The danger here is forgetting a check, or implementing one wrong, which you can alleviate with some abstractions and testing. Note, that this is for the access controls that really do depend on the data in some non-trivial way. If it's a check like isAdmin() everywhere, and you can very cleanly separate it out, try to.

Here's how I'd go about it:

Force all data access to go through some data access wrapper. The app shouldn't need to know that it's backed by SQL. The wrapper could be very thin and the queries all implemented by stored procs in the db, or it could be thicker and be done with an ORM, whatever suits you. The abstraction away from the underlying data store is useful for testing. Implement your business logic in another layer gets the data from the data layer, performs security checks and does whatever business logic it's supposed to.

Then to ensure the security checks are being performed, write security tests that call the logic layer with cases that should should fail, like loading an object from a different business entity. To ensure the queries like "get all foos my organization" are only returning the correct data, write tests that check that the correct parameters are passed from the logic layer to the data layer (use a mock for the data layer). In turn you should test that the data layer does it's job correctly by using a test/mock db.

Hope that helps
 
Status
Not open for further replies.