Skills

Security Review

当涉及添加身份验证(Authentication)、处理用户输入、操作机密(Secrets)、创建 API...

Install

npx skills add xu-xiang/everything-claude-code-zh --skill security-review -g -a claude-code -y
Category
Engineering
Updated
8mo ago
License
MIT
Context cost
Mediumabout 2,421 tokens
Runs scripts
No, instructions only

What it does

当涉及添加身份验证(Authentication)、处理用户输入、操作机密(Secrets)、创建 API 终端节点或实现支付/敏感功能时,请使用此技能。提供全面的安全检查清单和模式。

安全审查技能(Security Review Skill)

本技能(Skill)旨在确保所有代码遵循安全最佳实践,并识别潜在的漏洞。

何时激活

  • 实现身份验证(Authentication)或授权(Authorization)时
  • 处理用户输入或文件上传时
  • 创建新的 API 终端节点时
  • 操作机密(Secrets)或凭据(Credentials)时
  • 实现支付功能时
  • 存储或传输敏感数据时
  • 集成第三方 API 时

安全检查清单

1. 机密管理(Secrets Management)

❌ 严禁这样做

const apiKey = "sk-proj-xxxxx"  // 硬编码机密
const dbPassword = "password123" // 在源代码中编写密码

✅ 务必这样做

const apiKey = process.env.OPENAI_API_KEY
const dbUrl = process.env.DATABASE_URL

// 验证机密是否存在
if (!apiKey) {
  throw new Error('OPENAI_API_KEY not configured')
}

验证步骤

  • 无硬编码的 API 密钥(Keys)、令牌(Tokens)或密码
  • 所有机密均存储在环境变量中
  • .env.local 已包含在 .gitignore 中
  • Git 历史记录中无机密信息
  • 生产环境机密配置在托管平台(如 Vercel, Railway)

2. 输入校验(Input Validation)

始终校验用户输入

import { z } from 'zod'

// 定义校验模式(Schema)
const CreateUserSchema = z.object({
  email: z.string().email(),
  name: z.string().min(1).max(100),
  age: z.number().int().min(0).max(150)
})

// 在处理前进行校验
export async function createUser(input: unknown) {
  try {
    const validated = CreateUserSchema.parse(input)
    return await db.users.create(validated)
  } catch (error) {
    if (error instanceof z.ZodError) {
      return { success: false, errors: error.errors }
    }
    throw error
  }
}

文件上传校验

function validateFileUpload(file: File) {
  // 大小检查(最大 5MB)
  const maxSize = 5 * 1024 * 1024
  if (file.size > maxSize) {
    throw new Error('File too large (max 5MB)')
  }

  // 类型检查
  const allowedTypes = ['image/jpeg', 'image/png', 'image/gif']
  if (!allowedTypes.includes(file.type)) {
    throw new Error('Invalid file type')
  }

  // 扩展名检查
  const allowedExtensions = ['.jpg', '.jpeg', '.png', '.gif']
  const extension = file.name.toLowerCase().match(/\.[^.]+$/)?.[0]
  if (!extension || !allowedExtensions.includes(extension)) {
    throw new Error('Invalid file extension')
  }

  return true
}
Read the whole SKILL.md on GitHub

More in Engineering