Skip to main content

Overview

The helmet middleware sets security-related HTTP headers to protect your application from common vulnerabilities like clickjacking, XSS, and content-type sniffing. Use it when you need:
  • Protection against common web vulnerabilities
  • Content Security Policy (CSP)
  • HTTP Strict Transport Security (HSTS)
  • Compliance with security best practices

Installation

Quick Start

Configuration

Options

HSTSOptions

Examples

Default Security Headers

Sets these headers:

Content Security Policy

HTTP Strict Transport Security

Frame Options

Full Custom Configuration

Individual Header Functions

Permissions Policy

DNS Prefetch Control

Cross-Origin Policies

API Reference

Functions

Individual Header Functions

Security Headers Explained

Technical Details

Architecture

The helmet middleware is implemented as a higher-order function that wraps request handlers and injects security headers before passing control to the next handler in the chain. The middleware follows a configuration-based approach where all security headers are controlled through the Options struct.

Implementation

  • Header Injection: Headers are set conditionally based on non-empty/non-nil configuration values
  • HSTS Formatting: The formatHSTS helper function constructs the Strict-Transport-Security header value from duration and boolean flags, converting the duration to seconds and appending directives as needed
  • DNS Prefetch Control: Uses a pointer to bool to distinguish between unset (nil), enabled (true), and disabled (false) states
  • Default Configuration: The Default() function provides a preset configuration aligned with modern security best practices
  • Individual Functions: Convenience functions like XFrameOptions() and ContentSecurityPolicy() create middleware instances with single-header configurations

Header Setting Logic

The New() function checks each option field and sets the corresponding header only if:
  • String fields are non-empty
  • Boolean fields are true (or pointer fields are non-nil)
  • Struct pointer fields (like HSTSOptions) are non-nil
This approach ensures headers are only set when explicitly configured, preventing empty or unwanted headers from being sent.

Performance Considerations

  • Minimal overhead: Only string operations and header setting
  • No dynamic allocations during request processing
  • All configuration is computed once during middleware creation
  • Headers are set before calling the next handler, ensuring they’re present even if the handler fails

Best Practices

  1. Start with Default() - Then customize as needed
  2. Content Security Policy - Start strict, loosen as needed
  3. HSTS - Enable for HTTPS-only sites
  4. Test thoroughly - Security headers can break functionality
  5. Report-Only mode - Test CSP before enforcing

CSP Development Strategy

Testing

The helmet middleware includes comprehensive test coverage for all security headers and configuration options:
  • secure - HTTPS enforcement
  • csrf - CSRF protection