Skip to content

Comments

[IMPROVE] Add error messages to the creation of channels or usernames containing reserved words#21016

Merged
sampaiodiego merged 20 commits intodevelopfrom
reserved-words
Apr 20, 2021
Merged

[IMPROVE] Add error messages to the creation of channels or usernames containing reserved words#21016
sampaiodiego merged 20 commits intodevelopfrom
reserved-words

Conversation

@matheusbsilva137
Copy link
Contributor

Proposed changes (including videos or screenshots)

Display error messages when the user attempts to create or edit users' or channels' names with any of the following words (case-insensitive):

  • admin;
  • administrator;
  • system;
  • user.
    create-channel
    register-username
    change-channel
    change-username

Issue(s)

Task - ClickUp

Steps to test or reproduce

Users:

  1. Register a new user;
  2. Update username in My Account > Profile (the admin must allow username changes in Administration > Accounts > Allow Username Change).

Channels:

  1. Create Channel;
  2. Channel's Name Update in Room Info > Edit (the error is displayed after the user save changes).

Further comments

@KevLehman
Copy link
Member

Is it possible to centralize all the reserved words into a single place/file? That way, we won't have to modify all files to add a new reserved word.

PD: I thought this may be the use case for settings, but not sure if we can create a system only setting.

Copy link
Member

@sampaiodiego sampaiodiego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as @KevLehman pointed out, please make a change to have a unified place with invalid names, that would help maintaining the code 👍 having a hidden setting is a good way to achieve that, but along with that I'd consider creating a function to validate it that is called in multiple places.

I'd also not struggle to have the function working on frontend if it becomes challenging.

We need to cover cases where a user or channel might have one of those names already.

And I'd also ask for some API tests =)


export const validateName = function(name) {
if (settings.get('Accounts_SystemBlockedUsernameList').includes(name.toLowerCase())) {
throw new Meteor.Error('invalid-name', `${ name } is a reserved name.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case, since this function is almost "pure", it is better to use regular Error instead of Meteor.Error.. we should use Meteor.Error only on Meteor environments (like Meteor methods), otherwise always use regular Node exceptions

@@ -0,0 +1,9 @@
import { Meteor } from 'meteor/meteor';

import { settings } from '../../../settings';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you cannot import from the final file, pls try at least import from the server folder, for example:

Suggested change
import { settings } from '../../../settings';
import { settings } from '../../../settings/server';


import { settings } from '../../../settings';

export const validateName = function(name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you convert this file to TypeScript pls? <3

nameValidation = new RegExp('^[0-9a-zA-Z-_.]+$');
}

if (settings.get('Accounts_SystemBlockedUsernameList').includes(username.toLowerCase())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to not use validateName function here?

}

if (!nameValidation.test(slugifiedName)) {
if (!nameValidation.test(slugifiedName) || settings.get('Accounts_SystemBlockedUsernameList').includes(slugifiedName.toLowerCase())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to not use validateName function here?

import { API } from '../api';
import { settings } from '../../../settings';
import { Team } from '../../../../server/sdk';
import { validateName } from '../../../lib';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can, import direct from the file:

Suggested change
import { validateName } from '../../../lib';
import { validateName } from '../../../lib/server/functions/validateName';

import { createUser, login, deleteUser, getUserStatus } from '../../data/users.helper.js';
import { createRoom } from '../../data/rooms.helper';

const reservedWords = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this array to tests/data/api-data.js ? this way you can use it here and on tests/end-to-end/api/02-channels.js without having to duplicate it =)

@sampaiodiego sampaiodiego added this to the 3.14.0 milestone Apr 20, 2021
@sampaiodiego sampaiodiego merged commit 49cc2c9 into develop Apr 20, 2021
@sampaiodiego sampaiodiego deleted the reserved-words branch April 20, 2021 19:41
@sampaiodiego sampaiodiego mentioned this pull request Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants