Skip to content

Node.js based forum software built for the modern web

fix: three ways a custom profile field could not be saved (#14846)

palmoni5974fefd78e00
diff --git a/src/controllers/accounts/helpers.js b/src/controllers/accounts/helpers.js
index b51a8f175b..d87bc56bc8 100644
--- a/src/controllers/accounts/helpers.js
+++ b/src/controllers/accounts/helpers.js
@@ -190,7 +190,11 @@ helpers.getCustomUserFields = async function (callerUID, userData) {
fields.forEach((f) => {
let userValue = userData[f.key];
if (f.type === 'select-multi' && userValue) {
- userValue = JSON.parse(userValue || '[]');
+ try {
+ userValue = JSON.parse(userValue);
+ } catch (err) {
+ userValue = [];
+ }
}
if (f.type === 'input-link' && userValue) {
userValue = utils.isSafeHref(userValue) ? userValue : '';
diff --git a/src/user/profile.js b/src/user/profile.js
index 58a84ccefc..eba92093d8 100644
--- a/src/user/profile.js
+++ b/src/user/profile.js
@@ -116,7 +116,7 @@ module.exports = function (User) {
require_tld: true,
});
- if (type === 'input-number' && !utils.isNumber(value)) {
+ if (value && type === 'input-number' && !utils.isNumber(value)) {
throw new Error(tx.compile(
'error:custom-user-field-invalid-number', field.name
));
@@ -133,15 +133,20 @@ module.exports = function (User) {
'error:custom-user-field-invalid-link', field.name
));
} else if (field.type === 'select') {
- const opts = field['select-options'].split('\n').filter(Boolean);
+ const opts = (field['select-options'] || '').split('\n').filter(Boolean);
if (!opts.includes(value) && value !== '') {
throw new Error(tx.compile(
'error:custom-user-field-select-value-invalid', field.name
));
}
} else if (field.type === 'select-multi') {
- const opts = field['select-options'].split('\n').filter(Boolean);
- const values = JSON.parse(value || '[]');
+ const opts = (field['select-options'] || '').split('\n').filter(Boolean);
+ let values;
+ try {
+ values = JSON.parse(value || '[]');
+ } catch (err) {
+ values = null;
+ }
if (!Array.isArray(values) || !values.every(value => opts.includes(value))) {
throw new Error(tx.compile(
'error:custom-user-field-select-value-invalid', field.name
diff --git a/test/user.js b/test/user.js
index 931494b022..352501fc0c 100644
--- a/test/user.js
+++ b/test/user.js
@@ -2068,6 +2068,55 @@ describe('User', () => {
});
});
+ describe('custom profile fields', () => {
+ let fieldUid;
+ const keys = ['favnumber', 'favcolour', 'langs'];
+
+ before(async () => {
+ fieldUid = await User.create({ username: 'customfielduser' });
+ await db.sortedSetAdd('user-custom-fields', [0, 1, 2], keys);
+ await db.setObjectBulk([
+ ['user-custom-field:favnumber', { key: 'favnumber', name: 'Favourite number', type: 'input-number' }],
+ ['user-custom-field:favcolour', { key: 'favcolour', name: 'Favourite colour', type: 'select' }],
+ ['user-custom-field:langs', { key: 'langs', name: 'Languages', type: 'select-multi' }],
+ ]);
+ await User.reloadCustomFieldWhitelist();
+ });
+
+ after(async () => {
+ await db.delete('user-custom-fields');
+ await db.deleteAll(keys.map(k => `user-custom-field:${k}`));
+ await User.reloadCustomFieldWhitelist();
+ });
+
+ it('should allow a numeric field to be cleared', async () => {
+ await User.updateProfile(fieldUid, { uid: fieldUid, favnumber: '7' });
+ assert.strictEqual(await User.getUserField(fieldUid, 'favnumber'), '7');
+
+ await User.updateProfile(fieldUid, { uid: fieldUid, favnumber: '' });
+ assert.strictEqual(await User.getUserField(fieldUid, 'favnumber'), '');
+ });
+
+ it('should still reject a non-numeric value', async () => {
+ await assert.rejects(
+ User.updateProfile(fieldUid, { uid: fieldUid, favnumber: 'seven' }),
+ { message: '[[error:custom-user-field-invalid-number, Favourite number]]' }
+ );
+ });
+
+ it('should not throw when a select field has no options configured', async () => {
+ await User.updateProfile(fieldUid, { uid: fieldUid, favcolour: '' });
+ assert.strictEqual(await User.getUserField(fieldUid, 'favcolour'), '');
+ });
+
+ it('should reject a select-multi value that is not valid json', async () => {
+ await assert.rejects(
+ User.updateProfile(fieldUid, { uid: fieldUid, langs: 'not json' }),
+ { message: '[[error:custom-user-field-select-value-invalid, Languages]]' }
+ );
+ });
+ });
+
describe('invites', () => {
let notAnInviterUid;
let inviterUid;