Skip to content

Node.js based forum software built for the modern web

fix: dont allow creating custom fields that overwrite

Barış Soner Uşaklı84483ef51988
diff --git a/install/package.json b/install/package.json
index e082c93394..f368319fad 100644
--- a/install/package.json
+++ b/install/package.json
@@ -108,7 +108,7 @@
"nodebb-plugin-emoji-android": "4.1.1",
"nodebb-plugin-link-preview": "2.2.8",
"nodebb-plugin-markdown": "13.2.6",
- "nodebb-plugin-mentions": "5.0.1",
+ "nodebb-plugin-mentions": "5.0.2",
"nodebb-plugin-spam-be-gone": "2.3.4",
"nodebb-plugin-web-push": "0.8.1",
"nodebb-rewards-essentials": "1.0.3",
diff --git a/src/socket.io/admin/user.js b/src/socket.io/admin/user.js
index 6b5cf738a2..096e498aff 100644
--- a/src/socket.io/admin/user.js
+++ b/src/socket.io/admin/user.js
@@ -157,9 +157,12 @@ User.exportUsersCSV = async function (socket, data) {
};
User.saveCustomFields = async function (socket, fields) {
- const userFields = await user.getUserFieldWhitelist();
+ const protectedFields = [
+ ...await user.getUserFieldWhitelist(),
+ ...user.protectedFields,
+ ];
for (const field of fields) {
- if (userFields.includes(field.key) || userFields.includes(field.key.toLowerCase())) {
+ if (protectedFields.includes(field.key) || protectedFields.includes(field.key.toLowerCase())) {
throw new Error(`[[error:invalid-custom-user-field, ${field.key}]]`);
}
}
diff --git a/src/user/data.js b/src/user/data.js
index b8629fdb00..ffa5ddf7a7 100644
--- a/src/user/data.js
+++ b/src/user/data.js
@@ -35,6 +35,11 @@ module.exports = function (User) {
'cover:position', 'groupTitle', 'muted', 'mutedUntil', 'mutedReason',
];
+ User.protectedFields = [
+ '_id', '_key', 'password', 'password:shaWrapped', 'passwordExpiry',
+ 'lastqueuetime', 'rss_token', 'blocksCount', 'gdpr_consent',
+ ];
+
let customFieldWhiteList = null;
const urlFieldList = [
diff --git a/src/user/profile.js b/src/user/profile.js
index b4915d65c1..fdee1f7131 100644
--- a/src/user/profile.js
+++ b/src/user/profile.js
@@ -22,8 +22,9 @@ module.exports = function (User) {
...await db.getSortedSetRange('user-custom-fields', 0, -1),
];
if (Array.isArray(extraFields)) {
- fields = _.uniq(fields.concat(extraFields));
+ fields = fields.concat(extraFields);
}
+ fields = _.uniq(fields).filter(field => !User.protectedFields.includes(field));
if (!data.uid) {
throw new Error('[[error:invalid-update-uid]]');
}
diff --git a/test/user/custom-fields.js b/test/user/custom-fields.js
index 8a637f49a2..8c662d7311 100644
--- a/test/user/custom-fields.js
+++ b/test/user/custom-fields.js
@@ -37,6 +37,23 @@ describe('custom user fields', () => {
await adminUser.saveCustomFields({ uid: adminUid }, fields);
});
+ it('should fail to create custom user fields for reserved fields', async () => {
+ const protectedFields = [
+ ...await user.getUserFieldWhitelist(),
+ ...user.protectedFields,
+ ];
+ for (const field of protectedFields) {
+ // eslint-disable-next-line no-await-in-loop
+ await assert.rejects(
+ adminUser.saveCustomFields({ uid: adminUid }, [
+ { key: field },
+ ]),
+ { message: `[[error:invalid-custom-user-field, ${field}]]` },
+ `Failed for ${field}`
+ );
+ }
+ });
+
it('should fail to update a field if user does not have enough reputation', async () => {
await assert.rejects(
user.updateProfile(lowRepUid, {