安全审查技能
此技能确保所有代码遵循安全最佳实践,并识别潜在漏洞。
何时激活
- 实现身份验证或授权时
- 处理用户输入或文件上传时
- 创建新的 API 端点时
- 处理密钥或凭据时
- 实现支付功能时
- 存储或传输敏感数据时
- 集成第三方 API 时
安全检查清单
1. 密钥管理
FAIL: 绝对不要这样做
const apiKey = "sk-proj-xxxxx" // Hardcoded secret
const dbPassword = "password123" // In source code
PASS: 始终这样做
const apiKey = process.env.OPENAI_API_KEY
const dbUrl = process.env.DATABASE_URL
// Verify secrets exist
if (!apiKey) {
throw new Error('OPENAI_API_KEY not configured')
}
验证步骤
- [ ] 没有硬编码的 API 密钥、令牌或密码
- [ ] 所有密钥都存储在环境变量中
- [ ]
.env文件在 .gitignore 中 - [ ] git 历史记录中没有密钥
- [ ] 生产环境密钥存储在托管平台中(Vercel, Railway)
2. 输入验证
始终验证用户输入
import { z } from 'zod'
// Define validation schema
const CreateUserSchema = z.object({
email: z.string().email(),
name: z.string().min(1).max(100),
age: z.number().int().min(0).max(150)
})
// Validate before processing
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.erro…