npx skills add ...
npx skills add davila7/claude-code-templates --skill api-security-best-practices
npx skills add davila7/claude-code-templates --skill api-security-best-practices
Implement secure API design patterns including authentication, authorization, input validation, rate limiting, and protection against common API vulnerabilities
Guide developers in building secure APIs by implementing authentication, authorization, input validation, rate limiting, and protection against common vulnerabilities. This skill covers security patterns for REST, GraphQL, and WebSocket APIs.
I'll help you implement secure authentication:
Protect against injection attacks:
Prevent abuse and DDoS attacks:
Secure sensitive data:
Verify security implementation:
Symptoms: JWT secret hardcoded or committed to Git Solution: ```javascript // ❌ Bad const JWT_SECRET = 'my-secret-key';
// ✅ Good const JWT_SECRET = process.env.JWT_SECRET; if (!JWT_SECRET) { throw new Error('JWT_SECRET environment variable is required'); }
// Generate strong secret // node -e "console.log(require('crypto').randomBytes(64).toString('hex'))" ```
Symptoms: Users can set weak passwords like "password123" Solution: ```javascript const passwordSchema = z.string() .min(12, 'Password must be at least 12 characters') .regex(/[A-Z]/, 'Must contain uppercase letter') .regex(/[a-z]/, 'Must contain lowercase letter') .regex(/[0-9]/, 'Must contain number') .regex(/[^A-Za-z0-9]/, 'Must contain special character');
// Or use a password strength library const zxcvbn = require('zxcvbn'); const result = zxcvbn(password); if (result.score < 3) { return res.status(400).json({ error: 'Password too weak', suggestions: result.feedback.suggestions }); } ```
Symptoms: Users can access resources they shouldn't Solution: ```javascript // ❌ Bad: Only checks authentication app.delete('/api/posts/:id', authenticateToken, async (req, res) => { await prisma.post.delete({ where: { id: req.params.id } }); res.json({ success: true }); });
// ✅ Good: Checks both authentication and authorization app.delete('/api/posts/:id', authenticateToken, async (req, res) => { const post = await prisma.post.findUnique({ where: { id: req.params.id } });
if (!post) { return res.status(404).json({ error: 'Post not found' }); }
// Check if user owns the post or is admin if (post.userId !== req.user.userId && req.user.role !== 'admin') { return res.status(403).json({ error: 'Not authorized to delete this post' }); }
await prisma.post.delete({ where: { id: req.params.id } }); res.json({ success: true }); }); ```
Symptoms: Error messages reveal system details
Solution:
```javascript
// ❌ Bad: Exposes database details
app.post('/api/users', async (req, res) => {
try {
const user = await prisma.user.create({ data: req.body });
res.json(user);
} catch (error) {
res.status(500).json({ error: error.message });
// Error: "Unique constraint failed on the fields: (email)"
}
});
// ✅ Good: Generic error message app.post('/api/users', async (req, res) => { try { const user = await prisma.user.create({ data: req.body }); res.json(user); } catch (error) { console.error('User creation error:', error); // Log full error
} }); ```
@ethical-hacking-methodology - Security testing perspective@sql-injection-testing - Testing for SQL injection@xss-html-injection - Testing for XSS vulnerabilities@broken-authentication - Authentication vulnerabilities@backend-dev-guidelines - Backend development standards@systematic-debugging - Debug security issuesPro Tip: Security is not a one-time task - regularly audit your APIs, keep dependencies updated, and stay informed about new vulnerabilities!