Skip to content

Node.js based forum software built for the modern web

fix: dont mark nids that you don't own read/unread

Barış Soner Uşaklı001e45d4ce64
diff --git a/src/notifications.js b/src/notifications.js
index 0a108ff03b..095665683e 100644
--- a/src/notifications.js
+++ b/src/notifications.js
@@ -401,8 +401,11 @@ Notifications.markUnread = async function (nid, uid) {
if (!(parseInt(uid, 10) > 0) || !nid) {
return;
}
- const notification = await db.getObject(`notifications:${nid}`);
- if (!notification) {
+ const [notification, [userOwns]] = await Promise.all([
+ db.getObject(`notifications:${nid}`),
+ User.notifications.ownsNids([nid], uid),
+ ]);
+ if (!notification || !userOwns) {
throw new Error('[[error:no-notification]]');
}
notification.datetime = notification.datetime || Date.now();
@@ -418,7 +421,10 @@ Notifications.markReadMultiple = async function (nids, uid) {
if (!Array.isArray(nids) || !nids.length || !(parseInt(uid, 10) > 0)) {
return;
}
-
+ const userOwns = await User.notifications.ownsNids(nids, uid);
+ if (userOwns.some(owns => !owns)) {
+ throw new Error('[[error:no-notification]]');
+ }
let notificationKeys = nids.map(nid => `notifications:${nid}`);
let mergeIds = await db.getObjectsFields(notificationKeys, ['mergeId']);
// Isolate mergeIds and find related notifications
diff --git a/src/user/notifications.js b/src/user/notifications.js
index b56668b21f..03b651953c 100644
--- a/src/user/notifications.js
+++ b/src/user/notifications.js
@@ -98,6 +98,14 @@ async function getNotificationsFromSet(set, uid, start, stop) {
return await UserNotifications.getNotifications(nids, uid);
}
+UserNotifications.ownsNids = async function (nids, uid) {
+ const [isInRead, isInUnread] = await Promise.all([
+ db.isSortedSetMembers(`uid:${uid}:notifications:read`, nids),
+ db.isSortedSetMembers(`uid:${uid}:notifications:unread`, nids),
+ ]);
+ return nids.map((nid, index) => (isInRead[index] || isInUnread[index]));
+};
+
UserNotifications.getNotifications = async function (nids, uid) {
if (!Array.isArray(nids) || !nids.length) {
return [];
diff --git a/test/notifications.js b/test/notifications.js
index 3730a9886c..8b29abc55b 100644
--- a/test/notifications.js
+++ b/test/notifications.js
@@ -13,6 +13,7 @@ const categories = require('../src/categories');
const notifications = require('../src/notifications');
const socketNotifications = require('../src/socket.io/notifications');
const api = require('../src/api');
+const utils = require('../src/utils');
const sleep = util.promisify(setTimeout);
@@ -224,6 +225,19 @@ describe('Notifications', () => {
assert.deepStrictEqual(notifObj, { notification: undefined });
});
+ it('should not mark unread/read if notification does not belong to user', async () => {
+ const uid = await user.create({ username: utils.generateUUID().slice(0, 8) });
+ await assert.rejects(
+ api.notifications.markUnread({ uid: uid }, { nid: notification.nid }),
+ { message: '[[error:no-notification]]' },
+ );
+
+ await assert.rejects(
+ api.notifications.markRead({ uid: uid }, { nid: notification.nid }),
+ { message: '[[error:no-notification]]' },
+ );
+ });
+
it('should get user\'s notifications', async () => {
const data = await socketNotifications.get({ uid: uid }, {});
assert.equal(data.unread.length, 0);