DEV Community

Mark Tylerson
Mark Tylerson

Posted on

Best multi-framework Node.js XSS firewall

So, I was browsing npm the other day, searching for a way to enhance the XSS protection of a project I’m working on. I’m familiar with the OWASP Top 10, and XSS is one of those vulnerabilities that persistently arises, regardless of our efforts to mitigate it. Despite thorough input sanitization, it seems like there’s always a new vector waiting to exploit our app.

Then, I came across something that caught my attention: SNAF (Sarp’s Node App Firewall). This lightweight and highly configurable XSS scanner works across multiple Node.js frameworks. It has been a game-changer for me, and I thought I’d share it with anyone seeking a robust XSS filter to integrate into their stack.

Why XSS Protection Matters

If you’re working on a Node.js app, you’re likely already aware of XSS (Cross-Site Scripting) and its significance as one of the top threats listed on the OWASP Top 10. Let’s recap:

  • XSS occurs when an attacker injects malicious scripts into web pages that are executed on other users’ browsers.
  • It can steal session cookies, hijack accounts, and enable attackers to perform various actions, including stealing sensitive data and spreading malware.

No one enjoys being hacked, especially when it’s due to something as preventable as XSS. When I sought a simple yet effective method to block these attacks, SNAF caught my attention.

What Is SNAF?

SNAF is a Node.js firewall specifically designed for XSS protection. It’s a lightweight package that doesn’t add any unnecessary complexity to your app (zero dependencies, zero hassle). Moreover, it’s framework-agnostic, meaning it works with popular Node.js frameworks such as Express, Fastify, Koa, Hapi, and even Next.js. Simply plug it into your app as middleware, and it’ll begin its work.

Key Features of SNAF

SNAF provides out-of-the-box XSS protection for your Node.js app. It actively blocks or sanitizes malicious XSS payloads, allowing you to configure it to block, sanitize, or simply report suspicious content.

SNAF is framework agnostic and works with various Node.js frameworks, including Express, Next.js, Fastify, Koa, and Hapi. Simply plug it in, and it’s ready to go.

SNAF is designed to have minimal impact on your app’s performance, making it ideal for production environments. You can customize the protection level to suit your app’s needs, from blocking inline event handlers to sanitizing form inputs.

If you’re using TypeScript, you’ll benefit from extra type safety and autocompletion. However, SNAF also works seamlessly with JavaScript.

How Does It Work?

SNAF acts as a middleware that you insert into your Node.js app. It scans incoming requests for potential XSS payloads in various locations, such as URL parameters, form inputs, and user-generated content. Based on your configuration, SNAF either blocks, sanitizes, or reports any detected payloads.

The integration process is incredibly simple. Once you set it up, you’ll enjoy instant protection with minimal effort.

How to Get Started

I’ve personally used SNAF in Express.js, and the integration process couldn’t have been easier. Here’s a concise example:

const express = require("express");
const { createSnaf } = require("snaf");

const app = express();
const snaf = createSnaf({
  modules: {
    xss: {
      enabled: true,
      blockMode: "sanitize",
  },
});

app.use(snaf.express());

app.get("/", (req, res) => {
  res.send("Hello, world!");
});

app.listen(3005);
Enter fullscreen mode Exit fullscreen mode

This code snippet sets up a Node.js application and includes the Snaf Express middleware. The Snaf Express middleware provides XSS protection, which helps prevent cross-site scripting attacks.

Why I’m a Fan

One of the reasons I’m a fan of Snaf is its ease of integration and configuration. I’ve used many security tools in the past, but most of them either slow down my application or are extremely complicated to set up. Snaf, on the other hand, is neither of those things. It provides fast, lightweight, and reliable XSS protection with minimal effort.

Wrapping Up

If you’re working on a Node.js application and need XSS protection, I highly recommend giving Snaf a try. It’s a well-thought-out tool that solves an important problem without introducing a lot of complexity. In the world of security, simplicity is a huge win.

You can find Snaf on npm or GitHub. Please let me know what you think!

npm
GitHub

Top comments (0)