Files
habitica/website/server/libs/user/validation.js
Phillip Thelen f8d315ff6e Upgrade to mongoose 7 (#14971)
* remove some unused dependencies

* update mongoose version

* make common tests pass

* Make unit tests pass

* make api v3 integration tests pass

* fix lint issues

* fix issue with package-lock

* fix(lint): we don't need no .js

* fix(lint): update to latest config-habitrpg

* chore(npm): update package locks

* fix(test): replace deprecated fn

* chore(package): update eslint-habitrpg again

* fix(lint): server linting

* fix(lint): client linting

* fix(client): correct mangled common imports

* chore(npm): update package-locks

* fix(lint): punctuation, module

---------

Co-authored-by: SabreCat <sabrecat@gmail.com>
Co-authored-by: SabreCat <sabe@habitica.com>
2024-01-16 15:18:47 -06:00

62 lines
2.3 KiB
JavaScript

import bannedSlurs from '../bannedSlurs';
import bannedWords from '../bannedWords';
import {
getMatchesByWordArray,
normalizeUnicodeString,
removePunctuationFromString,
} from '../stringUtils';
import forbiddenUsernames from '../forbiddenUsernames';
const bannedSlurRegexes = bannedSlurs.map(word => new RegExp(`\\b([^a-z]+)?${word}([^a-z]+)?\\b`, 'i'));
const bannedWordRegexes = bannedWords.map(word => new RegExp(`\\b([^a-z]+)?${word}([^a-z]+)?\\b`, 'i'));
export function stringContainsProfanity (str, profanityType = 'bannedWord') {
const bannedRegexes = profanityType === 'slur' ? bannedSlurRegexes : bannedWordRegexes;
for (let i = 0; i < bannedRegexes.length; i += 1) {
const regEx = bannedRegexes[i];
const match = removePunctuationFromString(normalizeUnicodeString(str)).match(regEx);
if (match !== null && match[0] !== null) {
return true;
}
}
return false;
}
export function nameContainsNewline (username) {
return username.includes('\n');
}
function usernameIsForbidden (username) {
const forbiddenWordsMatched = getMatchesByWordArray(username, forbiddenUsernames);
return forbiddenWordsMatched.length > 0;
}
const invalidCharsRegex = /[^a-z0-9_-]/i;
function usernameContainsInvalidCharacters (username) {
const match = username.match(invalidCharsRegex);
return match !== null && match[0] !== null;
}
export function verifyDisplayName (displayName, res) {
const issues = [];
if (displayName.length < 1 || displayName.length > 30) issues.push(res.t('displaynameIssueLength'));
if (stringContainsProfanity(displayName, 'slur')) issues.push(res.t('bannedSlurUsedInProfile'));
if (nameContainsNewline(displayName)) issues.push(res.t('displaynameIssueNewline'));
return issues;
}
export function verifyUsername (username, res, newUser = true) {
const slurMessage = newUser
? res.t('usernameIssueSlur')
: res.t('bannedSlurUsedInProfile');
const issues = [];
if (username.length < 1 || username.length > 20) issues.push(res.t('usernameIssueLength'));
if (usernameContainsInvalidCharacters(username)) issues.push(res.t('usernameIssueInvalidCharacters'));
if (stringContainsProfanity(username, 'slur') || stringContainsProfanity(username)) issues.push(slurMessage);
if (usernameIsForbidden(username)) issues.push(res.t('usernameIssueForbidden'));
return issues;
}