diff --git a/backend/src/graphql/resolver/util/findUserByIdentifier.ts b/backend/src/graphql/resolver/util/findUserByIdentifier.ts index dd4f9a775..bd9a25071 100644 --- a/backend/src/graphql/resolver/util/findUserByIdentifier.ts +++ b/backend/src/graphql/resolver/util/findUserByIdentifier.ts @@ -4,7 +4,7 @@ import { validate, version } from 'uuid' import { LogError } from '@/server/LogError' -import { validAliasRegex } from './validateAlias' +import { VALID_ALIAS_REGEX } from './validateAlias' export const findUserByIdentifier = async (identifier: string): Promise => { let user: DbUser | undefined @@ -29,7 +29,7 @@ export const findUserByIdentifier = async (identifier: string): Promise } user = userContact.user user.emailContact = userContact - } else if (validAliasRegex.exec(identifier)) { + } else if (VALID_ALIAS_REGEX.exec(identifier)) { user = await DbUser.findOne({ where: { alias: identifier }, relations: ['emailContact'] }) if (!user) { throw new LogError('No user found to given identifier', identifier) diff --git a/backend/src/graphql/resolver/util/validateAlias.ts b/backend/src/graphql/resolver/util/validateAlias.ts index 88cb9b982..721733be4 100644 --- a/backend/src/graphql/resolver/util/validateAlias.ts +++ b/backend/src/graphql/resolver/util/validateAlias.ts @@ -4,9 +4,9 @@ import { User as DbUser } from '@entity/User' import { LogError } from '@/server/LogError' // eslint-disable-next-line security/detect-unsafe-regex -export const validAliasRegex = /^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+?)*$/ +export const VALID_ALIAS_REGEX = /^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+?)*$/ -const reservedAlias = [ +const RESERVED_ALIAS = [ 'admin', 'email', 'gast', @@ -27,8 +27,9 @@ const reservedAlias = [ export const validateAlias = async (alias: string): Promise => { if (alias.length < 3) throw new LogError('Given alias is too short', alias) if (alias.length > 20) throw new LogError('Given alias is too long', alias) - if (!alias.match(validAliasRegex)) throw new LogError('Invalid characters in alias', alias) - if (reservedAlias.includes(alias.toLowerCase())) throw new LogError('Alias is not allowed', alias) + if (!alias.match(VALID_ALIAS_REGEX)) throw new LogError('Invalid characters in alias', alias) + if (RESERVED_ALIAS.includes(alias.toLowerCase())) + throw new LogError('Alias is not allowed', alias) const aliasInUse = await DbUser.find({ where: { alias: Raw((a) => `LOWER(${a}) = "${alias.toLowerCase()}"`) }, })