Skip to content

Commit 7969d17

Browse files
committed
fix(noteController): should check permission when user view note
Signed-off-by: BoHong Li <[email protected]>
1 parent ea52ed8 commit 7969d17

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/note/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,27 @@ async function showNote (req, res) {
7777
return responseCodiMD(res, note)
7878
}
7979

80+
function canViewNote (note, isLogin, userId) {
81+
if (note.permission === 'private') {
82+
return note.ownerId === userId
83+
}
84+
if (note.permission === 'limited' || note.permission === 'protected') {
85+
return isLogin
86+
}
87+
return true
88+
}
89+
8090
async function showPublishNote (req, res) {
8191
const shortid = req.params.shortid
8292

8393
const note = await getNoteById(shortid, {
8494
includeUser: true
8595
})
8696

97+
if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) {
98+
return errorForbidden(req)
99+
}
100+
87101
if (!note) {
88102
return errorNotFound(res)
89103
}
@@ -130,10 +144,15 @@ async function noteActions (req, res) {
130144
const noteId = req.params.noteId
131145

132146
const note = await getNoteById(noteId)
147+
133148
if (!note) {
134149
return errorNotFound(res)
135150
}
136151

152+
if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) {
153+
return errorForbidden(req)
154+
}
155+
137156
const action = req.params.action
138157
switch (action) {
139158
case 'publish':

0 commit comments

Comments
 (0)