Skip to content

Helmet — Security Headers ​

helmet() sets a suite of HTTP security headers that protect against common web vulnerabilities. It's a drop-in replacement for the helmet npm package.

Coming from Express?

import { helmet } from 'bunway' replaces import helmet from 'helmet'. Options are identical.

Quick Start ​

ts
import { bunway, helmet } from 'bunway';

const app = bunway();
app.use(helmet());

Default Headers ​

Helmet sets these headers on every response:

HeaderDefault ValuePurpose
Content-Security-Policydefault-src 'self'...Prevent XSS
Cross-Origin-Embedder-Policyrequire-corpIsolate resources
Cross-Origin-Opener-Policysame-originIsolate browsing context
Cross-Origin-Resource-Policysame-originPrevent cross-origin reads
X-DNS-Prefetch-ControloffDisable DNS prefetching
X-Frame-OptionsSAMEORIGINPrevent clickjacking
Strict-Transport-Securitymax-age=15552000Force HTTPS
X-Download-OptionsnoopenIE download protection
X-Content-Type-OptionsnosniffPrevent MIME sniffing
X-Permitted-Cross-Domain-PoliciesnoneAdobe cross-domain
Referrer-Policyno-referrerControl referrer
X-XSS-Protection0Disable legacy XSS filter

Customization ​

ts
app.use(helmet({
  contentSecurityPolicy: {
    directives: {
      "default-src": ["'self'"],
      "script-src":  ["'self'", "'unsafe-inline'", "cdn.example.com"],
      "style-src":   ["'self'", "'unsafe-inline'"],
      "img-src":     ["'self'", "data:", "*.example.com"]
    }
  },
  crossOriginEmbedderPolicy: false,
  hsts: {
    maxAge: 31536000,
    includeSubDomains: true,
    preload: true
  },
  frameguard: { action: 'deny' }
}));

Options ​

ts
interface HelmetOptions {
  contentSecurityPolicy?: boolean | { directives?: Record<string, string[]>, reportOnly?: boolean };
  crossOriginEmbedderPolicy?: boolean | { policy?: string };
  crossOriginOpenerPolicy?: boolean | { policy?: string };
  crossOriginResourcePolicy?: boolean | { policy?: string };
  dnsPrefetchControl?: boolean | { allow?: boolean };
  frameguard?: boolean | { action?: 'deny' | 'sameorigin' };
  hidePoweredBy?: boolean;
  hsts?: boolean | { maxAge?: number, includeSubDomains?: boolean, preload?: boolean };
  ieNoOpen?: boolean;
  noSniff?: boolean;
  originAgentCluster?: boolean;
  permittedCrossDomainPolicies?: boolean | { permittedPolicies?: string };
  referrerPolicy?: boolean | { policy?: string | string[] };
  xssFilter?: boolean;
}

Full Security Stack ​

Use helmet() alongside bunWay's other security middleware for a complete setup:

ts
import {
  bunway,
  helmet,
  csrf,
  rateLimit,
  compression,
  cookieParser,
  session
} from 'bunway';

const app = bunway();

app.use(helmet());
app.use(compression());
app.use(rateLimit({ windowMs: 60000, max: 100 }));
app.use(cookieParser());
app.use(session({ secret: process.env.SESSION_SECRET }));
app.use(csrf({ secret: process.env.CSRF_SECRET! }));

app.get('/', (req, res) => res.json({ secure: true }));
app.listen(3000);