views:

1467

answers:

5

Traditionalist argue that stored procedures provide better security than if you use a Object Relational Mapping (ORM) framework such as NHibernate.

To counter that argument what are some approaches that can be used with NHibernate to ensure that proper security is in place (for example, preventing sql injection, etc.)?

(Please provide only one approach per answer)

+3  A: 

Use a dedicated, locked-down SQL account

Brett Veenstra
+5  A: 

Actually, NHibernate can be vulnerable to SQL injection if you use SQL or HQL to construct your queries. Make sure that you use parameterized queries if you need to do this, otherwise you're setting yourself up for a world of pain.

Kevin Pang
I guess what you mean is, as much as possible use the Criteria and Expression patterns instead of HQL. I have had the impression that HQL is in itself parameterized -- do you have a link that shows how HQL can be used for injection?
Jon Limjap
HQL is parameterized. You just can't concatenate strings in your HQL or you're doing the same thing as with SQL.
Ben Scheirman
+4  A: 

Protect your connection strings.

As of .NET 2.0 and NHibernate 1.2, it is easy to use encrypted connection strings (and other application settings) in your config files. Store your connection string in the <connectionStrings> block, then use the NHibernate connection.connection_string_name property instead of connection.connection_string. If you're running a web site and not a Windows app, you can use the aspnet_regiis command line tool to encrypt the <connectionStrings> block, while leaving the rest of your NHibernate settings in plaintext for easy editing.

Another strategy is to use Integrated Authentication for your database connection, if your database platform supports it. That way, you're (hopefully) not storing credentials in plaintext in your config file.

scott.caligan
+1  A: 

One of the arguments I've heard in favor of sprocs over ORM is that they don't want people to do whatever they want in the database. They disallow select/insert/update/delete on the tables themselves. Every action is controlled through a procedure which is reviewed by a DBA. I can understand where this thinking comes from... especially when you have a bunch of amateurs all with their hands in your database.

But times have changed and NHibernate is different. It's incredibly mature. In most cases it will write better SQL than your DBA :).

You still have to protect yourself from doing something stupid. As spiderman says "with great power comes great responsibility"

I think it's much more appropriate to give NHibernate the proper access to the database and control actions through other means, such as audit logging and regular backups. If someone were to do something stupid, you can always recover.

Ben Scheirman
+1 This is exactly what I would have said, well put (except for the spiderman comment of course :P )
Jay