http-security-headers
Type-safe, framework-agnostic HTTP security headers for Rust with Tower and Actix-Web integration.
Features
- 🔒 Type-safe configuration: Compile-time guarantees for header values
- 🏗️ Builder pattern: Ergonomic, fluent API for configuration
- 📦 Preset configurations: Strict, Balanced, and Relaxed security levels
- 🔌 Framework integrations: Tower middleware (Axum, Tonic, etc.) and Actix-Web support
- ⚡ Minimal core deps: Core crate only depends on
thiserror; middleware feature adds Tower + pin-project-lite - 📝 Well-documented: Comprehensive docs with examples
Security Headers Supported
| Header | Description |
|---|---|
| Content-Security-Policy (CSP) | Prevents XSS and code injection attacks |
| Strict-Transport-Security (HSTS) | Forces HTTPS connections |
| X-Frame-Options | Prevents clickjacking attacks |
| X-Content-Type-Options | Prevents MIME type sniffing |
| Referrer-Policy | Controls referrer information |
| Cross-Origin-Opener-Policy (COOP) | Isolates browsing contexts |
| Cross-Origin-Embedder-Policy (COEP) | Controls cross-origin resource loading |
| Cross-Origin-Resource-Policy (CORP) | Controls resource sharing |
Installation
Add to your Cargo.toml:
[]
= "0.1"
# For middleware support
= { = "0.1", = ["middleware"] }
# For Actix-Web integration
= { = "0.1", = ["actix"] }
Quick Start
Using Presets
use Preset;
// Use a preset configuration
let headers = Strict.build;
Custom Configuration
use ;
use Duration;
let csp = new
.default_src
.script_src
.style_src;
let headers = builder
.content_security_policy
.strict_transport_security
.x_frame_options_deny
.x_content_type_options_nosniff
.referrer_policy_no_referrer
.build
.unwrap;
With Axum
use ;
use ;
use Arc;
let headers = new;
let app = new
.route
.layer;
With Actix-Web
use ;
use ;
use Arc;
async
Presets
Strict
Recommended for applications that can enforce strict security policies.
let headers = Strict.build;
Includes:
- CSP:
default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none' - HSTS: 1 year, includeSubDomains
- X-Frame-Options: DENY
- X-Content-Type-Options: nosniff
- Referrer-Policy: no-referrer
- COOP: same-origin
- COEP: require-corp
- CORP: same-origin
Balanced
Provides good security while maintaining compatibility.
let headers = Balanced.build;
Includes:
- CSP:
default-src 'self'; script-src 'self' 'unsafe-inline'; object-src 'none' - HSTS: 1 year, includeSubDomains
- X-Frame-Options: SAMEORIGIN
- X-Content-Type-Options: nosniff
- Referrer-Policy: strict-origin-when-cross-origin
- COOP: same-origin-allow-popups
Relaxed
Baseline security with minimal restrictions.
let headers = Relaxed.build;
Includes:
- HSTS: 6 months
- X-Frame-Options: SAMEORIGIN
- X-Content-Type-Options: nosniff
- Referrer-Policy: strict-origin-when-cross-origin
Examples
Check out the examples directory:
- axum_basic.rs: Basic Axum integration with preset
- axum_custom.rs: Custom security headers configuration
- actix_basic.rs: Simple Actix-Web integration
Run examples:
Feature Flags
| Feature | Description |
|---|---|
middleware |
Enables Tower middleware support |
axum |
Enables Axum-specific helpers (requires middleware) |
actix |
Enables Actix-Web middleware integration (includes actix-web) |
observability |
Enables tracing support |
metrics |
Enables metrics collection |
validation |
Enables CSP/Permissions-Policy validation |
Documentation
Full documentation is available on docs.rs.
Comparison with Other Crates
| Feature | http-security-headers | secure-headers | tower-http |
|---|---|---|---|
| Type-safe configuration | ✅ | ❌ | Partial |
| Builder pattern | ✅ | ❌ | ❌ |
| Preset configurations | ✅ | ❌ | ❌ |
| Framework-agnostic | ✅ | ❌ | ✅ |
| CSP builder | ✅ | ❌ | ❌ |
| Full header support | ✅ | Partial | Partial |
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
Inspired by: