[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6CBTPF70MKTK8VFYSXPCNVJ":3,"skill-same:01M6CBTPF70MKTK8VFYSXPCNVJ":77},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":28,"publishedAt":44,"linkOnly":45,"markdownContent":46,"markdownBody":47,"frontMatter":48,"refName":49,"commitSha":50,"skillDirectory":51,"forksCount":14,"repositoryHtmlUrl":52,"repositoryHomepageUrl":6,"ownerHtmlUrl":53,"ownerBlogUrl":54,"ownerDescription":55,"updatedAt":56,"install":57,"resources":70},"01M6CBTPF70MKTK8VFYSXPCNVJ","api-security-best-practices",null,"Implement secure API design patterns including authentication, authorization, input validation, rate limiting, and protection against common API vulnerabilities","Gabriellmaia01\u002Fneuro-dose-assist","Gabriellmaia01","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F135177180?v=4","01M6CBRZEZC8G5C400AG3SPFNH","Gabriel","01M6CBRZK8MB7DWW0B4C82TV2G",0,"MIT","https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist\u002Fblob\u002Fmain\u002F.agent\u002Fskills\u002Fskills\u002Fapi-security-best-practices\u002FSKILL.md",[18,23],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000010","后端与接口","SCENARIO","使用场景",{"categoryNo":24,"categoryName":25,"taxonomyType":26,"taxonomyTypeDesc":27},"01JBSEEDCAT000000000000030","开发工程师","PROFESSION","适用职业",[29,30,31,32,33,34,35,36,37,38,39,40,41,42,43],"security","best practices","security best practices","api security best practices","api","design","implement","vulnerabilities","secure","api design","authentication","validation","authorization","common","design patterns","2026-11-10 10:12:30",false,"---\nname: api-security-best-practices\ndescription: \"Implement secure API design patterns including authentication, authorization, input validation, rate limiting, and protection against common API vulnerabilities\"\n---\n\n# API Security Best Practices\n\n## Overview\n\nGuide 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.\n\n## When to Use This Skill\n\n- Use when designing new API endpoints\n- Use when securing existing APIs\n- Use when implementing authentication and authorization\n- Use when protecting against API attacks (injection, DDoS, etc.)\n- Use when conducting API security reviews\n- Use when preparing for security audits\n- Use when implementing rate limiting and throttling\n- Use when handling sensitive data in APIs\n\n## How It Works\n\n### Step 1: Authentication & Authorization\n\nI'll help you implement secure authentication:\n- Choose authentication method (JWT, OAuth 2.0, API keys)\n- Implement token-based authentication\n- Set up role-based access control (RBAC)\n- Secure session management\n- Implement multi-factor authentication (MFA)\n\n### Step 2: Input Validation & Sanitization\n\nProtect against injection attacks:\n- Validate all input data\n- Sanitize user inputs\n- Use parameterized queries\n- Implement request schema validation\n- Prevent SQL injection, XSS, and command injection\n\n### Step 3: Rate Limiting & Throttling\n\nPrevent abuse and DDoS attacks:\n- Implement rate limiting per user\u002FIP\n- Set up API throttling\n- Configure request quotas\n- Handle rate limit errors gracefully\n- Monitor for suspicious activity\n\n### Step 4: Data Protection\n\nSecure sensitive data:\n- Encrypt data in transit (HTTPS\u002FTLS)\n- Encrypt sensitive data at rest\n- Implement proper error handling (no data leaks)\n- Sanitize error messages\n- Use secure headers\n\n### Step 5: API Security Testing\n\nVerify security implementation:\n- Test authentication and authorization\n- Perform penetration testing\n- Check for common vulnerabilities (OWASP API Top 10)\n- Validate input handling\n- Test rate limiting\n\n\n## Examples\n\n### Example 1: Implementing JWT Authentication\n\n```markdown\n## Secure JWT Authentication Implementation\n\n### Authentication Flow\n\n1. User logs in with credentials\n2. Server validates credentials\n3. Server generates JWT token\n4. Client stores token securely\n5. Client sends token with each request\n6. Server validates token\n\n### Implementation\n\n#### 1. Generate Secure JWT Tokens\n\n\\`\\`\\`javascript\n\u002F\u002F auth.js\nconst jwt = require('jsonwebtoken');\nconst bcrypt = require('bcrypt');\n\n\u002F\u002F Login endpoint\napp.post('\u002Fapi\u002Fauth\u002Flogin', async (req, res) => {\n  try {\n    const { email, password } = req.body;\n    \n    \u002F\u002F Validate input\n    if (!email || !password) {\n      return res.status(400).json({ \n        error: 'Email and password are required' \n      });\n    }\n    \n    \u002F\u002F Find user\n    const user = await db.user.findUnique({ \n      where: { email } \n    });\n    \n    if (!user) {\n      \u002F\u002F Don't reveal if user exists\n      return res.status(401).json({ \n        error: 'Invalid credentials' \n      });\n    }\n    \n    \u002F\u002F Verify password\n    const validPassword = await bcrypt.compare(\n      password, \n      user.passwordHash\n    );\n    \n    if (!validPassword) {\n      return res.status(401).json({ \n        error: 'Invalid credentials' \n      });\n    }\n    \n    \u002F\u002F Generate JWT token\n    const token = jwt.sign(\n      { \n        userId: user.id,\n        email: user.email,\n        role: user.role\n      },\n      process.env.JWT_SECRET,\n      { \n        expiresIn: '1h',\n        issuer: 'your-app',\n        audience: 'your-app-users'\n      }\n    );\n    \n    \u002F\u002F Generate refresh token\n    const refreshToken = jwt.sign(\n      { userId: user.id },\n      process.env.JWT_REFRESH_SECRET,\n      { expiresIn: '7d' }\n    );\n    \n    \u002F\u002F Store refresh token in database\n    await db.refreshToken.create({\n      data: {\n        token: refreshToken,\n        userId: user.id,\n        expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)\n      }\n    });\n    \n    res.json({\n      token,\n      refreshToken,\n      expiresIn: 3600\n    });\n    \n  } catch (error) {\n    console.error('Login error:', error);\n    res.status(500).json({ \n      error: 'An error occurred during login' \n    });\n  }\n});\n\\`\\`\\`\n\n#### 2. Verify JWT Tokens (Middleware)\n\n\\`\\`\\`javascript\n\u002F\u002F middleware\u002Fauth.js\nconst jwt = require('jsonwebtoken');\n\nfunction authenticateToken(req, res, next) {\n  \u002F\u002F Get token from header\n  const authHeader = req.headers['authorization'];\n  const token = authHeader && authHeader.split(' ')[1]; \u002F\u002F Bearer TOKEN\n  \n  if (!token) {\n    return res.status(401).json({ \n      error: 'Access token required' \n    });\n  }\n  \n  \u002F\u002F Verify token\n  jwt.verify(\n    token, \n    process.env.JWT_SECRET,\n    { \n      issuer: 'your-app',\n      audience: 'your-app-users'\n    },\n    (err, user) => {\n      if (err) {\n        if (err.name === 'TokenExpiredError') {\n          return res.status(401).json({ \n            error: 'Token expired' \n          });\n        }\n        return res.status(403).json({ \n          error: 'Invalid token' \n        });\n      }\n      \n      \u002F\u002F Attach user to request\n      req.user = user;\n      next();\n    }\n  );\n}\n\nmodule.exports = { authenticateToken };\n\\`\\`\\`\n\n#### 3. Protect Routes\n\n\\`\\`\\`javascript\nconst { authenticateToken } = require('.\u002Fmiddleware\u002Fauth');\n\n\u002F\u002F Protected route\napp.get('\u002Fapi\u002Fuser\u002Fprofile', authenticateToken, async (req, res) => {\n  try {\n    const user = await db.user.findUnique({\n      where: { id: req.user.userId },\n      select: {\n        id: true,\n        email: true,\n        name: true,\n        \u002F\u002F Don't return passwordHash\n      }\n    });\n    \n    res.json(user);\n  } catch (error) {\n    res.status(500).json({ error: 'Server error' });\n  }\n});\n\\`\\`\\`\n\n#### 4. Implement Token Refresh\n\n\\`\\`\\`javascript\napp.post('\u002Fapi\u002Fauth\u002Frefresh', async (req, res) => {\n  const { refreshToken } = req.body;\n  \n  if (!refreshToken) {\n    return res.status(401).json({ \n      error: 'Refresh token required' \n    });\n  }\n  \n  try {\n    \u002F\u002F Verify refresh token\n    const decoded = jwt.verify(\n      refreshToken, \n      process.env.JWT_REFRESH_SECRET\n    );\n    \n    \u002F\u002F Check if refresh token exists in database\n    const storedToken = await db.refreshToken.findFirst({\n      where: {\n        token: refreshToken,\n        userId: decoded.userId,\n        expiresAt: { gt: new Date() }\n      }\n    });\n    \n    if (!storedToken) {\n      return res.status(403).json({ \n        error: 'Invalid refresh token' \n      });\n    }\n    \n    \u002F\u002F Generate new access token\n    const user = await db.user.findUnique({\n      where: { id: decoded.userId }\n    });\n    \n    const newToken = jwt.sign(\n      { \n        userId: user.id,\n        email: user.email,\n        role: user.role\n      },\n      process.env.JWT_SECRET,\n      { expiresIn: '1h' }\n    );\n    \n    res.json({\n      token: newToken,\n      expiresIn: 3600\n    });\n    \n  } catch (error) {\n    res.status(403).json({ \n      error: 'Invalid refresh token' \n    });\n  }\n});\n\\`\\`\\`\n\n### Security Best Practices\n\n- ✅ Use strong JWT secrets (256-bit minimum)\n- ✅ Set short expiration times (1 hour for access tokens)\n- ✅ Implement refresh tokens for long-lived sessions\n- ✅ Store refresh tokens in database (can be revoked)\n- ✅ Use HTTPS only\n- ✅ Don't store sensitive data in JWT payload\n- ✅ Validate token issuer and audience\n- ✅ Implement token blacklisting for logout\n```\n\n\n### Example 2: Input Validation and SQL Injection Prevention\n\n```markdown\n## Preventing SQL Injection and Input Validation\n\n### The Problem\n\n**❌ Vulnerable Code:**\n\\`\\`\\`javascript\n\u002F\u002F NEVER DO THIS - SQL Injection vulnerability\napp.get('\u002Fapi\u002Fusers\u002F:id', async (req, res) => {\n  const userId = req.params.id;\n  \n  \u002F\u002F Dangerous: User input directly in query\n  const query = \\`SELECT * FROM users WHERE id = '\\${userId}'\\`;\n  const user = await db.query(query);\n  \n  res.json(user);\n});\n\n\u002F\u002F Attack example:\n\u002F\u002F GET \u002Fapi\u002Fusers\u002F1' OR '1'='1\n\u002F\u002F Returns all users!\n\\`\\`\\`\n\n### The Solution\n\n#### 1. Use Parameterized Queries\n\n\\`\\`\\`javascript\n\u002F\u002F ✅ Safe: Parameterized query\napp.get('\u002Fapi\u002Fusers\u002F:id', async (req, res) => {\n  const userId = req.params.id;\n  \n  \u002F\u002F Validate input first\n  if (!userId || !\u002F^\\d+$\u002F.test(userId)) {\n    return res.status(400).json({ \n      error: 'Invalid user ID' \n    });\n  }\n  \n  \u002F\u002F Use parameterized query\n  const user = await db.query(\n    'SELECT id, email, name FROM users WHERE id = $1',\n    [userId]\n  );\n  \n  if (!user) {\n    return res.status(404).json({ \n      error: 'User not found' \n    });\n  }\n  \n  res.json(user);\n});\n\\`\\`\\`\n\n#### 2. Use ORM with Proper Escaping\n\n\\`\\`\\`javascript\n\u002F\u002F ✅ Safe: Using Prisma ORM\napp.get('\u002Fapi\u002Fusers\u002F:id', async (req, res) => {\n  const userId = parseInt(req.params.id);\n  \n  if (isNaN(userId)) {\n    return res.status(400).json({ \n      error: 'Invalid user ID' \n    });\n  }\n  \n  const user = await prisma.user.findUnique({\n    where: { id: userId },\n    select: {\n      id: true,\n      email: true,\n      name: true,\n      \u002F\u002F Don't select sensitive fields\n    }\n  });\n  \n  if (!user) {\n    return res.status(404).json({ \n      error: 'User not found' \n    });\n  }\n  \n  res.json(user);\n});\n\\`\\`\\`\n\n#### 3. Implement Request Validation with Zod\n\n\\`\\`\\`javascript\nconst { z } = require('zod');\n\n\u002F\u002F Define validation schema\nconst createUserSchema = z.object({\n  email: z.string().email('Invalid email format'),\n  password: z.string()\n    .min(8, 'Password must be at least 8 characters')\n    .regex(\u002F[A-Z]\u002F, 'Password must contain uppercase letter')\n    .regex(\u002F[a-z]\u002F, 'Password must contain lowercase letter')\n    .regex(\u002F[0-9]\u002F, 'Password must contain number'),\n  name: z.string()\n    .min(2, 'Name must be at least 2 characters')\n    .max(100, 'Name too long'),\n  age: z.number()\n    .int('Age must be an integer')\n    .min(18, 'Must be 18 or older')\n    .max(120, 'Invalid age')\n    .optional()\n});\n\n\u002F\u002F Validation middleware\nfunction validateRequest(schema) {\n  return (req, res, next) => {\n    try {\n      schema.parse(req.body);\n      next();\n    } catch (error) {\n      res.status(400).json({\n        error: 'Validation failed',\n        details: error.errors\n      });\n    }\n  };\n}\n\n\u002F\u002F Use validation\napp.post('\u002Fapi\u002Fusers', \n  validateRequest(createUserSchema),\n  async (req, res) => {\n    \u002F\u002F Input is validated at this point\n    const { email, password, name, age } = req.body;\n    \n    \u002F\u002F Hash password\n    const passwordHash = await bcrypt.hash(password, 10);\n    \n    \u002F\u002F Create user\n    const user = await prisma.user.create({\n      data: {\n        email,\n        passwordHash,\n        name,\n        age\n      }\n    });\n    \n    \u002F\u002F Don't return password hash\n    const { passwordHash: _, ...userWithoutPassword } = user;\n    res.status(201).json(userWithoutPassword);\n  }\n);\n\\`\\`\\`\n\n#### 4. Sanitize Output to Prevent XSS\n\n\\`\\`\\`javascript\nconst DOMPurify = require('isomorphic-dompurify');\n\napp.post('\u002Fapi\u002Fcomments', authenticateToken, async (req, res) => {\n  const { content } = req.body;\n  \n  \u002F\u002F Validate\n  if (!content || content.length > 1000) {\n    return res.status(400).json({ \n      error: 'Invalid comment content' \n    });\n  }\n  \n  \u002F\u002F Sanitize HTML to prevent XSS\n  const sanitizedContent = DOMPurify.sanitize(content, {\n    ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a'],\n    ALLOWED_ATTR: ['href']\n  });\n  \n  const comment = await prisma.comment.create({\n    data: {\n      content: sanitizedContent,\n      userId: req.user.userId\n    }\n  });\n  \n  res.status(201).json(comment);\n});\n\\`\\`\\`\n\n### Validation Checklist\n\n- [ ] Validate all user inputs\n- [ ] Use parameterized queries or ORM\n- [ ] Validate data types (string, number, email, etc.)\n- [ ] Validate data ranges (min\u002Fmax length, value ranges)\n- [ ] Sanitize HTML content\n- [ ] Escape special characters\n- [ ] Validate file uploads (type, size, content)\n- [ ] Use allowlists, not blocklists\n```\n\n\n### Example 3: Rate Limiting and DDoS Protection\n\n```markdown\n## Implementing Rate Limiting\n\n### Why Rate Limiting?\n\n- Prevent brute force attacks\n- Protect against DDoS\n- Prevent API abuse\n- Ensure fair usage\n- Reduce server costs\n\n### Implementation with Express Rate Limit\n\n\\`\\`\\`javascript\nconst rateLimit = require('express-rate-limit');\nconst RedisStore = require('rate-limit-redis');\nconst Redis = require('ioredis');\n\n\u002F\u002F Create Redis client\nconst redis = new Redis({\n  host: process.env.REDIS_HOST,\n  port: process.env.REDIS_PORT\n});\n\n\u002F\u002F General API rate limit\nconst apiLimiter = rateLimit({\n  store: new RedisStore({\n    client: redis,\n    prefix: 'rl:api:'\n  }),\n  windowMs: 15 * 60 * 1000, \u002F\u002F 15 minutes\n  max: 100, \u002F\u002F 100 requests per window\n  message: {\n    error: 'Too many requests, please try again later',\n    retryAfter: 900 \u002F\u002F seconds\n  },\n  standardHeaders: true, \u002F\u002F Return rate limit info in headers\n  legacyHeaders: false,\n  \u002F\u002F Custom key generator (by user ID or IP)\n  keyGenerator: (req) => {\n    return req.user?.userId || req.ip;\n  }\n});\n\n\u002F\u002F Strict rate limit for authentication endpoints\nconst authLimiter = rateLimit({\n  store: new RedisStore({\n    client: redis,\n    prefix: 'rl:auth:'\n  }),\n  windowMs: 15 * 60 * 1000, \u002F\u002F 15 minutes\n  max: 5, \u002F\u002F Only 5 login attempts per 15 minutes\n  skipSuccessfulRequests: true, \u002F\u002F Don't count successful logins\n  message: {\n    error: 'Too many login attempts, please try again later',\n    retryAfter: 900\n  }\n});\n\n\u002F\u002F Apply rate limiters\napp.use('\u002Fapi\u002F', apiLimiter);\napp.use('\u002Fapi\u002Fauth\u002Flogin', authLimiter);\napp.use('\u002Fapi\u002Fauth\u002Fregister', authLimiter);\n\n\u002F\u002F Custom rate limiter for expensive operations\nconst expensiveLimiter = rateLimit({\n  windowMs: 60 * 60 * 1000, \u002F\u002F 1 hour\n  max: 10, \u002F\u002F 10 requests per hour\n  message: {\n    error: 'Rate limit exceeded for this operation'\n  }\n});\n\napp.post('\u002Fapi\u002Freports\u002Fgenerate', \n  authenticateToken,\n  expensiveLimiter,\n  async (req, res) => {\n    \u002F\u002F Expensive operation\n  }\n);\n\\`\\`\\`\n\n### Advanced: Per-User Rate Limiting\n\n\\`\\`\\`javascript\n\u002F\u002F Different limits based on user tier\nfunction createTieredRateLimiter() {\n  const limits = {\n    free: { windowMs: 60 * 60 * 1000, max: 100 },\n    pro: { windowMs: 60 * 60 * 1000, max: 1000 },\n    enterprise: { windowMs: 60 * 60 * 1000, max: 10000 }\n  };\n  \n  return async (req, res, next) => {\n    const user = req.user;\n    const tier = user?.tier || 'free';\n    const limit = limits[tier];\n    \n    const key = \\`rl:user:\\${user.userId}\\`;\n    const current = await redis.incr(key);\n    \n    if (current === 1) {\n      await redis.expire(key, limit.windowMs \u002F 1000);\n    }\n    \n    if (current > limit.max) {\n      return res.status(429).json({\n        error: 'Rate limit exceeded',\n        limit: limit.max,\n        remaining: 0,\n        reset: await redis.ttl(key)\n      });\n    }\n    \n    \u002F\u002F Set rate limit headers\n    res.set({\n      'X-RateLimit-Limit': limit.max,\n      'X-RateLimit-Remaining': limit.max - current,\n      'X-RateLimit-Reset': await redis.ttl(key)\n    });\n    \n    next();\n  };\n}\n\napp.use('\u002Fapi\u002F', authenticateToken, createTieredRateLimiter());\n\\`\\`\\`\n\n### DDoS Protection with Helmet\n\n\\`\\`\\`javascript\nconst helmet = require('helmet');\n\napp.use(helmet({\n  \u002F\u002F Content Security Policy\n  contentSecurityPolicy: {\n    directives: {\n      defaultSrc: [\"'self'\"],\n      styleSrc: [\"'self'\", \"'unsafe-inline'\"],\n      scriptSrc: [\"'self'\"],\n      imgSrc: [\"'self'\", 'data:', 'https:']\n    }\n  },\n  \u002F\u002F Prevent clickjacking\n  frameguard: { action: 'deny' },\n  \u002F\u002F Hide X-Powered-By header\n  hidePoweredBy: true,\n  \u002F\u002F Prevent MIME type sniffing\n  noSniff: true,\n  \u002F\u002F Enable HSTS\n  hsts: {\n    maxAge: 31536000,\n    includeSubDomains: true,\n    preload: true\n  }\n}));\n\\`\\`\\`\n\n### Rate Limit Response Headers\n\n\\`\\`\\`\nX-RateLimit-Limit: 100\nX-RateLimit-Remaining: 87\nX-RateLimit-Reset: 1640000000\nRetry-After: 900\n\\`\\`\\`\n```\n\n## Best Practices\n\n### ✅ Do This\n\n- **Use HTTPS Everywhere** - Never send sensitive data over HTTP\n- **Implement Authentication** - Require authentication for protected endpoints\n- **Validate All Inputs** - Never trust user input\n- **Use Parameterized Queries** - Prevent SQL injection\n- **Implement Rate Limiting** - Protect against brute force and DDoS\n- **Hash Passwords** - Use bcrypt with salt rounds >= 10\n- **Use Short-Lived Tokens** - JWT access tokens should expire quickly\n- **Implement CORS Properly** - Only allow trusted origins\n- **Log Security Events** - Monitor for suspicious activity\n- **Keep Dependencies Updated** - Regularly update packages\n- **Use Security Headers** - Implement Helmet.js\n- **Sanitize Error Messages** - Don't leak sensitive information\n\n### ❌ Don't Do This\n\n- **Don't Store Passwords in Plain Text** - Always hash passwords\n- **Don't Use Weak Secrets** - Use strong, random JWT secrets\n- **Don't Trust User Input** - Always validate and sanitize\n- **Don't Expose Stack Traces** - Hide error details in production\n- **Don't Use String Concatenation for SQL** - Use parameterized queries\n- **Don't Store Sensitive Data in JWT** - JWTs are not encrypted\n- **Don't Ignore Security Updates** - Update dependencies regularly\n- **Don't Use Default Credentials** - Change all default passwords\n- **Don't Disable CORS Completely** - Configure it properly instead\n- **Don't Log Sensitive Data** - Sanitize logs\n\n## Common Pitfalls\n\n### Problem: JWT Secret Exposed in Code\n**Symptoms:** JWT secret hardcoded or committed to Git\n**Solution:**\n\\`\\`\\`javascript\n\u002F\u002F ❌ Bad\nconst JWT_SECRET = 'my-secret-key';\n\n\u002F\u002F ✅ Good\nconst JWT_SECRET = process.env.JWT_SECRET;\nif (!JWT_SECRET) {\n  throw new Error('JWT_SECRET environment variable is required');\n}\n\n\u002F\u002F Generate strong secret\n\u002F\u002F node -e \"console.log(require('crypto').randomBytes(64).toString('hex'))\"\n\\`\\`\\`\n\n### Problem: Weak Password Requirements\n**Symptoms:** Users can set weak passwords like \"password123\"\n**Solution:**\n\\`\\`\\`javascript\nconst passwordSchema = z.string()\n  .min(12, 'Password must be at least 12 characters')\n  .regex(\u002F[A-Z]\u002F, 'Must contain uppercase letter')\n  .regex(\u002F[a-z]\u002F, 'Must contain lowercase letter')\n  .regex(\u002F[0-9]\u002F, 'Must contain number')\n  .regex(\u002F[^A-Za-z0-9]\u002F, 'Must contain special character');\n\n\u002F\u002F Or use a password strength library\nconst zxcvbn = require('zxcvbn');\nconst result = zxcvbn(password);\nif (result.score \u003C 3) {\n  return res.status(400).json({\n    error: 'Password too weak',\n    suggestions: result.feedback.suggestions\n  });\n}\n\\`\\`\\`\n\n### Problem: Missing Authorization Checks\n**Symptoms:** Users can access resources they shouldn't\n**Solution:**\n\\`\\`\\`javascript\n\u002F\u002F ❌ Bad: Only checks authentication\napp.delete('\u002Fapi\u002Fposts\u002F:id', authenticateToken, async (req, res) => {\n  await prisma.post.delete({ where: { id: req.params.id } });\n  res.json({ success: true });\n});\n\n\u002F\u002F ✅ Good: Checks both authentication and authorization\napp.delete('\u002Fapi\u002Fposts\u002F:id', authenticateToken, async (req, res) => {\n  const post = await prisma.post.findUnique({\n    where: { id: req.params.id }\n  });\n  \n  if (!post) {\n    return res.status(404).json({ error: 'Post not found' });\n  }\n  \n  \u002F\u002F Check if user owns the post or is admin\n  if (post.userId !== req.user.userId && req.user.role !== 'admin') {\n    return res.status(403).json({ \n      error: 'Not authorized to delete this post' \n    });\n  }\n  \n  await prisma.post.delete({ where: { id: req.params.id } });\n  res.json({ success: true });\n});\n\\`\\`\\`\n\n### Problem: Verbose Error Messages\n**Symptoms:** Error messages reveal system details\n**Solution:**\n\\`\\`\\`javascript\n\u002F\u002F ❌ Bad: Exposes database details\napp.post('\u002Fapi\u002Fusers', async (req, res) => {\n  try {\n    const user = await prisma.user.create({ data: req.body });\n    res.json(user);\n  } catch (error) {\n    res.status(500).json({ error: error.message });\n    \u002F\u002F Error: \"Unique constraint failed on the fields: (`email`)\"\n  }\n});\n\n\u002F\u002F ✅ Good: Generic error message\napp.post('\u002Fapi\u002Fusers', async (req, res) => {\n  try {\n    const user = await prisma.user.create({ data: req.body });\n    res.json(user);\n  } catch (error) {\n    console.error('User creation error:', error); \u002F\u002F Log full error\n    \n    if (error.code === 'P2002') {\n      return res.status(400).json({ \n        error: 'Email already exists' \n      });\n    }\n    \n    res.status(500).json({ \n      error: 'An error occurred while creating user' \n    });\n  }\n});\n\\`\\`\\`\n\n## Security Checklist\n\n### Authentication & Authorization\n- [ ] Implement strong authentication (JWT, OAuth 2.0)\n- [ ] Use HTTPS for all endpoints\n- [ ] Hash passwords with bcrypt (salt rounds >= 10)\n- [ ] Implement token expiration\n- [ ] Add refresh token mechanism\n- [ ] Verify user authorization for each request\n- [ ] Implement role-based access control (RBAC)\n\n### Input Validation\n- [ ] Validate all user inputs\n- [ ] Use parameterized queries or ORM\n- [ ] Sanitize HTML content\n- [ ] Validate file uploads\n- [ ] Implement request schema validation\n- [ ] Use allowlists, not blocklists\n\n### Rate Limiting & DDoS Protection\n- [ ] Implement rate limiting per user\u002FIP\n- [ ] Add stricter limits for auth endpoints\n- [ ] Use Redis for distributed rate limiting\n- [ ] Return proper rate limit headers\n- [ ] Implement request throttling\n\n### Data Protection\n- [ ] Use HTTPS\u002FTLS for all traffic\n- [ ] Encrypt sensitive data at rest\n- [ ] Don't store sensitive data in JWT\n- [ ] Sanitize error messages\n- [ ] Implement proper CORS configuration\n- [ ] Use security headers (Helmet.js)\n\n### Monitoring & Logging\n- [ ] Log security events\n- [ ] Monitor for suspicious activity\n- [ ] Set up alerts for failed auth attempts\n- [ ] Track API usage patterns\n- [ ] Don't log sensitive data\n\n## OWASP API Security Top 10\n\n1. **Broken Object Level Authorization** - Always verify user can access resource\n2. **Broken Authentication** - Implement strong authentication mechanisms\n3. **Broken Object Property Level Authorization** - Validate which properties user can access\n4. **Unrestricted Resource Consumption** - Implement rate limiting and quotas\n5. **Broken Function Level Authorization** - Verify user role for each function\n6. **Unrestricted Access to Sensitive Business Flows** - Protect critical workflows\n7. **Server Side Request Forgery (SSRF)** - Validate and sanitize URLs\n8. **Security Misconfiguration** - Use security best practices and headers\n9. **Improper Inventory Management** - Document and secure all API endpoints\n10. **Unsafe Consumption of APIs** - Validate data from third-party APIs\n\n## Related Skills\n\n- `@ethical-hacking-methodology` - Security testing perspective\n- `@sql-injection-testing` - Testing for SQL injection\n- `@xss-html-injection` - Testing for XSS vulnerabilities\n- `@broken-authentication` - Authentication vulnerabilities\n- `@backend-dev-guidelines` - Backend development standards\n- `@systematic-debugging` - Debug security issues\n\n## Additional Resources\n\n- [OWASP API Security Top 10](https:\u002F\u002Fowasp.org\u002Fwww-project-api-security\u002F)\n- [JWT Best Practices](https:\u002F\u002Ftools.ietf.org\u002Fhtml\u002Frfc8725)\n- [Express Security Best Practices](https:\u002F\u002Fexpressjs.com\u002Fen\u002Fadvanced\u002Fbest-practice-security.html)\n- [Node.js Security Checklist](https:\u002F\u002Fblog.risingstack.com\u002Fnode-js-security-checklist\u002F)\n- [API Security Checklist](https:\u002F\u002Fgithub.com\u002Fshieldfy\u002FAPI-Security-Checklist)\n\n---\n\n**Pro Tip:** Security is not a one-time task - regularly audit your APIs, keep dependencies updated, and stay informed about new vulnerabilities!\n","# API Security Best Practices\n\n## Overview\n\nGuide 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.\n\n## When to Use This Skill\n\n- Use when designing new API endpoints\n- Use when securing existing APIs\n- Use when implementing authentication and authorization\n- Use when protecting against API attacks (injection, DDoS, etc.)\n- Use when conducting API security reviews\n- Use when preparing for security audits\n- Use when implementing rate limiting and throttling\n- Use when handling sensitive data in APIs\n\n## How It Works\n\n### Step 1: Authentication & Authorization\n\nI'll help you implement secure authentication:\n- Choose authentication method (JWT, OAuth 2.0, API keys)\n- Implement token-based authentication\n- Set up role-based access control (RBAC)\n- Secure session management\n- Implement multi-factor authentication (MFA)\n\n### Step 2: Input Validation & Sanitization\n\nProtect against injection attacks:\n- Validate all input data\n- Sanitize user inputs\n- Use parameterized queries\n- Implement request schema validation\n- Prevent SQL injection, XSS, and command injection\n\n### Step 3: Rate Limiting & Throttling\n\nPrevent abuse and DDoS attacks:\n- Implement rate limiting per user\u002FIP\n- Set up API throttling\n- Configure request quotas\n- Handle rate limit errors gracefully\n- Monitor for suspicious activity\n\n### Step 4: Data Protection\n\nSecure sensitive data:\n- Encrypt data in transit (HTTPS\u002FTLS)\n- Encrypt sensitive data at rest\n- Implement proper error handling (no data leaks)\n- Sanitize error messages\n- Use secure headers\n\n### Step 5: API Security Testing\n\nVerify security implementation:\n- Test authentication and authorization\n- Perform penetration testing\n- Check for common vulnerabilities (OWASP API Top 10)\n- Validate input handling\n- Test rate limiting\n\n\n## Examples\n\n### Example 1: Implementing JWT Authentication\n\n```markdown\n## Secure JWT Authentication Implementation\n\n### Authentication Flow\n\n1. User logs in with credentials\n2. Server validates credentials\n3. Server generates JWT token\n4. Client stores token securely\n5. Client sends token with each request\n6. Server validates token\n\n### Implementation\n\n#### 1. Generate Secure JWT Tokens\n\n\\`\\`\\`javascript\n\u002F\u002F auth.js\nconst jwt = require('jsonwebtoken');\nconst bcrypt = require('bcrypt');\n\n\u002F\u002F Login endpoint\napp.post('\u002Fapi\u002Fauth\u002Flogin', async (req, res) => {\n  try {\n    const { email, password } = req.body;\n    \n    \u002F\u002F Validate input\n    if (!email || !password) {\n      return res.status(400).json({ \n        error: 'Email and password are required' \n      });\n    }\n    \n    \u002F\u002F Find user\n    const user = await db.user.findUnique({ \n      where: { email } \n    });\n    \n    if (!user) {\n      \u002F\u002F Don't reveal if user exists\n      return res.status(401).json({ \n        error: 'Invalid credentials' \n      });\n    }\n    \n    \u002F\u002F Verify password\n    const validPassword = await bcrypt.compare(\n      password, \n      user.passwordHash\n    );\n    \n    if (!validPassword) {\n      return res.status(401).json({ \n        error: 'Invalid credentials' \n      });\n    }\n    \n    \u002F\u002F Generate JWT token\n    const token = jwt.sign(\n      { \n        userId: user.id,\n        email: user.email,\n        role: user.role\n      },\n      process.env.JWT_SECRET,\n      { \n        expiresIn: '1h',\n        issuer: 'your-app',\n        audience: 'your-app-users'\n      }\n    );\n    \n    \u002F\u002F Generate refresh token\n    const refreshToken = jwt.sign(\n      { userId: user.id },\n      process.env.JWT_REFRESH_SECRET,\n      { expiresIn: '7d' }\n    );\n    \n    \u002F\u002F Store refresh token in database\n    await db.refreshToken.create({\n      data: {\n        token: refreshToken,\n        userId: user.id,\n        expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)\n      }\n    });\n    \n    res.json({\n      token,\n      refreshToken,\n      expiresIn: 3600\n    });\n    \n  } catch (error) {\n    console.error('Login error:', error);\n    res.status(500).json({ \n      error: 'An error occurred during login' \n    });\n  }\n});\n\\`\\`\\`\n\n#### 2. Verify JWT Tokens (Middleware)\n\n\\`\\`\\`javascript\n\u002F\u002F middleware\u002Fauth.js\nconst jwt = require('jsonwebtoken');\n\nfunction authenticateToken(req, res, next) {\n  \u002F\u002F Get token from header\n  const authHeader = req.headers['authorization'];\n  const token = authHeader && authHeader.split(' ')[1]; \u002F\u002F Bearer TOKEN\n  \n  if (!token) {\n    return res.status(401).json({ \n      error: 'Access token required' \n    });\n  }\n  \n  \u002F\u002F Verify token\n  jwt.verify(\n    token, \n    process.env.JWT_SECRET,\n    { \n      issuer: 'your-app',\n      audience: 'your-app-users'\n    },\n    (err, user) => {\n      if (err) {\n        if (err.name === 'TokenExpiredError') {\n          return res.status(401).json({ \n            error: 'Token expired' \n          });\n        }\n        return res.status(403).json({ \n          error: 'Invalid token' \n        });\n      }\n      \n      \u002F\u002F Attach user to request\n      req.user = user;\n      next();\n    }\n  );\n}\n\nmodule.exports = { authenticateToken };\n\\`\\`\\`\n\n#### 3. Protect Routes\n\n\\`\\`\\`javascript\nconst { authenticateToken } = require('.\u002Fmiddleware\u002Fauth');\n\n\u002F\u002F Protected route\napp.get('\u002Fapi\u002Fuser\u002Fprofile', authenticateToken, async (req, res) => {\n  try {\n    const user = await db.user.findUnique({\n      where: { id: req.user.userId },\n      select: {\n        id: true,\n        email: true,\n        name: true,\n        \u002F\u002F Don't return passwordHash\n      }\n    });\n    \n    res.json(user);\n  } catch (error) {\n    res.status(500).json({ error: 'Server error' });\n  }\n});\n\\`\\`\\`\n\n#### 4. Implement Token Refresh\n\n\\`\\`\\`javascript\napp.post('\u002Fapi\u002Fauth\u002Frefresh', async (req, res) => {\n  const { refreshToken } = req.body;\n  \n  if (!refreshToken) {\n    return res.status(401).json({ \n      error: 'Refresh token required' \n    });\n  }\n  \n  try {\n    \u002F\u002F Verify refresh token\n    const decoded = jwt.verify(\n      refreshToken, \n      process.env.JWT_REFRESH_SECRET\n    );\n    \n    \u002F\u002F Check if refresh token exists in database\n    const storedToken = await db.refreshToken.findFirst({\n      where: {\n        token: refreshToken,\n        userId: decoded.userId,\n        expiresAt: { gt: new Date() }\n      }\n    });\n    \n    if (!storedToken) {\n      return res.status(403).json({ \n        error: 'Invalid refresh token' \n      });\n    }\n    \n    \u002F\u002F Generate new access token\n    const user = await db.user.findUnique({\n      where: { id: decoded.userId }\n    });\n    \n    const newToken = jwt.sign(\n      { \n        userId: user.id,\n        email: user.email,\n        role: user.role\n      },\n      process.env.JWT_SECRET,\n      { expiresIn: '1h' }\n    );\n    \n    res.json({\n      token: newToken,\n      expiresIn: 3600\n    });\n    \n  } catch (error) {\n    res.status(403).json({ \n      error: 'Invalid refresh token' \n    });\n  }\n});\n\\`\\`\\`\n\n### Security Best Practices\n\n- ✅ Use strong JWT secrets (256-bit minimum)\n- ✅ Set short expiration times (1 hour for access tokens)\n- ✅ Implement refresh tokens for long-lived sessions\n- ✅ Store refresh tokens in database (can be revoked)\n- ✅ Use HTTPS only\n- ✅ Don't store sensitive data in JWT payload\n- ✅ Validate token issuer and audience\n- ✅ Implement token blacklisting for logout\n```\n\n\n### Example 2: Input Validation and SQL Injection Prevention\n\n```markdown\n## Preventing SQL Injection and Input Validation\n\n### The Problem\n\n**❌ Vulnerable Code:**\n\\`\\`\\`javascript\n\u002F\u002F NEVER DO THIS - SQL Injection vulnerability\napp.get('\u002Fapi\u002Fusers\u002F:id', async (req, res) => {\n  const userId = req.params.id;\n  \n  \u002F\u002F Dangerous: User input directly in query\n  const query = \\`SELECT * FROM users WHERE id = '\\${userId}'\\`;\n  const user = await db.query(query);\n  \n  res.json(user);\n});\n\n\u002F\u002F Attack example:\n\u002F\u002F GET \u002Fapi\u002Fusers\u002F1' OR '1'='1\n\u002F\u002F Returns all users!\n\\`\\`\\`\n\n### The Solution\n\n#### 1. Use Parameterized Queries\n\n\\`\\`\\`javascript\n\u002F\u002F ✅ Safe: Parameterized query\napp.get('\u002Fapi\u002Fusers\u002F:id', async (req, res) => {\n  const userId = req.params.id;\n  \n  \u002F\u002F Validate input first\n  if (!userId || !\u002F^\\d+$\u002F.test(userId)) {\n    return res.status(400).json({ \n      error: 'Invalid user ID' \n    });\n  }\n  \n  \u002F\u002F Use parameterized query\n  const user = await db.query(\n    'SELECT id, email, name FROM users WHERE id = $1',\n    [userId]\n  );\n  \n  if (!user) {\n    return res.status(404).json({ \n      error: 'User not found' \n    });\n  }\n  \n  res.json(user);\n});\n\\`\\`\\`\n\n#### 2. Use ORM with Proper Escaping\n\n\\`\\`\\`javascript\n\u002F\u002F ✅ Safe: Using Prisma ORM\napp.get('\u002Fapi\u002Fusers\u002F:id', async (req, res) => {\n  const userId = parseInt(req.params.id);\n  \n  if (isNaN(userId)) {\n    return res.status(400).json({ \n      error: 'Invalid user ID' \n    });\n  }\n  \n  const user = await prisma.user.findUnique({\n    where: { id: userId },\n    select: {\n      id: true,\n      email: true,\n      name: true,\n      \u002F\u002F Don't select sensitive fields\n    }\n  });\n  \n  if (!user) {\n    return res.status(404).json({ \n      error: 'User not found' \n    });\n  }\n  \n  res.json(user);\n});\n\\`\\`\\`\n\n#### 3. Implement Request Validation with Zod\n\n\\`\\`\\`javascript\nconst { z } = require('zod');\n\n\u002F\u002F Define validation schema\nconst createUserSchema = z.object({\n  email: z.string().email('Invalid email format'),\n  password: z.string()\n    .min(8, 'Password must be at least 8 characters')\n    .regex(\u002F[A-Z]\u002F, 'Password must contain uppercase letter')\n    .regex(\u002F[a-z]\u002F, 'Password must contain lowercase letter')\n    .regex(\u002F[0-9]\u002F, 'Password must contain number'),\n  name: z.string()\n    .min(2, 'Name must be at least 2 characters')\n    .max(100, 'Name too long'),\n  age: z.number()\n    .int('Age must be an integer')\n    .min(18, 'Must be 18 or older')\n    .max(120, 'Invalid age')\n    .optional()\n});\n\n\u002F\u002F Validation middleware\nfunction validateRequest(schema) {\n  return (req, res, next) => {\n    try {\n      schema.parse(req.body);\n      next();\n    } catch (error) {\n      res.status(400).json({\n        error: 'Validation failed',\n        details: error.errors\n      });\n    }\n  };\n}\n\n\u002F\u002F Use validation\napp.post('\u002Fapi\u002Fusers', \n  validateRequest(createUserSchema),\n  async (req, res) => {\n    \u002F\u002F Input is validated at this point\n    const { email, password, name, age } = req.body;\n    \n    \u002F\u002F Hash password\n    const passwordHash = await bcrypt.hash(password, 10);\n    \n    \u002F\u002F Create user\n    const user = await prisma.user.create({\n      data: {\n        email,\n        passwordHash,\n        name,\n        age\n      }\n    });\n    \n    \u002F\u002F Don't return password hash\n    const { passwordHash: _, ...userWithoutPassword } = user;\n    res.status(201).json(userWithoutPassword);\n  }\n);\n\\`\\`\\`\n\n#### 4. Sanitize Output to Prevent XSS\n\n\\`\\`\\`javascript\nconst DOMPurify = require('isomorphic-dompurify');\n\napp.post('\u002Fapi\u002Fcomments', authenticateToken, async (req, res) => {\n  const { content } = req.body;\n  \n  \u002F\u002F Validate\n  if (!content || content.length > 1000) {\n    return res.status(400).json({ \n      error: 'Invalid comment content' \n    });\n  }\n  \n  \u002F\u002F Sanitize HTML to prevent XSS\n  const sanitizedContent = DOMPurify.sanitize(content, {\n    ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a'],\n    ALLOWED_ATTR: ['href']\n  });\n  \n  const comment = await prisma.comment.create({\n    data: {\n      content: sanitizedContent,\n      userId: req.user.userId\n    }\n  });\n  \n  res.status(201).json(comment);\n});\n\\`\\`\\`\n\n### Validation Checklist\n\n- [ ] Validate all user inputs\n- [ ] Use parameterized queries or ORM\n- [ ] Validate data types (string, number, email, etc.)\n- [ ] Validate data ranges (min\u002Fmax length, value ranges)\n- [ ] Sanitize HTML content\n- [ ] Escape special characters\n- [ ] Validate file uploads (type, size, content)\n- [ ] Use allowlists, not blocklists\n```\n\n\n### Example 3: Rate Limiting and DDoS Protection\n\n```markdown\n## Implementing Rate Limiting\n\n### Why Rate Limiting?\n\n- Prevent brute force attacks\n- Protect against DDoS\n- Prevent API abuse\n- Ensure fair usage\n- Reduce server costs\n\n### Implementation with Express Rate Limit\n\n\\`\\`\\`javascript\nconst rateLimit = require('express-rate-limit');\nconst RedisStore = require('rate-limit-redis');\nconst Redis = require('ioredis');\n\n\u002F\u002F Create Redis client\nconst redis = new Redis({\n  host: process.env.REDIS_HOST,\n  port: process.env.REDIS_PORT\n});\n\n\u002F\u002F General API rate limit\nconst apiLimiter = rateLimit({\n  store: new RedisStore({\n    client: redis,\n    prefix: 'rl:api:'\n  }),\n  windowMs: 15 * 60 * 1000, \u002F\u002F 15 minutes\n  max: 100, \u002F\u002F 100 requests per window\n  message: {\n    error: 'Too many requests, please try again later',\n    retryAfter: 900 \u002F\u002F seconds\n  },\n  standardHeaders: true, \u002F\u002F Return rate limit info in headers\n  legacyHeaders: false,\n  \u002F\u002F Custom key generator (by user ID or IP)\n  keyGenerator: (req) => {\n    return req.user?.userId || req.ip;\n  }\n});\n\n\u002F\u002F Strict rate limit for authentication endpoints\nconst authLimiter = rateLimit({\n  store: new RedisStore({\n    client: redis,\n    prefix: 'rl:auth:'\n  }),\n  windowMs: 15 * 60 * 1000, \u002F\u002F 15 minutes\n  max: 5, \u002F\u002F Only 5 login attempts per 15 minutes\n  skipSuccessfulRequests: true, \u002F\u002F Don't count successful logins\n  message: {\n    error: 'Too many login attempts, please try again later',\n    retryAfter: 900\n  }\n});\n\n\u002F\u002F Apply rate limiters\napp.use('\u002Fapi\u002F', apiLimiter);\napp.use('\u002Fapi\u002Fauth\u002Flogin', authLimiter);\napp.use('\u002Fapi\u002Fauth\u002Fregister', authLimiter);\n\n\u002F\u002F Custom rate limiter for expensive operations\nconst expensiveLimiter = rateLimit({\n  windowMs: 60 * 60 * 1000, \u002F\u002F 1 hour\n  max: 10, \u002F\u002F 10 requests per hour\n  message: {\n    error: 'Rate limit exceeded for this operation'\n  }\n});\n\napp.post('\u002Fapi\u002Freports\u002Fgenerate', \n  authenticateToken,\n  expensiveLimiter,\n  async (req, res) => {\n    \u002F\u002F Expensive operation\n  }\n);\n\\`\\`\\`\n\n### Advanced: Per-User Rate Limiting\n\n\\`\\`\\`javascript\n\u002F\u002F Different limits based on user tier\nfunction createTieredRateLimiter() {\n  const limits = {\n    free: { windowMs: 60 * 60 * 1000, max: 100 },\n    pro: { windowMs: 60 * 60 * 1000, max: 1000 },\n    enterprise: { windowMs: 60 * 60 * 1000, max: 10000 }\n  };\n  \n  return async (req, res, next) => {\n    const user = req.user;\n    const tier = user?.tier || 'free';\n    const limit = limits[tier];\n    \n    const key = \\`rl:user:\\${user.userId}\\`;\n    const current = await redis.incr(key);\n    \n    if (current === 1) {\n      await redis.expire(key, limit.windowMs \u002F 1000);\n    }\n    \n    if (current > limit.max) {\n      return res.status(429).json({\n        error: 'Rate limit exceeded',\n        limit: limit.max,\n        remaining: 0,\n        reset: await redis.ttl(key)\n      });\n    }\n    \n    \u002F\u002F Set rate limit headers\n    res.set({\n      'X-RateLimit-Limit': limit.max,\n      'X-RateLimit-Remaining': limit.max - current,\n      'X-RateLimit-Reset': await redis.ttl(key)\n    });\n    \n    next();\n  };\n}\n\napp.use('\u002Fapi\u002F', authenticateToken, createTieredRateLimiter());\n\\`\\`\\`\n\n### DDoS Protection with Helmet\n\n\\`\\`\\`javascript\nconst helmet = require('helmet');\n\napp.use(helmet({\n  \u002F\u002F Content Security Policy\n  contentSecurityPolicy: {\n    directives: {\n      defaultSrc: [\"'self'\"],\n      styleSrc: [\"'self'\", \"'unsafe-inline'\"],\n      scriptSrc: [\"'self'\"],\n      imgSrc: [\"'self'\", 'data:', 'https:']\n    }\n  },\n  \u002F\u002F Prevent clickjacking\n  frameguard: { action: 'deny' },\n  \u002F\u002F Hide X-Powered-By header\n  hidePoweredBy: true,\n  \u002F\u002F Prevent MIME type sniffing\n  noSniff: true,\n  \u002F\u002F Enable HSTS\n  hsts: {\n    maxAge: 31536000,\n    includeSubDomains: true,\n    preload: true\n  }\n}));\n\\`\\`\\`\n\n### Rate Limit Response Headers\n\n\\`\\`\\`\nX-RateLimit-Limit: 100\nX-RateLimit-Remaining: 87\nX-RateLimit-Reset: 1640000000\nRetry-After: 900\n\\`\\`\\`\n```\n\n## Best Practices\n\n### ✅ Do This\n\n- **Use HTTPS Everywhere** - Never send sensitive data over HTTP\n- **Implement Authentication** - Require authentication for protected endpoints\n- **Validate All Inputs** - Never trust user input\n- **Use Parameterized Queries** - Prevent SQL injection\n- **Implement Rate Limiting** - Protect against brute force and DDoS\n- **Hash Passwords** - Use bcrypt with salt rounds >= 10\n- **Use Short-Lived Tokens** - JWT access tokens should expire quickly\n- **Implement CORS Properly** - Only allow trusted origins\n- **Log Security Events** - Monitor for suspicious activity\n- **Keep Dependencies Updated** - Regularly update packages\n- **Use Security Headers** - Implement Helmet.js\n- **Sanitize Error Messages** - Don't leak sensitive information\n\n### ❌ Don't Do This\n\n- **Don't Store Passwords in Plain Text** - Always hash passwords\n- **Don't Use Weak Secrets** - Use strong, random JWT secrets\n- **Don't Trust User Input** - Always validate and sanitize\n- **Don't Expose Stack Traces** - Hide error details in production\n- **Don't Use String Concatenation for SQL** - Use parameterized queries\n- **Don't Store Sensitive Data in JWT** - JWTs are not encrypted\n- **Don't Ignore Security Updates** - Update dependencies regularly\n- **Don't Use Default Credentials** - Change all default passwords\n- **Don't Disable CORS Completely** - Configure it properly instead\n- **Don't Log Sensitive Data** - Sanitize logs\n\n## Common Pitfalls\n\n### Problem: JWT Secret Exposed in Code\n**Symptoms:** JWT secret hardcoded or committed to Git\n**Solution:**\n\\`\\`\\`javascript\n\u002F\u002F ❌ Bad\nconst JWT_SECRET = 'my-secret-key';\n\n\u002F\u002F ✅ Good\nconst JWT_SECRET = process.env.JWT_SECRET;\nif (!JWT_SECRET) {\n  throw new Error('JWT_SECRET environment variable is required');\n}\n\n\u002F\u002F Generate strong secret\n\u002F\u002F node -e \"console.log(require('crypto').randomBytes(64).toString('hex'))\"\n\\`\\`\\`\n\n### Problem: Weak Password Requirements\n**Symptoms:** Users can set weak passwords like \"password123\"\n**Solution:**\n\\`\\`\\`javascript\nconst passwordSchema = z.string()\n  .min(12, 'Password must be at least 12 characters')\n  .regex(\u002F[A-Z]\u002F, 'Must contain uppercase letter')\n  .regex(\u002F[a-z]\u002F, 'Must contain lowercase letter')\n  .regex(\u002F[0-9]\u002F, 'Must contain number')\n  .regex(\u002F[^A-Za-z0-9]\u002F, 'Must contain special character');\n\n\u002F\u002F Or use a password strength library\nconst zxcvbn = require('zxcvbn');\nconst result = zxcvbn(password);\nif (result.score \u003C 3) {\n  return res.status(400).json({\n    error: 'Password too weak',\n    suggestions: result.feedback.suggestions\n  });\n}\n\\`\\`\\`\n\n### Problem: Missing Authorization Checks\n**Symptoms:** Users can access resources they shouldn't\n**Solution:**\n\\`\\`\\`javascript\n\u002F\u002F ❌ Bad: Only checks authentication\napp.delete('\u002Fapi\u002Fposts\u002F:id', authenticateToken, async (req, res) => {\n  await prisma.post.delete({ where: { id: req.params.id } });\n  res.json({ success: true });\n});\n\n\u002F\u002F ✅ Good: Checks both authentication and authorization\napp.delete('\u002Fapi\u002Fposts\u002F:id', authenticateToken, async (req, res) => {\n  const post = await prisma.post.findUnique({\n    where: { id: req.params.id }\n  });\n  \n  if (!post) {\n    return res.status(404).json({ error: 'Post not found' });\n  }\n  \n  \u002F\u002F Check if user owns the post or is admin\n  if (post.userId !== req.user.userId && req.user.role !== 'admin') {\n    return res.status(403).json({ \n      error: 'Not authorized to delete this post' \n    });\n  }\n  \n  await prisma.post.delete({ where: { id: req.params.id } });\n  res.json({ success: true });\n});\n\\`\\`\\`\n\n### Problem: Verbose Error Messages\n**Symptoms:** Error messages reveal system details\n**Solution:**\n\\`\\`\\`javascript\n\u002F\u002F ❌ Bad: Exposes database details\napp.post('\u002Fapi\u002Fusers', async (req, res) => {\n  try {\n    const user = await prisma.user.create({ data: req.body });\n    res.json(user);\n  } catch (error) {\n    res.status(500).json({ error: error.message });\n    \u002F\u002F Error: \"Unique constraint failed on the fields: (`email`)\"\n  }\n});\n\n\u002F\u002F ✅ Good: Generic error message\napp.post('\u002Fapi\u002Fusers', async (req, res) => {\n  try {\n    const user = await prisma.user.create({ data: req.body });\n    res.json(user);\n  } catch (error) {\n    console.error('User creation error:', error); \u002F\u002F Log full error\n    \n    if (error.code === 'P2002') {\n      return res.status(400).json({ \n        error: 'Email already exists' \n      });\n    }\n    \n    res.status(500).json({ \n      error: 'An error occurred while creating user' \n    });\n  }\n});\n\\`\\`\\`\n\n## Security Checklist\n\n### Authentication & Authorization\n- [ ] Implement strong authentication (JWT, OAuth 2.0)\n- [ ] Use HTTPS for all endpoints\n- [ ] Hash passwords with bcrypt (salt rounds >= 10)\n- [ ] Implement token expiration\n- [ ] Add refresh token mechanism\n- [ ] Verify user authorization for each request\n- [ ] Implement role-based access control (RBAC)\n\n### Input Validation\n- [ ] Validate all user inputs\n- [ ] Use parameterized queries or ORM\n- [ ] Sanitize HTML content\n- [ ] Validate file uploads\n- [ ] Implement request schema validation\n- [ ] Use allowlists, not blocklists\n\n### Rate Limiting & DDoS Protection\n- [ ] Implement rate limiting per user\u002FIP\n- [ ] Add stricter limits for auth endpoints\n- [ ] Use Redis for distributed rate limiting\n- [ ] Return proper rate limit headers\n- [ ] Implement request throttling\n\n### Data Protection\n- [ ] Use HTTPS\u002FTLS for all traffic\n- [ ] Encrypt sensitive data at rest\n- [ ] Don't store sensitive data in JWT\n- [ ] Sanitize error messages\n- [ ] Implement proper CORS configuration\n- [ ] Use security headers (Helmet.js)\n\n### Monitoring & Logging\n- [ ] Log security events\n- [ ] Monitor for suspicious activity\n- [ ] Set up alerts for failed auth attempts\n- [ ] Track API usage patterns\n- [ ] Don't log sensitive data\n\n## OWASP API Security Top 10\n\n1. **Broken Object Level Authorization** - Always verify user can access resource\n2. **Broken Authentication** - Implement strong authentication mechanisms\n3. **Broken Object Property Level Authorization** - Validate which properties user can access\n4. **Unrestricted Resource Consumption** - Implement rate limiting and quotas\n5. **Broken Function Level Authorization** - Verify user role for each function\n6. **Unrestricted Access to Sensitive Business Flows** - Protect critical workflows\n7. **Server Side Request Forgery (SSRF)** - Validate and sanitize URLs\n8. **Security Misconfiguration** - Use security best practices and headers\n9. **Improper Inventory Management** - Document and secure all API endpoints\n10. **Unsafe Consumption of APIs** - Validate data from third-party APIs\n\n## Related Skills\n\n- `@ethical-hacking-methodology` - Security testing perspective\n- `@sql-injection-testing` - Testing for SQL injection\n- `@xss-html-injection` - Testing for XSS vulnerabilities\n- `@broken-authentication` - Authentication vulnerabilities\n- `@backend-dev-guidelines` - Backend development standards\n- `@systematic-debugging` - Debug security issues\n\n## Additional Resources\n\n- [OWASP API Security Top 10](https:\u002F\u002Fowasp.org\u002Fwww-project-api-security\u002F)\n- [JWT Best Practices](https:\u002F\u002Ftools.ietf.org\u002Fhtml\u002Frfc8725)\n- [Express Security Best Practices](https:\u002F\u002Fexpressjs.com\u002Fen\u002Fadvanced\u002Fbest-practice-security.html)\n- [Node.js Security Checklist](https:\u002F\u002Fblog.risingstack.com\u002Fnode-js-security-checklist\u002F)\n- [API Security Checklist](https:\u002F\u002Fgithub.com\u002Fshieldfy\u002FAPI-Security-Checklist)\n\n---\n\n**Pro Tip:** Security is not a one-time task - regularly audit your APIs, keep dependencies updated, and stay informed about new vulnerabilities!\n",{"name":5,"description":7},"main","eac60cc6e0b6baa2a05a765a851fb10638f4e733",".agent\u002Fskills\u002Fskills\u002Fapi-security-best-practices","https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist","https:\u002F\u002Fgithub.com\u002FGabriellmaia01","https:\u002F\u002Fportfolio-bice-theta-81.vercel.app\u002F","💻 Desenvolvimento De Software_\r\n\r\n⚙️ Engenharia De Software\r\n","2026-11-12 08:51:23",{"source":8,"skillName":5,"commands":58,"prompt":68,"zipAvailable":69},[59,62,65],{"tool":60,"command":61},"npx","npx skills add Gabriellmaia01\u002Fneuro-dose-assist --skill api-security-best-practices",{"tool":63,"command":64},"bunx","bunx skills add Gabriellmaia01\u002Fneuro-dose-assist --skill api-security-best-practices",{"tool":66,"command":67},"pnpm","pnpm dlx skills add Gabriellmaia01\u002Fneuro-dose-assist --skill api-security-best-practices","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist\u002Fblob\u002Fmain\u002F.agent\u002Fskills\u002Fskills\u002Fapi-security-best-practices\u002FSKILL.md",true,[71],{"path":72,"resourceType":73,"resourceTypeDesc":74,"sizeBytes":75,"sourceHtmlUrl":76},"agents\u002Fopenai.yaml","REFERENCE","参考文档",183,"https:\u002F\u002Fgithub.com\u002Ffernando-delosrios-sp\u002Fsailpoint-utils\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Fzoom-out\u002Fagents\u002Fopenai.yaml",[78,102,130,148,159,180],{"skillNo":79,"name":80,"nameZh":6,"description":81,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":82,"categories":83,"keywords":87,"publishedAt":44},"01M6CCFWA9JJ23708FSKK1QNBW","zapier-make-patterns","No-code automation democratizes workflow building. Zapier and Make (formerly Integromat) let non-developers automate business processes without writing code. But no-code doesn't mean no-complexity - these platforms have their own patterns, pitfalls, and breaking points.  This skill covers when to use which platform, how to build reliable automations, and when to graduate to code-based solutions. Key insight: Zapier optimizes for simplicity and integrations (7000+ apps), Make optimizes for power","https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist\u002Fblob\u002Fmain\u002F.agent\u002Fskills\u002Fskills\u002Fzapier-make-patterns\u002FSKILL.md",[84],{"categoryNo":85,"categoryName":86,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000023","Skill与智能体开发",[88,89,90,91,92,93,94,95,96,97,98,99,100,101],"zapier make patterns","writing","workflow","automation","code","apache","how","automate","integrations","business","use","simplicity","power","complexity",{"skillNo":103,"name":104,"nameZh":6,"description":105,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":106,"categories":107,"keywords":114,"publishedAt":44},"01M6CCFSBPM49S618W7G4NHH13","Cross-Site Scripting and HTML Injection Testing","This skill should be used when the user asks to \"test for XSS vulnerabilities\", \"perform cross-site scripting attacks\", \"identify HTML injection flaws\", \"exploit client-side injection vulnerabilities\", \"steal cookies via XSS\", or \"bypass content security policies\". It provides comprehensive techniques for detecting, exploiting, and understanding XSS and HTML injection attack vectors in web applications.","https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist\u002Fblob\u002Fmain\u002F.agent\u002Fskills\u002Fskills\u002Fxss-html-injection\u002FSKILL.md",[108,111],{"categoryNo":109,"categoryName":110,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000008","测试与质量",{"categoryNo":112,"categoryName":113,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000009","前端开发",[115,116,117,118,119,120,121,122,123,124,125,126,127,128,36,129,29],"injection","scripting","html","site","Cross Site Scripting and HTML Injection Testing","testing","html injection testing","cookies","applications","xss","exploit","content","web","test","user",{"skillNo":131,"name":132,"nameZh":6,"description":133,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":134,"categories":135,"keywords":143,"publishedAt":44},"01M6CCFJ9XTPXHTE4JR06WY475","writing-skills","Use when creating new skills, editing existing skills, or verifying skills work before deployment","https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist\u002Fblob\u002Fmain\u002F.agent\u002Fskills\u002Fskills\u002Fwriting-skills\u002FSKILL.md",[136,137,140],{"categoryNo":109,"categoryName":110,"taxonomyType":21,"taxonomyTypeDesc":22},{"categoryNo":138,"categoryName":139,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000018","写作与文案",{"categoryNo":141,"categoryName":142,"taxonomyType":26,"taxonomyTypeDesc":27},"01JBSEEDCAT000000000000036","内容创作者",[89,144,145,146,147,98],"writing skills","deployment","work","editing",{"skillNo":149,"name":150,"nameZh":6,"description":151,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":152,"categories":153,"keywords":156,"publishedAt":44},"01M6CCFG98EMZZ43D1CPZZZ54E","writing-plans","Use when you have a spec or requirements for a multi-step task, before touching code","https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist\u002Fblob\u002Fmain\u002F.agent\u002Fskills\u002Fskills\u002Fwriting-plans\u002FSKILL.md",[154,155],{"categoryNo":138,"categoryName":139,"taxonomyType":21,"taxonomyTypeDesc":22},{"categoryNo":141,"categoryName":142,"taxonomyType":26,"taxonomyTypeDesc":27},[89,157,158,92,98],"writing plans","spec",{"skillNo":160,"name":161,"nameZh":6,"description":162,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":163,"categories":164,"keywords":166,"publishedAt":44},"01M6CCFDPACVC9ZM8TN63MGBP5","workflow-automation","Workflow automation is the infrastructure that makes AI agents reliable. Without durable execution, a network hiccup during a 10-step payment flow means lost money and angry customers. With it, workflows resume exactly where they left off.  This skill covers the platforms (n8n, Temporal, Inngest) and patterns (sequential, parallel, orchestrator-worker) that turn brittle scripts into production-grade automation.  Key insight: The platforms make different tradeoffs. n8n optimizes for accessibility","https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist\u002Fblob\u002Fmain\u002F.agent\u002Fskills\u002Fskills\u002Fworkflow-automation\u002FSKILL.md",[165],{"categoryNo":85,"categoryName":86,"taxonomyType":21,"taxonomyTypeDesc":22},[167,91,90,168,169,170,171,172,93,173,174,175,176,177,178,179],"Workflow Automation","network","ai agents","accessibility","workflows","infrastructure","flow","Production","execution","inngest","parallel","orchestrator","resume",{"skillNo":181,"name":182,"nameZh":6,"description":183,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":184,"categories":185,"keywords":187,"publishedAt":44},"01M6CCFB0KAB0PPNV38XQPX6YX","WordPress Penetration Testing","This skill should be used when the user asks to \"pentest WordPress sites\", \"scan WordPress for vulnerabilities\", \"enumerate WordPress users, themes, or plugins\", \"exploit WordPress vulnerabilities\", or \"use WPScan\". It provides comprehensive WordPress security assessment methodologies.","https:\u002F\u002Fgithub.com\u002FGabriellmaia01\u002Fneuro-dose-assist\u002Fblob\u002Fmain\u002F.agent\u002Fskills\u002Fskills\u002Fwordpress-penetration-testing\u002FSKILL.md",[186],{"categoryNo":109,"categoryName":110,"taxonomyType":21,"taxonomyTypeDesc":22},[120,182,188,189,29,36,190,191,129,98,125,192,193],"penetration testing","wordpress","assessment","plugins","pentest","security assessment"]