Security Headers
Security Headers provides a unified configuration for protecting your Express applications from common web vulnerabilities like Cross-Site Scripting (XSS), Clickjacking, and Cross-Origin Resource Sharing (CORS) issues.
Installation Guide
Install the component using the servercn CLI:
npx servercn-cli add security-headerThreat Model Coverage
Below is a precise breakdown of each header, the associated attack vector, and the mitigation strategy.
1. X-Content-Type-Options: nosniff
Attack:
Browsers attempt MIME sniffing and may execute JavaScript disguised as another file type (e.g., malicious JS embedded in an image upload).
Mitigation:
Forces browsers to strictly honor the declared Content-Type header.
2. X-Frame-Options: DENY (or SAMEORIGIN)
Attack (Clickjacking):
An attacker embeds your application inside a malicious iframe and overlays invisible UI elements to trick users into clicking privileged actions.
Mitigation:
Prevents your application from being framed.
3. X-XSS-Protection: 0
History & Risk:
Legacy browsers implemented a reflective XSS filter that was unreliable and, in some cases, exploitable.
Modern Best Practice:
Disable it (0) and rely on a properly configured Content-Security-Policy (CSP).
4. Strict-Transport-Security (HSTS)
Attack (Protocol Downgrade / MITM):
An attacker forces HTTPS traffic to downgrade to HTTP.
Mitigation:
Instructs the browser to only use HTTPS for your domain for a defined duration.
5. Content-Security-Policy (CSP)
Attack (XSS Injection):
An attacker injects malicious <script> tags into your pages.
Mitigation:
Whitelists allowed script, style, image, and connection sources.
6. Referrer-Policy
Data Leakage Risk:
The Referer header may expose full URLs including tokens, internal paths, or identifiers when navigating away.
Mitigation:
Controls how much URL information is shared with external origins.
7. Permissions-Policy
Attack Surface Expansion:
Malicious embedded contexts (iframes) attempt to access camera, microphone, geolocation, etc.
Mitigation:
Explicitly disables browser capabilities your application does not require.
8. Remove X-Powered-By
Information Disclosure:
X-Powered-By: Express reveals your framework stack.
Mitigation:
Disable to reduce reconnaissance surface area.
Basic Implementation
1. Middleware Configuration
The component provides a configureSecurityHeaders function that you should call during your Express app initialization.
Minimal
Advanced
2. Usage in App
Import and use the configuration in your main app.ts file.
Production Recommendations
1. Restrict CORS Origin
Never use * in production for authenticated APIs.
2. Customize CSP for External Providers
If using CDNs or third-party services:
3. Enable HSTS Only After HTTPS Is Stable
Once deployed over HTTPS permanently, consider submitting to the HSTS preload list.
Verification
You can verify your security headers using online tools like SecurityHeaders.com.