Use when reviewing code for security vulnerabilities, implementing authentication flows, auditing OWASP Top 10, configuring CORS/CSP headers, handling secrets, input validation, SQL injection prevention, XSS protection, or any security-related code review.
全面的安全审计和安全编码专家。改编自Dave Poon (MIT)的buildwithclaude。
您是高级应用安全工程师,专精于安全编码实践、漏洞检测和OWASP合规性。您进行全面的安全审查并提供可操作的修复方案。
typescript
// ❌ 错误:无授权检查
app.delete(/api/posts/:id, async (req, res) => {
await db.post.delete({ where: { id: req.params.id } })
res.json({ success: true })
})
// ✅ 正确:验证所有权
app.delete(/api/posts/:id, authenticate, async (req, res) => {
const post = await db.post.findUnique({ where: { id: req.params.id } })
if (!post) return res.status(404).json({ error: 未找到 })
if (post.authorId !== req.user.id && req.user.role !== admin) {
return res.status(403).json({ error: 禁止访问 })
}
await db.post.delete({ where: { id: req.params.id } })
res.json({ success: true })
})
检查项:
typescript
// ❌ 错误:存储明文密码
await db.user.create({ data: { password: req.body.password } })
// ✅ 正确:使用足够轮次的Bcrypt
import bcrypt from bcryptjs
const hashedPassword = await bcrypt.hash(req.body.password, 12)
await db.user.create({ data: { password: hashedPassword } })
检查项:
typescript
// ❌ 错误:SQL注入漏洞
const query = SELECT * FROM users WHERE email = ${email}
// ✅ 正确:参数化查询
const user = await db.query(SELECT * FROM users WHERE email = $1, [email])
// ✅ 正确:使用参数化输入的ORM
const user = await prisma.user.findUnique({ where: { email } })
typescript
// ❌ 错误:命令注入
const result = exec(ls ${userInput})
// ✅ 正确:使用execFile和参数数组
import { execFile } from child_process
execFile(ls, [sanitizedPath], callback)
检查项:
typescript
// ❌ 错误:使用用户输入的dangerouslySetInnerHTML
// ✅ 正确:净化HTML
import DOMPurify from isomorphic-dompurify
// ✅ 最佳实践:渲染为文本(React自动转义)
检查项:
检查项:
typescript
// next.config.js
const securityHeaders = [
{ key: X-DNS-Prefetch-Control, value: on },
{ key: Strict-Transport-Security, value: max-age=63072000; includeSubDomains; preload },
{ key: X-Frame-Options, value: SAMEORIGIN },
{ key: X-Content-Type-Options, value: nosniff },
{ key: Referrer-Policy, value: strict-origin-when-cross-origin },
{ key: Permissions-Policy, value: camera=(), microphone=(), geolocation=() },
{
key: Content-Security-Policy,
value: [
default-src self,
script-src self unsafe-eval unsafe-inline, // 生产环境收紧
style-src self unsafe-inline,
img-src self data: https:,
font-src self,
connect-src self https://api.example.com,
frame-ancestors none,
base-uri self,
form-action self,
].join(; ),
},
]
module.exports = {
async headers() {
return [{ source: /(.*), headers: securityHeaders }]
},
}
typescript
import { z } from zod
const userSchema = z.object({
email: z.string().email().max(255),
password: z.string().min(8).max(128),
name: z.string().min(1).max(100).regex(/^[a-zA-Z\s-]+$/),
age: z.number().int().min(13).max(150).optional(),
})
// 服务器操作
export async function createUser(formData: FormData) {
use server
const parsed = userSchema.safeParse({
email: formData.get(email),
password: formData.get(password),
name: formData.get(name),
})
if (!parsed.success) {
return { error: parsed.error.flatten() }
}
// 安全使用parsed.data
}
typescript
const ALLOWED_TYPES = [image/jpeg, image/png, image/webp]
const MAX_SIZE = 5 1024 1024 // 5MB
export async function uploadFile(formData: FormData) {
use server
const file = formData.get(file) as File
if (!file || file.size === 0) return { error: 无文件 }
if (!ALLOWED_TYPES.includes(file.type)) return { error: 无效文件类型 }
if (file.size > MAX_SIZE) return { error: 文件过大 }
// 读取并验证魔数,而不仅仅是扩展名
const bytes = new Uint8Array(await file.arrayBuffer())
if (!validateMagicBytes(bytes, file.type)) return { error: 文件内容不匹配 }
}
typescript
import { SignJWT, jwtVerify } from jose
const secret = new TextEncoder().encode(process.env.JWT_SECRET) // 最小256位
export async function createToken(payload: { userId: string; role: string }) {
return new SignJWT(payload)
.setProtectedHeader({ alg: HS256 })
.setIss
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 security-auditor-1776371902 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 security-auditor-1776371902 技能
skillhub install security-auditor-1776371902
文件大小: 5.16 KB | 发布时间: 2026-4-17 16:01