views:

95

answers:

5

How do I secure my "hibernate.cfg.xml" file? (Nhibernate). It currently sits in the route of the web app and can be viewed via the browser.

+1  A: 

That's a very good question.

There must be a few ways to secure it. One I can think of is - if you are not serving an XML in the web site - to change the MIME type of the XML registered against the web site so that it is not served.

Other solutions not directly securing the "hibernate.cfg.xml" file:

1) Define the configuration in web.config using NHibernate section handler

2) Configure in the code

3) Setup NHibernate to read a file with .config extension

Aliostad
A: 

Put NHibernate config in web.config, this way it won't be viewed in browsers.

sh_kamalh
+1  A: 

It could live in the bin directory and that way it is protected as well as if if you need to modify it, your app will auto restart as a change was made in the bin directory.

edit
you might actually want/have to do this if your nhibernate code lives in a separate class library such that you don't want to mix its configuration in with the web.config (or app.config if sharing the library between interfaces)

davidsleeps
1+ for mentioning bin
Aliostad
+4  A: 
<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="hibernate.cfg.xml"
           type="System.Web.HttpForbiddenHandler"/>
    </httpHandlers>
  </system.web>
<configuration>
Diego Mijelshon
This seems clean, interested to hear peoples views on this...
Andi
BTW, that's exactly what is done by default with *.config files
Diego Mijelshon
+2  A: 

Your hibernate.cfg.xml should be set to 'Copy to Output Directory: Copy Always' When you build it it is copied into your output direct. If you publish your site, it will only be in your output (bin) directory so noone will be able to access it.

Alistair
Any comments on this?
Andi
Check out: http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx 'Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property "Copy to Output" to "Copy always".' If you do this then it is read from your bin folder (and protected from being accessed). There is no reason for it to exist anywhere else.
Alistair