A comprehensive security middleware for Elysia.js applications that helps secure your apps by setting various HTTP headers.
- 🛡️ Content Security Policy (CSP)
- 🔒 X-Frame-Options protection
- 🚫 XSS Protection
- 🌐 DNS Prefetch Control
- 📜 Referrer Policy
- 🔑 Permissions Policy
- 🔐 HTTP Strict Transport Security (HSTS)
- 🌍 Cross-Origin Resource Policy (CORP)
- 🚪 Cross-Origin Opener Policy (COOP)
- 📝 Report-To header configuration
- ✨ Custom headers support
bun add elysiajs-helmet
import { Elysia } from 'elysia'
import { elysiaHelmet } from 'elysiajs-helmet'
const app = new Elysia()
.use(elysiaHelmet({}))
.get('/', () => 'Hello, Secure World!')
.listen(3000)
Note: Production mode is automatically enabled when
NODE_ENV
is set to'production'
. In production mode, additional security measures are enforced.
import { Elysia } from 'elysia'
import { elysiaHelmet } from 'elysiajs-helmet'
const app = new Elysia()
.use(elysiaHelmet({
csp: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:", "https:"],
useNonce: true
},
hsts: {
maxAge: 31536000,
includeSubDomains: true,
preload: true
},
frameOptions: 'DENY',
referrerPolicy: 'strict-origin-when-cross-origin',
permissionsPolicy: {
camera: ["'none'"],
microphone: ["'none'"]
}
}))
.listen(3000)
interface CSPConfig {
defaultSrc?: string[];
scriptSrc?: string[];
styleSrc?: string[];
imgSrc?: string[];
fontSrc?: string[];
connectSrc?: string[];
frameSrc?: string[];
objectSrc?: string[];
baseUri?: string[];
reportUri?: string;
useNonce?: boolean;
reportOnly?: boolean;
}
interface HSTSConfig {
maxAge?: number;
includeSubDomains?: boolean;
preload?: boolean;
}
interface ReportToConfig {
group: string;
maxAge: number;
endpoints: Array<{
url: string;
priority?: number;
weight?: number;
}>;
includeSubdomains?: boolean;
}
The middleware comes with secure defaults:
- CSP with
'self'
as default source - Frame options set to
DENY
- XSS Protection enabled
- DNS Prefetch Control disabled
- Strict Referrer Policy
- And more secure defaults
You can override any of these defaults by passing your own configuration.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT Abdelrahman Shaheen