A Professional ASP.NET Core API - Security Headers
With the help of headers, your website could send some useful information to the browser. Let’s see how it is possible to add more protection to your website.
To add a header for each request, we can use middleware.
Enforce HTTPS
HTTPS is pretty awesome. It not only encrypts the traffic between the client and server so others can’t see it, but also prevents others from modifying the content. So it also provides integrity. And being that you can now have HTTPS for free with services like Let’s Encrypt, most apps should start to look into using HTTPS.
1 |
|
Headers via middleware
This is my favorite. Specifying headers in middleware can be done in C# code by creating one or more pieces of middleware. Most examples in this post will use this approach. In short, you either create a new middleware class or call the Use
method directly in the Configure
method in Startup.cs
:
1 |
|
The code adds a new header named Header-Name
to all responses. It’s important to call the Use
method before calling UseEndpoints
, UseMvc
, and similar.
Types of headers
The following list examines an important part of application headers.
Strict-Transport-Security (HSTS)
It tells the browser: “You shall only access this URL over a secure connection.”. By submitting a Strict-Transport-Security
header, the browser saves it and redirects itself to the HTTPS version without making an insecure call.
1 |
|
X-Frame-Options
The X-Frame-Options
HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a <frame>
, <iframe>
, <embed>
or <object>
. Sites can use this to avoid clickjacking attacks, by ensuring that their content is not embedded into other sites.
1 |
|
Change the value to SAMEORIGIN
to allow your site to iframe
pages.
The X-Frame-Options header is automatically added with the value SAMEORIGIN when enabling anti-forgery
:
1 |
|
If you don’t want to add it automatically
1 |
|
X-Permitted-Cross-Domain-Policies
The X-Permitted-Cross-Domain-Policies
HTTP response header can be used to indicate whether or not an Adobe products such as Adobe Reader should be allowed to render a page. Sites can use this to avoid clickjacking attacks, by ensuring that their content is not embedded into other applications.
1 |
|
X-XSS-Protection
The HTTP X-XSS-Protection
response header is a feature that stops pages from loading when they detect reflected cross-site scripting (XSS)
attacks. Although these protections are largely unnecessary in modern browsers when sites implement a strong Content-Security-Policy
that disables the use of inline JavaScript ('unsafe-inline'
), they can still provide protections for users of older web browsers that don’t yet support CSP.
1 |
|
The value 1
means enabled
and the mode of block
will block the browser from rendering the page.
X-Content-Type-Options
MIME-type sniffing is an attack where a hacker tries to exploit missing metadata on served files. The header can be added in middleware:
1 |
|
The value of nosniff
will prevent primarily old browsers from MIME-sniffing.
Referrer-Policy
When you click a link on a website, the calling URL is automatically transferred to the linked site. Unless this is necessary, you should disable it using the Referrer-Policy
header:
1 |
|
There are a lot of possible values for this header, like same-origin
that will set the referrer as long as the user stays on the same website.
Feature-Policy
The Feature-Policy
header tells the browser which platform features your website needs. Most web apps won’t need to access the microphone or the vibrator functions available on mobile browsers. Why not be explicit about it to avoid imported scripts or framed pages to do things you don’t expect:
1 |
|
X-Powered-By
Like ASP.NET, ASP.NET Core will return the X-Powered-By
header. This happens when you host your website on IIS. This also means that you simply cannot remove the header in middleware, since this is out of hands for ASP.NET Core. web.config
to the rescue:
1 |
|
Server
Like X-Powered-By, IIS kindly identify itself in the Server
header. While hackers probably quickly find out anyway, you should still make it as hard as possible by removing the header. There’s a dedicated security feature available in web.config
to do that:
1 |
|
To disable the Server
header from Kestrel
, you need to set AddServerHeader
to false
.
1 |
|
Content-Security-Policy (CSP)
The Content-Security-Policy
response header allows web site administrators to control resources the user agent is allowed to load for a given page. With a few exceptions, policies mostly involve specifying server origins and script endpoints. This helps guard against cross-site scripting attacks (XSS).
1 |
|
Expect-CT
The Expect-CT header lets sites opt in to reporting and/or enforcement of Certificate Transparency requirements, to prevent the use of misissued certificates for that site from going unnoticed.
1 |
|
All-In-One as a middleware
1 |
|
How to use the middleware?
You should call athe above custom middleware as the following:
1 |
|
Validation
You can check you have correctly set the security headers by using the following service: https://securityheaders.com
Ready to use libraries
NWebsec
NWebsec consists of several security libraries for ASP.NET applications.
Site
: https://github.com/NWebsec/NWebsec
Docs
: https://docs.nwebsec.com/en/latest/
NetEscapades.AspNetCore.SecurityHeaders
Small package to allow adding security headers to ASP.NET Core websites
Site
: https://github.com/andrewlock/NetEscapades.AspNetCore.SecurityHeaders
Reference(s)
Most of the information in this article has gathered from various references.
- https://blog.elmah.io/the-asp-net-core-security-headers-guide/
- https://www.meziantou.net/security-headers-in-asp-net-core.htm
- https://improveandrepeat.com/2019/05/how-to-improve-the-security-headers-for-your-asp-net-application/
- https://www.c-sharpcorner.com/article/asp-net-core-security-headers/
- https://andrewlock.net/adding-default-security-headers-in-asp-net-core/
- https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl
- https://joonasw.net/view/hsts-in-aspnet-core
- https://joonasw.net/view/enforcing-https-in-aspnet-core