Skip to main content

Overview

The secure middleware enforces HTTPS connections and adds security headers. It’s a comprehensive security solution that combines SSL redirect with security header management. Use it when you need:
  • Automatic HTTP to HTTPS redirect
  • Security headers in one middleware
  • Flexible security configuration
  • Development/production toggle

Installation

Quick Start

Configuration

Options

Examples

Basic HTTPS Redirect

Production Configuration

Development Mode

Behind a Proxy

Custom SSL Host

Temporary Redirect

Frame Options

Environment-Based Configuration

Full Security Setup

API Reference

Functions

Headers Set

The middleware sets these headers based on configuration:

Technical Details

Implementation Overview

The secure middleware implements a comprehensive security layer that:
  1. HTTPS Detection: Checks for secure connections via:
    • Direct TLS connection (r.TLS != nil)
    • Proxy headers (X-Forwarded-Proto, X-Forwarded-SSL, etc.)
    • Case-insensitive header matching using strings.EqualFold
  2. SSL Redirection: When HTTP is detected and SSLRedirect is enabled:
    • Constructs HTTPS URL: https:// + host + requestURI
    • Uses SSLHost if specified, otherwise uses request host
    • Returns 301 (permanent) or 307 (temporary) redirect based on SSLTemporaryRedirect
  3. Security Header Application: Sets headers conditionally based on:
    • Connection security (HTTPS vs HTTP)
    • Configuration options
    • Development mode (bypasses all security when IsDevelopment is true)

Header Construction

Strict-Transport-Security (HSTS):
  • Only set on HTTPS connections unless ForceSTSHeader is true
  • Requires STSSeconds > 0 to be enabled
X-Frame-Options:
  • Priority: CustomFrameOptions > FrameDeny > none
  • FrameDeny sets “DENY”, CustomFrameOptions allows “SAMEORIGIN” or other values
Default Headers (when using New()):
  • X-Content-Type-Options: nosniff
  • X-Frame-Options: DENY
  • X-XSS-Protection: 1; mode=block
  • SSLRedirect: enabled

Proxy Support

The middleware checks proxy headers in order:
Common proxy headers:
  • X-Forwarded-Proto (default)
  • X-Forwarded-SSL
  • Front-End-Https

Performance Optimization

The middleware uses a custom itoa function instead of strconv.Itoa for integer-to-string conversion:
  • Avoids heap allocations
  • Uses fixed-size array [20]byte for conversion
  • Handles negative numbers and zero efficiently

Security Considerations

  1. HSTS Commitment - Once set, browsers remember for max-age duration
  2. SSL Certificate - Ensure valid certificate before enabling HSTS
  3. Subdomains - Only enable STSIncludeSubdomains if all subdomains support HTTPS
  4. Preload - Apply at hstspreload.org for browser built-in list

HSTS Timeline

Best Practices

  • Start with short HSTS duration, increase gradually
  • Test thoroughly before enabling preload
  • Use IsDevelopment flag for local development
  • Configure ProxyHeaders when behind load balancers

Testing

The secure middleware includes comprehensive test coverage for all features: