[英]Creating deeply nested object in Prisma securely
我正在使用具有以下數據結構的 Prisma 和 Nextjs,並使用 Next-Auth 進行身份驗證。
user
|-->profile
|-->log
|-->sublog
現在,CRUD 通過 Nextjs 上的 API 路由發送到數據庫。 我想通過 API 安全地寫入sublog
。
所以當我寫這篇文章時,它是開放式的:
const sublog = await prisma.sublog.create({
data: {
name: req.body.name,
content: req.body.content,
log: {
connect: {
id: req.body.logId,
}
}
}
})
我可以從前端和后端訪問用戶會話以獲取用戶 ID。 但是我不確定如何使表單提交安全,只有擁有log
的用戶才能被允許提交sublog
。
關於如何在深度嵌套時安全地提交內容的任何想法?
PS 請注意,我可以打開和關閉任何在前端編輯/刪除數據的組件——但這只是在前端,我想在 API 上保護它,這樣即使客戶端能夠以某種方式訪問表單中的表單不屬於他們的log
,它仍然會從 API 推送錯誤,因為客戶端不屬於那里。
在允許執行prisma.sublog.create
之前,您需要進行 prisma 查詢來檢查誰擁有log
。 Prisma 與所有權的概念無關——您需要自己添加並檢查該邏輯。
const fullLog = await prisma.log.findUnique({
select: { // don't know what your model looks like, just guessing
id: true,
profile: {
select: {
userId: true
}
}
},
where: {
id: req.body.logId
}
});
// currentUserId = however you get the current user's id
if (fullLog && fullLog.profile.userId !== currentUserId) {
// throw an error
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.