Loading...
Loading...
Compare original and translation side by side
// ✅ CORRECT: Use withEmailAccount for email-scoped operations
export const GET = withEmailAccount(async (request, { params }) => {
const { emailAccountId } = request.auth;
// ...
});
// ✅ CORRECT: Use withAuth for user-scoped operations
export const GET = withAuth(async (request) => {
const { userId } = request.auth;
// ...
});
// ❌ WRONG: Direct access without authentication
export const GET = async (request) => {
// This exposes data to unauthenticated users!
const data = await prisma.user.findMany();
return NextResponse.json(data);
};// ✅ 正确:使用withEmailAccount处理邮箱账户范围的操作
export const GET = withEmailAccount(async (request, { params }) => {
const { emailAccountId } = request.auth;
// ...
});
// ✅ 正确:使用withAuth处理用户范围的操作
export const GET = withAuth(async (request) => {
const { userId } = request.auth;
// ...
});
// ❌ 错误:无身份验证直接访问
export const GET = async (request) => {
// 这会向未认证用户暴露数据!
const data = await prisma.user.findMany();
return NextResponse.json(data);
};// ✅ CORRECT: Always include user/account filtering
const schedule = await prisma.schedule.findUnique({
where: {
id: scheduleId,
emailAccountId // 🔒 Critical: Ensures user owns this resource
},
});
// ✅ CORRECT: Filter by user ownership
const rules = await prisma.rule.findMany({
where: {
emailAccountId, // 🔒 Only user's rules
enabled: true
},
});
// ❌ WRONG: Missing user/account filtering
const schedule = await prisma.schedule.findUnique({
where: { id: scheduleId }, // 🚨 Any user can access any schedule!
});// ✅ 正确:始终包含用户/账户过滤条件
const schedule = await prisma.schedule.findUnique({
where: {
id: scheduleId,
emailAccountId // 🔒 关键:确保用户拥有该资源
},
});
// ✅ 正确:按用户所有权过滤
const rules = await prisma.rule.findMany({
where: {
emailAccountId, // 🔒 仅返回该用户的规则
enabled: true
},
});
// ❌ 错误:缺少用户/账户过滤条件
const schedule = await prisma.schedule.findUnique({
where: { id: scheduleId }, // 🚨 任何用户都可以访问任何日程!
});// ✅ CORRECT: Validate ownership before operations
async function updateRule({ ruleId, emailAccountId, data }) {
const rule = await prisma.rule.findUnique({
where: {
id: ruleId,
emailAccount: { id: emailAccountId } // 🔒 Ownership check
},
});
if (!rule) throw new SafeError("Rule not found"); // Returns 404, doesn't leak existence
return prisma.rule.update({
where: { id: ruleId },
data,
});
}
// ❌ WRONG: Direct updates without ownership validation
async function updateRule({ ruleId, data }) {
return prisma.rule.update({
where: { id: ruleId }, // 🚨 User can modify any rule!
data,
});
}// ✅ 正确:在操作前验证所有权
async function updateRule({ ruleId, emailAccountId, data }) {
const rule = await prisma.rule.findUnique({
where: {
id: ruleId,
emailAccount: { id: emailAccountId } // 🔒 所有权校验
},
});
if (!rule) throw new SafeError("Rule not found"); // 返回404,不泄露资源存在性
return prisma.rule.update({
where: { id: ruleId },
data,
});
}
// ❌ 错误:无所有权验证直接更新
async function updateRule({ ruleId, data }) {
return prisma.rule.update({
where: { id: ruleId }, // 🚨 用户可以修改任何规则!
data,
});
}withEmailAccountwithEmailAccountemailAccountIdexport const GET = withEmailAccount(async (request) => {
const { emailAccountId, userId, email } = request.auth;
// All three fields available
});emailAccountIdexport const GET = withEmailAccount(async (request) => {
const { emailAccountId, userId, email } = request.auth;
// 三个字段均可用
});withAuthwithAuthuserIdexport const GET = withAuth(async (request) => {
const { userId } = request.auth;
// Only userId available
});userIdexport const GET = withAuth(async (request) => {
const { userId } = request.auth;
// 仅userId可用
});withErrorwithErrorhasCronSecret// ✅ CORRECT: Public endpoint with custom auth
export const GET = withError(async (request) => {
const session = await auth();
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
});
// ✅ CORRECT: Cron endpoint with secret validation
export const POST = withError(async (request) => {
if (!hasCronSecret(request)) {
captureException(new Error("Unauthorized cron request"));
return new Response("Unauthorized", { status: 401 });
}
// ... cron logic
});
// ❌ WRONG: Cron endpoint without validation
export const POST = withError(async (request) => {
// 🚨 Anyone can trigger this cron job!
await sendDigestEmails();
});hasCronSecret// ✅ 正确:带自定义认证的公开端点
export const GET = withError(async (request) => {
const session = await auth();
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
});
// ✅ 正确:带密钥验证的定时任务端点
export const POST = withError(async (request) => {
if (!hasCronSecret(request)) {
captureException(new Error("Unauthorized cron request"));
return new Response("Unauthorized", { status: 401 });
}
// ... 定时任务逻辑
});
// ❌ 错误:无验证的定时任务端点
export const POST = withError(async (request) => {
// 🚨 任何人都可以触发这个定时任务!
await sendDigestEmails();
});// ✅ CORRECT: GET cron endpoint
export const GET = withError(async (request) => {
if (!hasCronSecret(request)) {
captureException(new Error("Unauthorized cron request"));
return new Response("Unauthorized", { status: 401 });
}
// Safe to execute cron logic
await processScheduledTasks();
return NextResponse.json({ success: true });
});
// ✅ CORRECT: POST cron endpoint
export const POST = withError(async (request) => {
if (!(await hasPostCronSecret(request))) {
captureException(new Error("Unauthorized cron request"));
return new Response("Unauthorized", { status: 401 });
}
// Safe to execute cron logic
await processBulkOperations();
return NextResponse.json({ success: true });
});// ✅ 正确:GET类型定时任务端点
export const GET = withError(async (request) => {
if (!hasCronSecret(request)) {
captureException(new Error("Unauthorized cron request"));
return new Response("Unauthorized", { status: 401 });
}
// 安全执行定时任务逻辑
await processScheduledTasks();
return NextResponse.json({ success: true });
});
// ✅ 正确:POST类型定时任务端点
export const POST = withError(async (request) => {
if (!(await hasPostCronSecret(request))) {
captureException(new Error("Unauthorized cron request"));
return new Response("Unauthorized", { status: 401 });
}
// 安全执行定时任务逻辑
await processBulkOperations();
return NextResponse.json({ success: true });
});withErrorwithAuthwithEmailAccounthasCronSecret(request)hasPostCronSecret(request)captureException401withErrorwithAuthwithEmailAccounthasCronSecret(request)hasPostCronSecret(request)captureException401// Digest/summary emails
export const POST = withError(async (request) => {
if (!hasCronSecret(request)) {
captureException(new Error("Unauthorized cron request: digest"));
return new Response("Unauthorized", { status: 401 });
}
await sendDigestEmails();
});
// Cleanup operations
export const POST = withError(async (request) => {
if (!(await hasPostCronSecret(request))) {
captureException(new Error("Unauthorized cron request: cleanup"));
return new Response("Unauthorized", { status: 401 });
}
await cleanupExpiredData();
});
// System monitoring
export const GET = withError(async (request) => {
if (!hasCronSecret(request)) {
captureException(new Error("Unauthorized cron request: monitor"));
return new Response("Unauthorized", { status: 401 });
}
await monitorSystemHealth();
});// 摘要/汇总邮件
export const POST = withError(async (request) => {
if (!hasCronSecret(request)) {
captureException(new Error("Unauthorized cron request: digest"));
return new Response("Unauthorized", { status: 401 });
}
await sendDigestEmails();
});
// 清理操作
export const POST = withError(async (request) => {
if (!(await hasPostCronSecret(request))) {
captureException(new Error("Unauthorized cron request: cleanup"));
return new Response("Unauthorized", { status: 401 });
}
await cleanupExpiredData();
});
// 系统监控
export const GET = withError(async (request) => {
if (!hasCronSecret(request)) {
captureException(new Error("Unauthorized cron request: monitor"));
return new Response("Unauthorized", { status: 401 });
}
await monitorSystemHealth();
});CRON_SECRETundefinedCRON_SECRETundefined
**⚠️ Never use predictable cron secrets like:**
- `"secret"`
- `"password"`
- `"cron"`
- Short or simple strings
---
**⚠️ 绝对不要使用可预测的定时任务密钥,例如:**
- `"secret"`
- `"password"`
- `"cron"`
- 短或简单的字符串
---// User-scoped queries
const user = await prisma.user.findUnique({
where: { id: userId },
select: { id: true, email: true } // Only return needed fields
});
// Email account-scoped queries
const emailAccount = await prisma.emailAccount.findUnique({
where: { id: emailAccountId, userId }, // Double validation
});
// Related resource queries with ownership
const rule = await prisma.rule.findUnique({
where: {
id: ruleId,
emailAccount: { id: emailAccountId }
},
include: { actions: true }
});
// Filtered list queries
const schedules = await prisma.schedule.findMany({
where: { emailAccountId },
orderBy: { createdAt: 'desc' }
});// 用户范围查询
const user = await prisma.user.findUnique({
where: { id: userId },
select: { id: true, email: true } // 仅返回所需字段
});
// 邮箱账户范围查询
const emailAccount = await prisma.emailAccount.findUnique({
where: { id: emailAccountId, userId }, // 双重验证
});
// 带所有权校验的关联资源查询
const rule = await prisma.rule.findUnique({
where: {
id: ruleId,
emailAccount: { id: emailAccountId }
},
include: { actions: true }
});
// 过滤后的列表查询
const schedules = await prisma.schedule.findMany({
where: { emailAccountId },
orderBy: { createdAt: 'desc' }
});// Missing user scoping
const schedules = await prisma.schedule.findMany(); // 🚨 Returns ALL schedules
// Missing ownership validation
const rule = await prisma.rule.findUnique({
where: { id: ruleId } // 🚨 Can access any user's rule
});
// Exposed sensitive fields
const user = await prisma.user.findUnique({
where: { id: userId }
// 🚨 Returns ALL fields including sensitive data
});
// Direct parameter usage
const userId = request.nextUrl.searchParams.get('userId');
const user = await prisma.user.findUnique({
where: { id: userId } // 🚨 User can access any user by changing URL
});// 缺少用户范围限定
const schedules = await prisma.schedule.findMany(); // 🚨 返回所有日程
// 缺少所有权验证
const rule = await prisma.rule.findUnique({
where: { id: ruleId } // 🚨 可以访问任何用户的规则
});
// 暴露敏感字段
const user = await prisma.user.findUnique({
where: { id: userId }
// 🚨 返回所有字段,包括敏感数据
});
// 直接使用参数
const userId = request.nextUrl.searchParams.get('userId');
const user = await prisma.user.findUnique({
where: { id: userId } // 🚨 用户可通过修改URL访问任何用户数据
});// ✅ CORRECT: Validate all inputs
export const GET = withEmailAccount(async (request, { params }) => {
const { id } = await params;
if (!id) {
return NextResponse.json(
{ error: "Missing schedule ID" },
{ status: 400 }
);
}
// Additional validation
if (typeof id !== 'string' || id.length < 10) {
return NextResponse.json(
{ error: "Invalid schedule ID format" },
{ status: 400 }
);
}
});
// ❌ WRONG: Using parameters without validation
export const GET = withEmailAccount(async (request, { params }) => {
const { id } = await params;
// 🚨 Direct usage without validation
const schedule = await prisma.schedule.findUnique({ where: { id } });
});// ✅ 正确:验证所有输入
export const GET = withEmailAccount(async (request, { params }) => {
const { id } = await params;
if (!id) {
return NextResponse.json(
{ error: "缺少日程ID" },
{ status: 400 }
);
}
// 额外验证
if (typeof id !== 'string' || id.length < 10) {
return NextResponse.json(
{ error: "无效的日程ID格式" },
{ status: 400 }
);
}
});
// ❌ 错误:未验证直接使用参数
export const GET = withEmailAccount(async (request, { params }) => {
const { id } = await params;
// 🚨 直接使用未验证的参数
const schedule = await prisma.schedule.findUnique({ where: { id } });
});// ✅ CORRECT: Always validate request bodies
const updateRuleSchema = z.object({
name: z.string().min(1).max(100),
enabled: z.boolean(),
conditions: z.array(z.object({
type: z.enum(['FROM', 'SUBJECT', 'BODY']),
value: z.string().min(1)
}))
});
export const PUT = withEmailAccount(async (request) => {
const body = await request.json();
const validatedData = updateRuleSchema.parse(body); // Throws on invalid data
// Use validatedData, not body
});// ✅ 正确:始终验证请求体
const updateRuleSchema = z.object({
name: z.string().min(1).max(100),
enabled: z.boolean(),
conditions: z.array(z.object({
type: z.enum(['FROM', 'SUBJECT', 'BODY']),
value: z.string().min(1)
}))
});
export const PUT = withEmailAccount(async (request) => {
const body = await request.json();
const validatedData = updateRuleSchema.parse(body); // 数据无效时抛出错误
// 使用validatedData而非原始body
});// ✅ CORRECT: Safe error responses
if (!rule) {
throw new SafeError("Rule not found"); // Generic 404
}
if (!hasPermission) {
throw new SafeError("Access denied"); // Generic 403
}
// ❌ WRONG: Information disclosure
if (!rule) {
throw new Error(`Rule ${ruleId} does not exist for user ${userId}`);
// 🚨 Reveals internal IDs and logic
}
if (!rule.emailAccountId === emailAccountId) {
throw new Error("This rule belongs to a different account");
// 🚨 Confirms existence of rule and reveals ownership info
}// ✅ 正确:安全的错误响应
if (!rule) {
throw new SafeError("Rule not found"); // 通用404错误
}
if (!hasPermission) {
throw new SafeError("Access denied"); // 通用403错误
}
// ❌ 错误:信息泄露
if (!rule) {
throw new Error(`Rule ${ruleId} does not exist for user ${userId}`);
// 🚨 泄露内部ID和逻辑
}
if (!rule.emailAccountId === emailAccountId) {
throw new Error("This rule belongs to a different account");
// 🚨 确认规则存在并泄露所有权信息
}// ✅ CORRECT: Consistent error format
export const GET = withEmailAccount(async (request) => {
try {
// ... operation
} catch (error) {
if (error instanceof SafeError) {
return NextResponse.json(
{ error: error.message, isKnownError: true },
{ status: error.statusCode || 400 }
);
}
// Let middleware handle unexpected errors
throw error;
}
});// ✅ 正确:统一的错误格式
export const GET = withEmailAccount(async (request) => {
try {
// ... 操作逻辑
} catch (error) {
if (error instanceof SafeError) {
return NextResponse.json(
{ error: error.message, isKnownError: true },
{ status: error.statusCode || 400 }
);
}
// 让中间件处理意外错误
throw error;
}
});// ❌ VULNERABLE: User can access any rule by changing ID
export const GET = async (request, { params }) => {
const { ruleId } = await params;
const rule = await prisma.rule.findUnique({ where: { id: ruleId } });
return NextResponse.json(rule);
};
// ✅ SECURE: Always validate ownership
export const GET = withEmailAccount(async (request, { params }) => {
const { emailAccountId } = request.auth;
const { ruleId } = await params;
const rule = await prisma.rule.findUnique({
where: {
id: ruleId,
emailAccount: { id: emailAccountId } // 🔒 Ownership validation
}
});
if (!rule) throw new SafeError("Rule not found");
return NextResponse.json(rule);
});// ❌ 存在漏洞:用户可通过修改ID访问任何规则
export const GET = async (request, { params }) => {
const { ruleId } = await params;
const rule = await prisma.rule.findUnique({ where: { id: ruleId } });
return NextResponse.json(rule);
};
// ✅ 安全:始终验证所有权
export const GET = withEmailAccount(async (request, { params }) => {
const { emailAccountId } = request.auth;
const { ruleId } = await params;
const rule = await prisma.rule.findUnique({
where: {
id: ruleId,
emailAccount: { id: emailAccountId } // 🔒 所有权验证
}
});
if (!rule) throw new SafeError("Rule not found");
return NextResponse.json(rule);
});// ❌ VULNERABLE: User can modify any field
export const PUT = withEmailAccount(async (request) => {
const body = await request.json();
const rule = await prisma.rule.update({
where: { id: body.id },
data: body // 🚨 User controls all fields, including ownership!
});
});
// ✅ SECURE: Explicitly allow only safe fields
const updateSchema = z.object({
name: z.string(),
enabled: z.boolean(),
// Only allow specific fields
});
export const PUT = withEmailAccount(async (request) => {
const body = await request.json();
const validatedData = updateSchema.parse(body);
const rule = await prisma.rule.update({
where: {
id: ruleId,
emailAccount: { id: emailAccountId } // Maintain ownership
},
data: validatedData // Only validated fields
});
});// ❌ 存在漏洞:用户可修改任何字段
export const PUT = withEmailAccount(async (request) => {
const body = await request.json();
const rule = await prisma.rule.update({
where: { id: body.id },
data: body // 🚨 用户控制所有字段,包括所有权!
});
});
// ✅ 安全:仅显式允许安全字段
const updateSchema = z.object({
name: z.string(),
enabled: z.boolean(),
// 仅允许特定字段
});
export const PUT = withEmailAccount(async (request) => {
const body = await request.json();
const validatedData = updateSchema.parse(body);
const rule = await prisma.rule.update({
where: {
id: ruleId,
emailAccount: { id: emailAccountId } // 保持所有权校验
},
data: validatedData // 仅使用已验证的字段
});
});// ❌ VULNERABLE: User can modify admin-only fields
const rule = await prisma.rule.update({
where: { id: ruleId },
data: {
...updateData,
// 🚨 What if updateData contains system fields?
ownerId: 'different-user-id', // User changes ownership!
systemGenerated: false, // User modifies system flags!
}
});
// ✅ SECURE: Whitelist approach
const allowedFields = {
name: updateData.name,
enabled: updateData.enabled,
instructions: updateData.instructions,
// Only explicitly allowed fields
};
const rule = await prisma.rule.update({
where: {
id: ruleId,
emailAccount: { id: emailAccountId }
},
data: allowedFields
});// ❌ 存在漏洞:用户可修改管理员专属字段
const rule = await prisma.rule.update({
where: { id: ruleId },
data: {
...updateData,
// 🚨 如果updateData包含系统字段会怎样?
ownerId: 'different-user-id', // 用户更改所有权!
systemGenerated: false, // 用户修改系统标志!
}
});
// ✅ 安全:白名单方式
const allowedFields = {
name: updateData.name,
enabled: updateData.enabled,
instructions: updateData.instructions,
// 仅显式允许的字段
};
const rule = await prisma.rule.update({
where: {
id: ruleId,
emailAccount: { id: emailAccountId }
},
data: allowedFields
});// ❌ VULNERABLE: Anyone can trigger cron operations
export const POST = withError(async (request) => {
// 🚨 No authentication - anyone can send digest emails!
await sendDigestEmailsToAllUsers();
return NextResponse.json({ success: true });
});
// ❌ VULNERABLE: Weak cron validation
export const POST = withError(async (request) => {
const body = await request.json();
if (body.secret !== "simple-password") { // 🚨 Predictable secret
return new Response("Unauthorized", { status: 401 });
}
await performSystemMaintenance();
});
// ✅ SECURE: Proper cron authentication
export const POST = withError(async (request) => {
if (!hasCronSecret(request)) { // 🔒 Strong secret validation
captureException(new Error("Unauthorized cron request"));
return new Response("Unauthorized", { status: 401 });
}
await performSystemMaintenance();
});// ❌ 存在漏洞:任何人都可以触发定时任务操作
export const POST = withError(async (request) => {
// 🚨 无身份验证 - 任何人都可以发送摘要邮件!
await sendDigestEmailsToAllUsers();
return NextResponse.json({ success: true });
});
// ❌ 存在漏洞:弱定时任务验证
export const POST = withError(async (request) => {
const body = await request.json();
if (body.secret !== "simple-password") { // 🚨 可预测的密钥
return new Response("Unauthorized", { status: 401 });
}
await performSystemMaintenance();
});
// ✅ 安全:正确的定时任务身份验证
export const POST = withError(async (request) => {
if (!hasCronSecret(request)) { // 🔒 强密钥验证
captureException(new Error("Unauthorized cron request"));
return new Response("Unauthorized", { status: 401 });
}
await performSystemMaintenance();
});withAuthwithEmailAccountwithErrorhasCronSecret()hasPostCronSecret()withAuthwithEmailAccountwithErrorhasCronSecret()hasPostCronSecret()findUniquefindFirstfindManyfindUniquefindFirstfindManyapps/web/app/api/user/frequency/[id]/route.tsexport const GET = withEmailAccount(async (request, { params }) => {
const emailAccountId = request.auth.emailAccountId;
const { id } = await params;
if (!id) return NextResponse.json({ error: "Missing frequency id" }, { status: 400 });
const schedule = await prisma.schedule.findUnique({
where: { id, emailAccountId }, // 🔒 Scoped to user's account
});
if (!schedule) {
return NextResponse.json({ error: "Schedule not found" }, { status: 404 });
}
return NextResponse.json(schedule);
});apps/web/app/api/user/rules/[id]/route.tsconst rule = await prisma.rule.findUnique({
where: {
id: ruleId,
emailAccount: { id: emailAccountId } // 🔒 Relationship-based ownership check
},
include: { actions: true, categoryFilters: true },
});apps/web/app/api/user/frequency/[id]/route.tsexport const GET = withEmailAccount(async (request, { params }) => {
const emailAccountId = request.auth.emailAccountId;
const { id } = await params;
if (!id) return NextResponse.json({ error: "Missing frequency id" }, { status: 400 });
const schedule = await prisma.schedule.findUnique({
where: { id, emailAccountId }, // 🔒 限定在用户账户范围内
});
if (!schedule) {
return NextResponse.json({ error: "Schedule not found" }, { status: 404 });
}
return NextResponse.json(schedule);
});apps/web/app/api/user/rules/[id]/route.tsconst rule = await prisma.rule.findUnique({
where: {
id: ruleId,
emailAccount: { id: emailAccountId } // 🔒 基于关联关系的所有权校验
},
include: { actions: true, categoryFilters: true },
});describe("Security Tests", () => {
it("should not allow access without authentication", async () => {
const response = await request.get("/api/user/rules/123");
expect(response.status).toBe(401);
});
it("should not allow access to other users' resources", async () => {
const response = await request
.get("/api/user/rules/other-user-rule-id")
.set("Authorization", "Bearer valid-token")
.set("X-Email-Account-ID", "user-account-id");
expect(response.status).toBe(404); // Not 403, to avoid info disclosure
});
});describe("Security Tests", () => {
it("should not allow access without authentication", async () => {
const response = await request.get("/api/user/rules/123");
expect(response.status).toBe(401);
});
it("should not allow access to other users' resources", async () => {
const response = await request
.get("/api/user/rules/other-user-rule-id")
.set("Authorization", "Bearer valid-token")
.set("X-Email-Account-ID", "user-account-id");
expect(response.status).toBe(404); // 避免返回403以防止信息泄露
});
});