Overview
HTTP security headers protect web applications from XSS, clickjacking, MIME sniffing, and other attacks. This skill covers configuring Content Security Policy (CSP), CORS, HSTS, and other headers, plus auditing tools to verify proper setup.
Capabilities
- Configure Content Security Policy (CSP) directives
- Set up Cross-Origin Resource Sharing (CORS) policies
- Enable HTTP Strict Transport Security (HSTS)
- Audit existing headers with security scanners
- Implement Permissions-Policy for feature restriction
- Generate headers for Express, Nginx, Apache, Cloudflare
When to Use
Trigger phrases:
"security headers"
"Web security headers — CSP, CORS, HSTS, X-Frame-Options"
Hardening a web application before production
Fixing CSP or CORS issues in security audits
Configuring headers for API servers
Meeting compliance requirements (PCI-DSS, SOC2)
Preventing XSS, clickjacking, or data leakage
Pseudo Content
- Configure audit, configure, cors, frame, harden settings before first use
Content Security Policy (CSP)
// Express.js
app.use((req, res, next) => {
res.setHeader('Content-Security-Policy', [
"default-src 'self'",
"script-src 'self' 'nonce-{random}' https://cdn.jsdelivr.net",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
"img-src 'self' data: https:",
"font-src 'self' https://fonts.gstatic.com",
"connect-src 'self' https://api.example.com",
"frame-ancestors 'none'",
"base-uri 'self'",
"form-action 'self'",
].join('; '));
next();
});
CORS Configuration
// Express.js with cors middleware
const cors = require('cors');
app.use(cors({
origin: ['https://app.example.com', 'https://admin.example.com'],
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
maxAge: 86400, // Preflight cache 24h
}));
// Nginx
# add_header Access-Control-Allow-Origin "https://app.example.com";
# add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE";
# add_header Access-Control-Allow-Headers "Content-Type, Authorization";
# add_header Access-Control-Allow-Credentials "true";
# add_header Access-Control-Max-Age "86400";
HSTS (HTTP Strict Transport Security)
# Nginx
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# Express.js
app.use((req, res, next) => {
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');
next();
});
Full Security Headers Stack
// Express.js middleware
app.use((req, res, next) => {
// Prevent MIME sniffing
res.setHeader('X-Content-Type-Options', 'nosniff');
// Clickjacking protection
res.setHeader('X-Frame-Options', 'DENY');
// XSS protection (legacy)
res.setHeader('X-XSS-Protection', '1; mode=block');
// Referrer policy
res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
// Permissions policy
res.setHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
// Remove server header
res.removeHeader('X-Powered-By');
next();
});
Audit with CLI
# Security Headers check
curl -sI https://example.com | grep -iE "strict-transport|content-security|x-frame|x-content-type|referrer-policy|permissions-policy"
# Using securityheaders.com API
curl "https://securityheaders.com/?q=https://example.com&followRedirects=on"
# Mozilla Observatory
curl "https://http-observatory.security.mozilla.org/api/v1/analyze?host=example.com"
# Using npx
npx security-headers check https://example.com
Nginx Full Config
server {
listen 443 ssl http2;
# HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# CSP
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline';" always;
# Anti-clickjacking
add_header X-Frame-Options "DENY" always;
# MIME sniffing
add_header X-Content-Type-Options "nosniff" always;
# Referrer
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# Permissions
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
# Remove server version
server_tokens off;
}
Common Patterns
| Header |
Protection |
Value |
Content-Security-Policy |
XSS, injection |
default-src 'self' |
Strict-Transport-Security |
SSL stripping |
max-age=31536000; includeSubDomains |
X-Frame-Options |
Clickjacking |
DENY or SAMEORIGIN |
X-Content-Type-Options |
MIME sniffing |
nosniff |
Referrer-Policy |
Data leakage |
strict-origin-when-cross-origin |
Permissions-Policy |
Feature abuse |
camera=(), microphone=() |
How to Use
- Understand the requirement and existing codebase patterns
- Design the solution with error handling and testability in mind
- Implement incrementally with tests for each change
- Verify against expected outcomes (manual and automated)
- Document usage, edge cases, and integration points
- Review with team before merging to shared branches
When NOT to Use
- Task is about deployment, not development (use deploy skills)
- Task is about code review, not writing (use review skills)
- You need to understand existing code first (use research skills)
- Task is about testing only (use test skills)
- Requirements are unclear (clarify first)
- Task is trivially simple (single line fix)
Red Flags
- Skipping tests to ship faster: Untested code breaks in production when you least expect it
- No error handling in production code: Unhandled errors crash services and lose user data
- Hardcoded configuration values: Hardcoded values prevent environment switching and leak secrets
- Ignoring security implications: Missing input validation, auth bypasses, and injection vulnerabilities
- Over-engineering simple solutions: Premature abstraction adds complexity without proportional benefit
Verification
Process
- Analyze the task requirements
- Apply domain expertise
- Verify output quality
Anti-Rationalization Table
| Rationalization |
Reality |
| "Tests slow me down" |
Bugs slow you down 10x more. Tests are speed, not overhead. |
| "I will refactor later" |
Technical debt compounds. Refactor as you go. |
| "It works on my machine" |
If it is not in CI, it does not work. Ship proof, not claims. |
1---2name: security-headers3description: Use when web security headers — CSP, CORS, HSTS, X-Frame-Options. Configure, audit, and harden HTTP security headers. Use when working with security headers.4license: Apache-2.05---6789## Overview1011HTTP security headers protect web applications from XSS, clickjacking, MIME sniffing, and other attacks. This skill covers configuring Content Security Policy (CSP), CORS, HSTS, and other headers, plus auditing tools to verify proper setup.1213## Capabilities1415- Configure Content Security Policy (CSP) directives16- Set up Cross-Origin Resource Sharing (CORS) policies17- Enable HTTP Strict Transport Security (HSTS)18- Audit existing headers with security scanners19- Implement Permissions-Policy for feature restriction20- Generate headers for Express, Nginx, Apache, Cloudflare2122## When to Use23**Trigger phrases:**24- "security headers"25- "Web security headers — CSP, CORS, HSTS, X-Frame-Options"262728- Hardening a web application before production29- Fixing CSP or CORS issues in security audits30- Configuring headers for API servers31- Meeting compliance requirements (PCI-DSS, SOC2)32- Preventing XSS, clickjacking, or data leakage3334## Pseudo Content3536- Configure audit, configure, cors, frame, harden settings before first use373839### Content Security Policy (CSP)40```javascript41// Express.js42app.use((req, res, next) => {43 res.setHeader('Content-Security-Policy', [44 "default-src 'self'",45 "script-src 'self' 'nonce-{random}' https://cdn.jsdelivr.net",46 "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",47 "img-src 'self' data: https:",48 "font-src 'self' https://fonts.gstatic.com",49 "connect-src 'self' https://api.example.com",50 "frame-ancestors 'none'",51 "base-uri 'self'",52 "form-action 'self'",53 ].join('; '));54 next();55});56```5758### CORS Configuration59```javascript60// Express.js with cors middleware61const cors = require('cors');6263app.use(cors({64 origin: ['https://app.example.com', 'https://admin.example.com'],65 methods: ['GET', 'POST', 'PUT', 'DELETE'],66 allowedHeaders: ['Content-Type', 'Authorization'],67 credentials: true,68 maxAge: 86400, // Preflight cache 24h69}));7071// Nginx72# add_header Access-Control-Allow-Origin "https://app.example.com";73# add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE";74# add_header Access-Control-Allow-Headers "Content-Type, Authorization";75# add_header Access-Control-Allow-Credentials "true";76# add_header Access-Control-Max-Age "86400";77```7879### HSTS (HTTP Strict Transport Security)80```nginx81# Nginx82add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;8384# Express.js85app.use((req, res, next) => {86 res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');87 next();88});89```9091### Full Security Headers Stack92```javascript93// Express.js middleware94app.use((req, res, next) => {95 // Prevent MIME sniffing96 res.setHeader('X-Content-Type-Options', 'nosniff');9798 // Clickjacking protection99 res.setHeader('X-Frame-Options', 'DENY');100101 // XSS protection (legacy)102 res.setHeader('X-XSS-Protection', '1; mode=block');103104 // Referrer policy105 res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');106107 // Permissions policy108 res.setHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');109110 // Remove server header111 res.removeHeader('X-Powered-By');112113 next();114});115```116117### Audit with CLI118```bash119# Security Headers check120curl -sI https://example.com | grep -iE "strict-transport|content-security|x-frame|x-content-type|referrer-policy|permissions-policy"121122# Using securityheaders.com API123curl "https://securityheaders.com/?q=https://example.com&followRedirects=on"124125# Mozilla Observatory126curl "https://http-observatory.security.mozilla.org/api/v1/analyze?host=example.com"127128# Using npx129npx security-headers check https://example.com130```131132### Nginx Full Config133```nginx134server {135 listen 443 ssl http2;136137 # HSTS138 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;139140 # CSP141 add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline';" always;142143 # Anti-clickjacking144 add_header X-Frame-Options "DENY" always;145146 # MIME sniffing147 add_header X-Content-Type-Options "nosniff" always;148149 # Referrer150 add_header Referrer-Policy "strict-origin-when-cross-origin" always;151152 # Permissions153 add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;154155 # Remove server version156 server_tokens off;157}158```159160## Common Patterns161162| Header | Protection | Value |163|--------|-----------|-------|164| `Content-Security-Policy` | XSS, injection | `default-src 'self'` |165| `Strict-Transport-Security` | SSL stripping | `max-age=31536000; includeSubDomains` |166| `X-Frame-Options` | Clickjacking | `DENY` or `SAMEORIGIN` |167| `X-Content-Type-Options` | MIME sniffing | `nosniff` |168| `Referrer-Policy` | Data leakage | `strict-origin-when-cross-origin` |169| `Permissions-Policy` | Feature abuse | `camera=(), microphone=()` |170171## How to Use1721731. Understand the requirement and existing codebase patterns1742. Design the solution with error handling and testability in mind1753. Implement incrementally with tests for each change1764. Verify against expected outcomes (manual and automated)1775. Document usage, edge cases, and integration points1786. Review with team before merging to shared branches179180## When NOT to Use181182- Task is about deployment, not development (use deploy skills)183- Task is about code review, not writing (use review skills)184- You need to understand existing code first (use research skills)185- Task is about testing only (use test skills)186- Requirements are unclear (clarify first)187- Task is trivially simple (single line fix)188189190## Red Flags191192- **Skipping tests to ship faster**: Untested code breaks in production when you least expect it193- **No error handling in production code**: Unhandled errors crash services and lose user data194- **Hardcoded configuration values**: Hardcoded values prevent environment switching and leak secrets195- **Ignoring security implications**: Missing input validation, auth bypasses, and injection vulnerabilities196- **Over-engineering simple solutions**: Premature abstraction adds complexity without proportional benefit197198## Verification199200- [ ] Skill output matches expected behavior201202## Process2032041. Analyze the task requirements2052. Apply domain expertise2063. Verify output quality207208## Anti-Rationalization Table209210| Rationalization | Reality |211|---|---|212| "Tests slow me down" | Bugs slow you down 10x more. Tests are speed, not overhead. |213| "I will refactor later" | Technical debt compounds. Refactor as you go. |214| "It works on my machine" | If it is not in CI, it does not work. Ship proof, not claims. |