fix(client,server): usernames should not be a http error code (#37804)
* fix(client,server): usernames should not be a http error code * feat: reject invalid chars first Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
@ -1,13 +1,31 @@
|
||||
const validCharsRE = /^[a-zA-Z0-9\-_+]+$/;
|
||||
const invalidCharError = {
|
||||
valid: false,
|
||||
error: 'contains invalid characters'
|
||||
error: 'contains invalid characters.'
|
||||
};
|
||||
const validationSuccess = { valid: true, error: null };
|
||||
const usernameTooShort = { valid: false, error: 'is too short' };
|
||||
const usernameTooShort = { valid: false, error: 'is too short.' };
|
||||
const usernameIsHttpStatusCode = {
|
||||
valid: false,
|
||||
error: 'is a reserved error code.'
|
||||
};
|
||||
|
||||
exports.validate = str => {
|
||||
if (str.length < 3) return usernameTooShort;
|
||||
const isNumeric = num => !isNaN(num);
|
||||
const validCharsRE = /^[a-zA-Z0-9\-_+]*$/;
|
||||
const isHttpStatusCode = str =>
|
||||
isNumeric(str) && (parseInt(str, 10) >= 100 && parseInt(str, 10) <= 599);
|
||||
const isValidUsername = str => {
|
||||
if (!validCharsRE.test(str)) return invalidCharError;
|
||||
if (str.length < 3) return usernameTooShort;
|
||||
if (isHttpStatusCode(str)) return usernameIsHttpStatusCode;
|
||||
return validationSuccess;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
isNumeric,
|
||||
isHttpStatusCode,
|
||||
isValidUsername,
|
||||
validationSuccess,
|
||||
usernameTooShort,
|
||||
usernameIsHttpStatusCode,
|
||||
invalidCharError
|
||||
};
|
||||
|
Reference in New Issue
Block a user