Claude Code 安全最佳实践
全面掌握Claude Code开发中的安全最佳实践,保护你的代码、数据和系统安全。
安全威胁概览
🔴 高风险威胁
- 代码注入攻击
- 敏感信息泄露
- 权限提升攻击
🟡 中风险威胁
- 依赖包漏洞
- 配置错误
- 日志信息泄露
🟢 低风险威胁
- 版本信息暴露
- 调试信息残留
- 错误处理不当
输入验证与防护
SQL注入防护
❌ 危险的做法
// 直接拼接SQL,存在注入风险
const query = `SELECT * FROM users WHERE id = ${userId}`;
const result = await db.query(query);
✅ 安全的做法
// 使用参数化查询
const query = 'SELECT * FROM users WHERE id = ?';
const result = await db.query(query, [userId]);
// 或使用ORM
const user = await User.findById(userId);
XSS防护
// 输入验证和输出编码
import { escape } from 'html-escaper';
import validator from 'validator';
function sanitizeInput(input) {
// 验证输入格式
if (!validator.isLength(input, { min: 1, max: 1000 })) {
throw new Error('Invalid input length');
}
// HTML编码
return escape(input);
}
// 使用CSP头部
app.use((req, res, next) => {
res.setHeader(
'Content-Security-Policy',
"default-src 'self'; script-src 'self' 'unsafe-inline'"
);
next();
});
敏感信息保护
环境变量管理
# .env 文件管理
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
JWT_SECRET=your-super-secret-key
API_KEY=your-api-key
# .gitignore 必须包含
.env
.env.local
.env.production
密钥轮换策略
// 密钥管理服务
class KeyManager {
constructor() {
this.keys = new Map();
this.rotationInterval = 24 * 60 * 60 * 1000; // 24小时
}
async rotateKey(keyId) {
const newKey = await this.generateSecureKey();
const oldKey = this.keys.get(keyId);
// 保留旧密钥一段时间以支持正在处理的请求
this.keys.set(keyId, newKey);
setTimeout(() => {
this.keys.delete(`${keyId}_old`);
}, this.rotationInterval);
if (oldKey) {
this.keys.set(`${keyId}_old`, oldKey);
}
return newKey;
}
}
身份认证与授权
JWT安全实现
// 安全的JWT实现
class AuthService {
generateTokens(user) {
const accessToken = jwt.sign(
{ userId: user.id, role: user.role },
process.env.JWT_SECRET,
{ expiresIn: '15m' }
);
const refreshToken = jwt.sign(
{ userId: user.id, tokenVersion: user.tokenVersion },
process.env.REFRESH_SECRET,
{ expiresIn: '7d' }
);
return { accessToken, refreshToken };
}
async validateToken(token) {
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
// 检查用户是否仍然有效
const user = await User.findById(decoded.userId);
if (!user || !user.isActive) {
throw new Error('User not found or inactive');
}
return decoded;
} catch (error) {
throw new Error('Invalid token');
}
}
}
权限控制中间件
// RBAC权限控制
function requirePermission(permission) {
return async (req, res, next) => {
try {
const user = req.user;
const hasPermission = await checkUserPermission(user.id, permission);
if (!hasPermission) {
return res.status(403).json({
error: 'Insufficient permissions'
});
}
next();
} catch (error) {
res.status(401).json({ error: 'Authentication required' });
}
};
}
// 使用示例
app.delete('/api/users/:id',
authenticateToken,
requirePermission('user:delete'),
deleteUser
);
安全编码规范
代码审查清单
安全测试与监控
自动化安全测试
// 安全测试用例
describe('Security Tests', () => {
describe('SQL Injection Protection', () => {
it('should prevent SQL injection in user queries', async () => {
const maliciousInput = "1'; DROP TABLE users; --";
const response = await request(app)
.get(`/api/users/${maliciousInput}`)
.expect(400);
expect(response.body.error).toContain('Invalid input');
});
});
describe('XSS Protection', () => {
it('should sanitize HTML in user content', async () => {
const xssPayload = '';
const response = await request(app)
.post('/api/comments')
.send({ content: xssPayload })
.expect(201);
expect(response.body.content).not.toContain('