SKILL.md--- name: security-express description: |- Review Express.js security audit patterns for middleware and routes. Use for auditing Helmet.js, CORS, body-parser limits, and auth middleware. Use proactively when reviewing Express.js apps. Examples: - user: "Secure my Express app" → add Helmet.js and disable x-powered-by - user: "Check Express CORS config" → verify origin allowlists and credentials - user: "Review Express auth middleware" → check route order and coverage - user: "Scan for Express path traversal" → verify path normalization and validation - user: "Audit Express session config" → check secure, httpOnly, and sameSite flags --- <overview> Security audit patterns for Express.js applications covering essential security middleware, CORS configuration, auth patterns, and common vulnerabilities. </overview> <rules> ## Essential Security Middleware ### Helmet.js (Security Headers) ```javascript // ❌ Missing security headers const app = express(); // ✓ Use Helmet const helmet = require('helmet'); app.use(helmet()); ``` **Check if Helmet is installed and used.** It sets: - Content-Security-Policy - X-Content-Type-Options: nosniff - X-Frame-Options: DENY - Strict-Transport-Security - And more... ### Disable X-Powered-By ```javascript // ❌ Default (header reveals framework) const app = express(); // ✓ Disable fingerprinting app.disable('x-powered-by'); // or: app.set('x-powered-by', false); ``` ### CORS Configuration ```javascript // ❌ CRITICAL: Allow all origins app.use(cors()); app.use(cors({ origin: '*' })); // ❌ HIGH: Reflect origin with credentials app.use(cors({ origin: true, // Reflects any origin! credentials: true })); // ✓ Explicit allowlist app.use(cors({ origin: ['https://app.example.com', 'https://admin.example.com'], credentials: true, })); // ✓ Function for dynamic validation app.use(cors({ origin: (origin, callback) => { const allowed = ['https://app.example.com']; if (!origin || allowed.includes(origin)) { callback(null, true); } else { callback(new Error('Not allowed by CORS')); } }, credentials: true, })); ``` ## Body Parser Limits ```javascript // ❌ No limit (DoS risk) app.use(express.json()); // ✓ Set reasonable limits app.use(express.json({ limit: '100kb' })); app.use(express.urlencoded({ extended: true, limit: '100kb' })); ``` </rules> <vulnerabilities> ## Auth Middleware Patterns ### Missing Auth on Routes ```javascript // ❌ No auth on admin routes app.get('/api/admin/users', async (req, res) => { res.json(await User.find()); }); // ✓ Auth middleware applied app.get('/api/admin/users', requireAuth, requireAdmin, async (req, res) => { res.json(await User.find()); }); ``` ### Middleware Order Matters ```javascript // ❌ Wrong order - static files before auth app.use(express.static('uploads')); // Exposed! app.use(requireAuth); // ✓ Auth before protected static files app.use('/public', express.static('public')); // Intentionally p