security-reviewlisted
Install: claude install-skill aldo832/ai-max
# 安全审查 Skill
此 skill 确保所有代码遵循安全最佳实践并识别潜在漏洞。
## 何时激活
- 实现认证或授权
- 处理用户输入或文件上传
- 创建新的 API 端点
- 处理密钥或凭证
- 实现支付功能
- 存储或传输敏感数据
- 集成第三方 API
## 安全检查清单
### 1. 密钥管理
#### ❌ 永不这样做
```typescript
const apiKey = "sk-proj-xxxxx" // 硬编码的密钥
const dbPassword = "password123" // 在源代码中
```
#### ✅ 始终这样做
```typescript
const apiKey = process.env.OPENAI_API_KEY
const dbUrl = process.env.DATABASE_URL
// 验证密钥存在
if (!apiKey) {
throw new Error('OPENAI_API_KEY not configured')
}
```
#### 验证步骤
- [ ] 没有硬编码的 API 密钥、令牌或密码
- [ ] 所有密钥都在环境变量中
- [ ] `.env.local` 在 .gitignore 中
- [ ] git 历史中没有密钥
- [ ] 生产密钥在托管平台(Vercel、Railway)中
### 2. 输入验证
#### 始终验证用户输入
```typescript
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
}
}
```
#### 文件上传验证
```typescript
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 ty