views:

748

answers:

3

I searched online a bit and couldn't find anything that really nailed the spot or covered the bases how to go about setting up users/roles on a database.

Basically, there would be a user that would be used to access the database from the application (web application in this case) that will need access to database for the regular database operations (select, insert, update, delete) and executing stored procedures (with exec to run stored procedures within other stored procedures/UDFs).

Then, we would also have a user that would be main admin (this is simple enough).

I currently have a development environment where we don't really manage the security too well in my opinion (application uses a user with db_owner role, though it is an intranet application). Even though it is an intranet application, we still have security in mind and would like to see what are some of the ways developers set up the users/roles for this type of environment.

EDIT: Web application and SQL Server reside on separate machines.

EDIT: Forgot to mention that an ORM is used that would need direct read/write access.

Question: What are the "best practices" on setting up the user for application access? What roles would apply and what are some of the catches?

+1  A: 

Create a user 'webuser' that the web application uses.

Only grant stored proc execute permissions to this user. Do not allow direct table read/write. If you need to read something from a table, write a proc. If you need to write data, write another proc.

This way everything is kept nice and simple. One app user, with only the relevant permissions. If security is compromised, then all the intruder can do is run the procs.

Kev Riley
There is one issue with this (forgot some pertinent information in the original question). We use an ORM that generates classes for us and therefore cannot limit to just stored procedures to read/write data.
Manuel
sooo 2001 . . . .
Wyatt Barnett
@Manuel: ok that changes things - but if you ever remove the ORM restriction (and it is), then I would still recommend the user method over roles/schema permissions.
Kev Riley
@Wyatt: that was a good year! The age old war/discussion over how to implement security will never die, and I see you favour a different method to me. That's fine - each to their own!
Kev Riley
+3  A: 

For a long time the SQL Server guidelines for application access to the database were to isolate access to data into stored procedures, group procedures into a schema and grant execute on the schema to the principal used by the application. Ownership chaining would guarantee data access to the procedure callers. The access can be reviewed by inspecting the stored procedures. This is a simple model, easy to understand, design, deploy and manage. Use of stored procedure can leverage code signing, the most granular and powerfull access control method, and the only one that is tamper evident (signature is lost if procedure is altered).

The problem is that every bit of technology comming out from the Visual Studio designers flies in the face of this recommendation. Developers are presented with models that are just hard to use exclusively with stored procedures. Developers love to design their class models first and generate the table structure from the logical model. The procedure based guidelines reuire the procedures to exists first, before the first line of the application is written, and this is actually problematic in development due to the iterative way of modern development. This is not unsolvable, as long as the team leadership is aware of the issue and addresses it (ie. have the procedures ready, even as mocks, when the dev cycle starts).

Remus Rusanu
+1: Great answer. Highlights some very pertinent issues/considerations when designing datbases for security.
John Sansom
I forgot some information regarding our use of an ORM, that basically needs direct read/write access to the database. For something like this, would adding the roles for read and write be the appropriate way?
Manuel
From a security point of view: no. You want to protect the data from being arbitrarily updated by a compromised web server (defense in depth). Practically, there aren't feasable alternatives. At least make the access *only* to the needed tables, do *not* add the web server principal to the db_datareader/db_datawriter roles.
Remus Rusanu
I like this approach a lot, but there are probably 100+ tables in our database, most of which are accessed by the ORM objects. Maintaining this would cause a lot of headaches.
Manuel
You can group all objects (tables) into a schema and grant the necessary permissions (SELECT, INSERT, UPDATE, DELETE) on the schema. See http://msdn.microsoft.com/en-us/library/ms187940.aspx. The web app has read/write permission *only* on the object in the schema. You can even separate the objects (tables) into read-only and read-write by using two schemas.
Remus Rusanu
+3  A: 

First, I tend to encapsulate permissions in database roles rather than attach them to single user principals. The big win here is roles are part of your database, so you can completely script security then tell the deployment types to "add a user and add him to this role" and they aren't fighting SQL permission boogeymen. Furthermore, this keeps things clean enough that you can avoid developing in db_owner mode and feel alot better about yourself--as well as practice like you play and generally avoid any issues.

Insofar as applying permissions for that role, I tend to cast the net wider these days, especially if one is using ORMs and handling security through the application. In T-SQL terms, it looks like this:

GRANT SELECT, UPDATE, INSERT, DELETE, EXECUTE on SCHEMA::DBO to [My DB Role]

This might seem a bit scary at first, but it really isn't -- that role can't do anything other than manipulate data. No access to extended procs or system procs or granting user access, etc. The other big advantage is that changing the schema--like adding a table or a procedure--requires no further security work so long as you remain within that schema.

Another thing to take into consideration for SQL 2005+ is to use database schemas to secure groups of objects. Now, the big trick here is that many ORMs and migration tools don't like them, but if you render the default schema [dbo] to the app, you can use alternative schemas for special secured stuff. Eg--create an ADMIN schema for special, brutal database cleanup procedures that should be manually run by admins. Or even a separate schema for a special, highly secured part of the application that needs more granular DB permissions.

Insofar as wiring in users where you have separate boxes, even without a domain you can use Windows authentication (in Sql Server terms integrated authentication). Just make a user with the same credentials (user/pass combo) on both boxes. Setup an app domain to run as that user on the web box and setup a Sql Server user backed by that principal on the sql box and profit. That said, using the database roles can pretty much divorce you from this decision as the deployment types should be able to handle creating sql users and modifying connection strings as required.

Wyatt Barnett
I'm leaning towards this approach, as it would be cleaner with the amount of tables needed to have these permissions set. I'm just wondering if the compromise is worth it (it is in an intranet, so our risk is a little lower than a web environment).
Manuel
I really wouldn't call it much of a compromise--the ORM angle should protect against SQL injection attacks by and large, since there are not any truly "loose" strings ever hitting the database. Really, the largest attack vector here is any stored proc that calls sp_execute_sql. Moreover, application-layer security is alot easier to both build and test, relying upon specialized database permissions is a holdover to a bygone era where it was much more difficult to secure the upper parts of the stack.
Wyatt Barnett
I'm going with this approach as it works well with an ongoing development (Remus' is a very similar approach, but using a separate schema would need to move all out objects to a new schema...great for a new development, though).
Manuel