Security review skill: comprehensive security checklist and patterns. Use when adding authentication, handling user input, working with secrets, or creating API endpoints.
View on GitHubxiaobei930/claude-code-best-practices
cc-best
January 25, 2026
Select agents to install to:
npx add-skill https://github.com/xiaobei930/claude-code-best-practices/blob/main/skills/security-review/SKILL.md -a claude-code --skill security-reviewInstallation paths:
.claude/skills/security-review/# 安全审查技能
本技能确保所有代码遵循安全最佳实践,识别潜在漏洞。
## 触发条件
- 实现认证或授权
- 处理用户输入或文件上传
- 创建新的 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 未配置");
}
```
```python
import os
api_key = os.getenv("API_KEY")
if not api_key:
raise ValueError("API_KEY 未配置")
```
#### 检查项
- [ ] 无硬编码的 API 密钥、Token 或密码
- [ ] 所有密钥在环境变量中
- [ ] `.env.local` 在 .gitignore 中
- [ ] Git 历史中无密钥
- [ ] 生产密钥在托管平台配置
### 2. 输入验证
#### 始终验证用户输入
```typescript
import { z } from "zod";
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) {
const validated = CreateUserSchema.parse(input);
return await db.users.create(validated);
}
```
```python
from pydantic import BaseModel, validator
class UserCreate(BaseModel):
email: str
name: str
age: int
@validator("age")
def validate_age(cls, v):
if v < 0 or v > 150:
raise ValueError("年龄无效")
return v
```
#### 文件上传验证
```typescript
function validateFileUpload(file: File) {
// 大小检查 (5MB 限制)
const maxSize = 5 * 1024 * 1024;
if (file.size > maxSize) {
throw new Error("文件过大 (最大 5MB)");
}
// 类型检查
const allowedTypes = ["image/jpeg", "image/png", "image/gif"];
if (!allowedTypes.includes(file.type)) {
throw new Error("文件类型不允许");
}
// 扩展名检查
const allowedExtensions = [".jpg", ".jpeg", ".png", ".gif"];
const extension = file.name.toLowerCase().match(/\.[^.]+$/)?.[0];
if (!extension || !allowedExtensions.includes(extension)) {
throw new Error("文件扩展名无效");
}
return true;
}
```
#### 检查项
- [ ] 所有用户输入使用 Schema 验证
- [ ] 文件上传限制(大小、类型、扩展名)
- [ ] 不直