@fastify/helmet

Security headers plugin for Fastify — sets HTTP headers that protect against common web vulnerabilities like XSS, clickjacking, and MIME sniffing.

@fastify/helmet

Fastify

Security headers plugin for Fastify — sets HTTP headers that protect against common web vulnerabilities like XSS, clickjacking, and MIME sniffing.

Fit

Bundle (gzip)

4.4 kB

15.0 kB raw

Install size includes transitive dependencies. Bundle size is gzipped browser payload.

Freshness

The Problem

By default, Fastify (and Node.js HTTP servers generally) set no security-related response headers. This exposes apps to clickjacking, MIME sniffing, and XSS attacks that a few HTTP headers can prevent.

What It Does

@fastify/helmet is the Fastify wrapper around the helmet library. It sets Content-Security-Policy, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, and other protective headers by default. Each header is configurable or can be disabled individually.

Installation

bash
npm install @fastify/helmet

Usage Example

typescript
import fastify from 'fastify';
import helmet from '@fastify/helmet';

const app = fastify();

await app.register(helmet, {
  contentSecurityPolicy: {
    directives: {
      defaultSrc: ["'self'"],
      scriptSrc: ["'self'", "'unsafe-inline'", 'cdn.example.com'],
      imgSrc: ["'self'", 'data:', 'https:'],
    },
  },
});

Related packages