Skip to content

Node.js based forum software built for the modern web

fix: dont set invalid strings into notificationType_ properties

Barış Soner Uşaklıc62bdddd2faf
diff --git a/src/user/settings.js b/src/user/settings.js
index f299482dc7..c7f69f4849 100644
--- a/src/user/settings.js
+++ b/src/user/settings.js
@@ -21,6 +21,8 @@ module.exports = function (User) {
categoryWatchState: 'notwatching',
});
+ const validNotificationTypeValues = ['none', 'notification', 'email', 'notificationemail'];
+
User.getSettings = async function (uid) {
if (parseInt(uid, 10) <= 0) {
const isSpider = parseInt(uid, 10) === -1;
@@ -96,6 +98,9 @@ module.exports = function (User) {
const notificationTypes = await notifications.getAllNotificationTypes();
notificationTypes.forEach((notificationType) => {
settings[notificationType] = getSetting(settings, notificationType, 'notification');
+ if (!validNotificationTypeValues.includes(settings[notificationType])) {
+ settings[notificationType] = 'notification';
+ }
});
settings.chatAllowList = parseJSONArray(settings.chatAllowList || '[]', []);
@@ -159,7 +164,7 @@ module.exports = function (User) {
const notificationTypes = await notifications.getAllNotificationTypes();
notificationTypes.forEach((notificationType) => {
- if (data[notificationType]) {
+ if (Object.hasOwn(data, notificationType) && validNotificationTypeValues.includes(data[notificationType])) {
settings[notificationType] = data[notificationType];
}
});
@@ -204,6 +209,13 @@ module.exports = function (User) {
} catch (err) {
throw new Error('[[error:invalid-data]]');
}
+
+ const notificationTypes = await notifications.getAllNotificationTypes();
+ notificationTypes.forEach((notificationType) => {
+ if (Object.hasOwn(data, notificationType) && !validNotificationTypeValues.includes(data[notificationType])) {
+ throw new Error('[[error:invalid-notification-type]]');
+ }
+ });
}
User.updateDigestSetting = async function (uid, dailyDigestFreq) {
diff --git a/test/user.js b/test/user.js
index 5512fde32f..541bff08cf 100644
--- a/test/user.js
+++ b/test/user.js
@@ -1791,6 +1791,19 @@ describe('User', () => {
}
});
+ it('should error if notificationType_upvote is invalid', async () => {
+ const data = {
+ uid: testUid,
+ settings: {
+ notificationType_upvote: '<invalid-string>',
+ },
+ };
+ await assert.rejects(
+ apiUser.updateSettings({ uid: testUid }, data),
+ { message: '[[error:invalid-notification-type]]' }
+ );
+ });
+
it('should set moderation note', async () => {
const adminUid = await User.create({ username: 'noteadmin' });
await groups.join('administrators', adminUid);