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,4 audit, and harden HTTP security headers. Use when working with security headers.5domain: development6author: oyi777license: Apache-2.08subdomain: software-development9tags:10- coding11- headers12- security13- software-engineering14- testing15version: 1.0.016category: development17metadata:18 source: https://skillmd.com/skills/oyi77/security-headers19---20212223## Overview2425HTTP 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.2627## Capabilities2829- Configure Content Security Policy (CSP) directives30- Set up Cross-Origin Resource Sharing (CORS) policies31- Enable HTTP Strict Transport Security (HSTS)32- Audit existing headers with security scanners33- Implement Permissions-Policy for feature restriction34- Generate headers for Express, Nginx, Apache, Cloudflare3536## When to Use37**Trigger phrases:**38- "security headers"39- "Web security headers — CSP, CORS, HSTS, X-Frame-Options"404142- Hardening a web application before production43- Fixing CSP or CORS issues in security audits44- Configuring headers for API servers45- Meeting compliance requirements (PCI-DSS, SOC2)46- Preventing XSS, clickjacking, or data leakage4748## Pseudo Content4950- Configure audit, configure, cors, frame, harden settings before first use515253### Content Security Policy (CSP)54```javascript55// Express.js56app.use((req, res, next) => {57 res.setHeader('Content-Security-Policy', [58 "default-src 'self'",59 "script-src 'self' 'nonce-{random}' https://cdn.jsdelivr.net",60 "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",61 "img-src 'self' data: https:",62 "font-src 'self' https://fonts.gstatic.com",63 "connect-src 'self' https://api.example.com",64 "frame-ancestors 'none'",65 "base-uri 'self'",66 "form-action 'self'",67 ].join('; '));68 next();69});70```7172### CORS Configuration73```javascript74// Express.js with cors middleware75const cors = require('cors');7677app.use(cors({78 origin: ['https://app.example.com', 'https://admin.example.com'],79 methods: ['GET', 'POST', 'PUT', 'DELETE'],80 allowedHeaders: ['Content-Type', 'Authorization'],81 credentials: true,82 maxAge: 86400, // Preflight cache 24h83}));8485// Nginx86# add_header Access-Control-Allow-Origin "https://app.example.com";87# add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE";88# add_header Access-Control-Allow-Headers "Content-Type, Authorization";89# add_header Access-Control-Allow-Credentials "true";90# add_header Access-Control-Max-Age "86400";91```9293### HSTS (HTTP Strict Transport Security)94```nginx95# Nginx96add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;9798# Express.js99app.use((req, res, next) => {100 res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');101 next();102});103```104105### Full Security Headers Stack106```javascript107// Express.js middleware108app.use((req, res, next) => {109 // Prevent MIME sniffing110 res.setHeader('X-Content-Type-Options', 'nosniff');111112 // Clickjacking protection113 res.setHeader('X-Frame-Options', 'DENY');114115 // XSS protection (legacy)116 res.setHeader('X-XSS-Protection', '1; mode=block');117118 // Referrer policy119 res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');120121 // Permissions policy122 res.setHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');123124 // Remove server header125 res.removeHeader('X-Powered-By');126127 next();128});129```130131### Audit with CLI132```bash133# Security Headers check134curl -sI https://example.com | grep -iE "strict-transport|content-security|x-frame|x-content-type|referrer-policy|permissions-policy"135136# Using securityheaders.com API137curl "https://securityheaders.com/?q=https://example.com&followRedirects=on"138139# Mozilla Observatory140curl "https://http-observatory.security.mozilla.org/api/v1/analyze?host=example.com"141142# Using npx143npx security-headers check https://example.com144```145146### Nginx Full Config147```nginx148server {149 listen 443 ssl http2;150151 # HSTS152 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;153154 # CSP155 add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline';" always;156157 # Anti-clickjacking158 add_header X-Frame-Options "DENY" always;159160 # MIME sniffing161 add_header X-Content-Type-Options "nosniff" always;162163 # Referrer164 add_header Referrer-Policy "strict-origin-when-cross-origin" always;165166 # Permissions167 add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;168169 # Remove server version170 server_tokens off;171}172```173174## Common Patterns175176| Header | Protection | Value |177|--------|-----------|-------|178| `Content-Security-Policy` | XSS, injection | `default-src 'self'` |179| `Strict-Transport-Security` | SSL stripping | `max-age=31536000; includeSubDomains` |180| `X-Frame-Options` | Clickjacking | `DENY` or `SAMEORIGIN` |181| `X-Content-Type-Options` | MIME sniffing | `nosniff` |182| `Referrer-Policy` | Data leakage | `strict-origin-when-cross-origin` |183| `Permissions-Policy` | Feature abuse | `camera=(), microphone=()` |184185## How to Use1861871. Understand the requirement and existing codebase patterns1882. Design the solution with error handling and testability in mind1893. Implement incrementally with tests for each change1904. Verify against expected outcomes (manual and automated)1915. Document usage, edge cases, and integration points1926. Review with team before merging to shared branches193194## When NOT to Use195196- Task is about deployment, not development (use deploy skills)197- Task is about code review, not writing (use review skills)198- You need to understand existing code first (use research skills)199- Task is about testing only (use test skills)200- Requirements are unclear (clarify first)201- Task is trivially simple (single line fix)202203204## Red Flags205206- **Skipping tests to ship faster**: Untested code breaks in production when you least expect it207- **No error handling in production code**: Unhandled errors crash services and lose user data208- **Hardcoded configuration values**: Hardcoded values prevent environment switching and leak secrets209- **Ignoring security implications**: Missing input validation, auth bypasses, and injection vulnerabilities210- **Over-engineering simple solutions**: Premature abstraction adds complexity without proportional benefit211212## Verification213214- [ ] Skill output matches expected behavior215216## Process2172181. Analyze the task requirements2192. Apply domain expertise2203. Verify output quality221222## Anti-Rationalization Table223224| Rationalization | Reality |225|---|---|226| "Tests slow me down" | Bugs slow you down 10x more. Tests are speed, not overhead. |227| "I will refactor later" | Technical debt compounds. Refactor as you go. |228| "It works on my machine" | If it is not in CI, it does not work. Ship proof, not claims. |