Is Your Web App Ready to Survive the Zombie Apocalypse of Online Security? Discover Helmet.js!

Making Your Express.js App Zombie-Proof with Helmet.js: Enhancing Security by Configuring HTTP Headers Efficiently

Is Your Web App Ready to Survive the Zombie Apocalypse of Online Security? Discover Helmet.js!

When you’re working on web applications, security isn’t something you can just scrape over. It’s like trying to survive a zombie apocalypse with a butter knife—bad move. One of the golden tools to amp up your Express.js app’s security is Helmet.js. This nifty little helper is perfect for securing HTTP headers, and today, we’re going to dive deep into using Helmet.js to enable the Referrer-Policy header. This header is a game-changer because it controls the info sent in the Referer header, which is a pretty big deal for your privacy and security.

So, picture this: the Referer header is like a gossiping neighbor, spilling all the tea about where you’ve been online. Sometimes, this can reveal sensitive information—you definitely don’t want everyone knowing your business. That’s where the Referrer-Policy comes into play; it’s like a bouncer at a club, deciding who gets in and what info gets out. By configuring this header, you can manage what the Referer header shares, which beefs up user privacy and security.

First things first—to use Helmet.js and set this control in motion, you’ve gotta install it. Lucky for us, it’s super simple with npm. Just run:

npm install helmet --save

After you’ve got it in the bag, include Helmet.js in your Express app like this:

const express = require('express');
const helmet = require('helmet');
const app = express();

app.use(helmet());

Now, let’s talk about configuring the Referrer-Policy with Helmet.js. By default, Helmet.js sets the Referrer-Policy to no-referrer, which means zip, nada, nothing gets sent. But, if that doesn’t fit your needs, you can tweak it easily.

For instance, if you want to tell the app to send referrer info only when the request is made to the same origin, you’d set up Helmet like this:

app.use(
  helmet({
    referrerPolicy: {
      policy: 'same-origin',
    },
  })
);

There are various preset policies you can choose from, depending on how much or how little information you want to share. Here’s the rundown:

  • no-referrer: Nope, no info is sent.
  • no-referrer-when-downgrade: Nothing is sent when going from HTTPS to HTTP.
  • origin: Just the origin of the document is sent.
  • origin-when-cross-origin: Full URL for the same origin, but only the origin for different origins.
  • same-origin: Full URL sent only for the same origin.
  • strict-origin: Only the origin is sent when on the same origin, and nothing is sent for a different origin.
  • strict-origin-when-cross-origin: Full URL for same origin, only origin for different, and nothing from HTTPS to HTTP.
  • unsafe-url: Full URL is sent all the time, even when moving from HTTPS to HTTP.

Want your setup to send just the origin? Do it up like this:

app.use(
  helmet({
    referrerPolicy: {
      policy: 'origin',
    },
  })
);

And if for some reason you decide you don’t want the Referrer-Policy header at all, you can disable it by setting the policy to false:

app.use(
  helmet({
    referrerPolicy: false,
  })
);

But wait, it gets even better. Helmet.js isn’t a one-trick pony. You can combine different security headers for an all-around protective shield. Here’s a bit of magic:

app.use(
  helmet({
    referrerPolicy: {
      policy: 'same-origin',
    },
    hsts: {
      maxAge: 31536000, // That's 1 year in seconds
      includeSubDomains: true,
    },
    frameguard: {
      action: 'deny',
    },
    noSniff: true,
  })
);

What does that do? You guessed it—it sets the Referrer-Policy, enables HTTP Strict Transport Security (HSTS) for a year, denies iframe embedding with frameguard, and prevents MIME type sniffing. Way to cover all bases, right?

To make sure the Referrer-Policy header is working as it should, you can pop open your browser’s developer tools and check out the HTTP headers. Here’s the quick route in Chrome:

  1. Right-click and hit “Inspect” or just press Ctrl + Shift + I (Windows/Linux) or Cmd + Opt + I (Mac).
  2. Head over to the “Network” tab.
  3. Click on any HTTP request.
  4. Check out the “Headers” tab to see the HTTP headers, including Referrer-Policy.

Bringing it all together, enabling this Referrer-Policy header through Helmet.js is as easy as pie and ramps up the privacy and security of your Express.js applications significantly. By managing the Referer header info, you protect your users’ privacy and slam the door on potential security issues.

Don’t forget, if you want to achieve robust security, always tailor the policy to your app’s requirements and mix it up with other security headers. Helmet.js is like having your own security guard for HTTP headers, keeping your application safe from various threats and vulnerabilities.

Incorporating Helmet.js into your development process is a must-do. It ensures your HTTP headers are configured securely, which is crucial for protecting your app from attacks. With its simplicity and powerful features, Helmet.js is a can’t-miss tool for developers aiming to lock down their Express.js applications tightly.