Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Thomas Ardal
Thomas Ardal

Posted on • Originally published atblog.elmah.io

     

Content-Security-Policy in ASP.NET MVC

In the previous post,Improving security in ASP.NET MVC using custom headers, I skipped talking about theContent-Security-Policy header entirely. It is not harder to implement, but since it requires a bit more explanation to understand, the header now has its own post. As a small teaser, I will show you an easy way to implement theContent-Security-Policy header using elmah.io in the next post.

TheContent-Security-Policy header, is an HTTP response header much like the ones from the previous post. The header helps to prevent code injection attacks like cross-site scripting and clickjacking, by telling the browser which dynamic resources that are allowed to load.

Let's start with a simple example:

<system.webServer><httpProtocol><customHeaders><addname="Content-Security-Policy"value="default-src 'self'"/></customHeaders></httpProtocol></system.webServer>
Enter fullscreen modeExit fullscreen mode

Thevalue of theContent-Security-Policy header is made up of x segments separated by a semicolon. In the example above, we only specify a single segment, saying "only load resources from self".self translates to the same origin as the HTML resource. With this minimum configuration, your HTML is allowed to fetch JavaScripts, stylesheets, etc. from the same domain that served the HTML referencing the resources. You won't be able to include external scripts from CDNs and similar.

Let's say that you host everything yourself, but want to include jQuery from cdnjs. You would need the following value to allow the browser to make requests outside your origin:

<addname="Content-Security-Policy"value="default-src 'self' https://cdnjs.cloudflare.com"/>
Enter fullscreen modeExit fullscreen mode

Remember the segments I talked about? You can configure which domains to load different kind of resources from using a range of different*-src keys like this:

<addname="Content-Security-Policy"value="default-src 'self'; script-src 'self' https://cdnjs.cloudflare.com; style-src 'self' https://maxcdn.bootstrapcdn.com"/>
Enter fullscreen modeExit fullscreen mode

This configuration lets your web application load resources from its own domain plus scripts fromcdnjs.cloudflare.com and stylesheets frommaxcdn.bootstrapcdn.com. The combinations are endless, so check out thedocumentation on Mozilla.org for details.

Chances are, you don't have a document where every dependency of your website is written down. Implementing theContent-Security-Policy header therefore takes time and digging. The best approach is to start limiting resources toself and testing the entire web application and see if it works or not. If running with developer tools open in Chrome or whatever browser may be your favorite, the Console will tell you when your web app tries to fetch or execute code not allowed in the header:

Content-Security-Policy results in the console

Another approach to catching all needed configuration, is to start by using an alternative header namedContent-Security-Policy-Report-Only:

<addname="Content-Security-Policy-Report-Only"value="default-src 'self'"/>
Enter fullscreen modeExit fullscreen mode

By adding this header instead ofContent-Security-Policy, the browser will keep telling when something isn't allowed, but allow it anyway. This way you can keep an eye on the console when running your website in production. When all error messages in the console are gone, you switch back to the original header.

Would your users appreciate fewer errors?

elmah.io is the easy error logging and uptime monitoring service for .NET. Take back control of your errors with support for all .NET web and logging frameworks.

➡️Error Monitoring for .NET Web Applications ⬅️

This article first appeared on the elmah.io blog athttps://blog.elmah.io/content-security-policy-in-asp-net-mvc/

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Entrepreneur and builder of online things. Bootstrapping elmah.io. Dad to 👦👧 Powered by ☕&🍜 Likes Star Wars, stonk trading, 3D printing, and retro games.
  • Location
    Aarhus, Denmark
  • Work
    Founder at elmah.io
  • Joined

More fromThomas Ardal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp