Modern web browsers offer a lot of security features aimed at protecting your users from a wide variety of threats such as malware installed on their device, hackers listening to their network traffic and malicious phishing sites pretending to be something that they’re not. Unfortunately quite a few of these features are disabled by default in order to ensure backward compatibility with older websites that rely on insecure behavior to function properly. What a lot of developers don’t know is that many of these settings are actually incredibly simple to enable, and you can protect your app from a wide variety of attacks in less than an afternoon. Here is a list of the most important security HTTP headers and why you should care about them.
1. Frame Options
Setting an X-Frame-Options header on your website is a way to protect from someone creating a wrapper around your site doing whatever they want and displaying your page in an iframe. This allows attackers to force your users to click on some part of your website, while hidden in an iframe (these are known as clickjacking attacks). You can either choose to completely block rendering your site inside a frame by setting this header to DENY, allow it to be rendered by other pages on the same server with SAMEORIGIN or, you can specify a list of whitelisted domains with ALLOW-FROM.
2. XSS Auditor
Cross-site scripting (XSS) is one of the most common and dangerous type attacks on the web, as it is often used to inject malicious code into your app to extract data about a logged in user, or take advantage of their user privileges to perform actions not available by default. Setting the X-XSS-Protection header allows modern browsers to block attackers from Reflected XSS attacks.
3. Content Type Options
4. HTTP Strict Transport Security
Strict Transport Security tells browsers they should refuse to connect to this website as HTTP, which prevents connections from insecure locations to be downgraded from HTTPS to HTTP by an attacker. If the initial connection to a website is made over HTTP and redirected to HTTPS, the HSTS header will force further connections initiated by the user to only go through HTTPS.
5. Public Key Pinning Extension for HTTP
Public Key Pinning Extension for HTTP is another header that is only useful for sites running on HTTPS. While this header won’t do anything the first time, a user loads your site it will register the certificate your site is using, preventing your user’s browser from connecting to a malicious server with a different certificate that is pretending to be your website. This can be especially useful to protect your users against attackers who can create certificates for any domain (e.g. from abusing a trusted Certificate Authority or finding an HTTPS flaw).
6. Content Security Policy
Most of the other headers introduced here are quite simple, although I’ll have to admit that this one does take a little bit of effort to implement. A Content Security Policy (CSP) header lists all authorized domains and resources your app is allowed to use. Thus if a user loads a page where an attacker has injected a malicious resource, the browser will load your page, but prevent the attacker’s resource from loading. One side effect of using this header is that if you add new assets to your application, you will need to update your Content Security Policy accordingly.
7. Cookie Protection
If you’re using cookies to store session or authentication information to keep users logged into your site, there are a couple of tricks to improve your security. There are two directives you should add which protect attackers from reading cookies in different places:
- 2) The Secure attribute will only allow the cookies to be transferred over an HTTPS connection and not over HTTP, so an attacker with access to your network won’t be able to read unencrypted cookies.