fix: validate username before registering user (#32743)
parent
1e1e849e25
commit
dd37ea1b35
@ -0,0 +1,7 @@ |
||||
--- |
||||
'@rocket.chat/web-ui-registration': patch |
||||
'@rocket.chat/i18n': patch |
||||
'@rocket.chat/meteor': patch |
||||
--- |
||||
|
||||
Fixes an issue where creating a new user with an invalid username (containing special characters) resulted in an error message, but the user was still created. The user creation process now properly aborts when an invalid username is provided. |
||||
@ -0,0 +1,15 @@ |
||||
import { settings } from '../../../settings/server'; |
||||
|
||||
export const validateUsername = (username: string): boolean => { |
||||
const settingsRegExp = settings.get('UTF8_User_Names_Validation'); |
||||
const defaultPattern = /^[0-9a-zA-Z-_.]+$/; |
||||
|
||||
let usernameRegExp: RegExp; |
||||
try { |
||||
usernameRegExp = settingsRegExp ? new RegExp(`^${settingsRegExp}$`) : defaultPattern; |
||||
} catch (e) { |
||||
usernameRegExp = defaultPattern; |
||||
} |
||||
|
||||
return usernameRegExp.test(username); |
||||
}; |
||||
@ -0,0 +1,94 @@ |
||||
import { expect } from 'chai'; |
||||
import proxyquire from 'proxyquire'; |
||||
import sinon from 'sinon'; |
||||
|
||||
describe('validateUsername', () => { |
||||
const getStub = sinon.stub(); |
||||
|
||||
const proxySettings = { |
||||
settings: { |
||||
get: getStub, |
||||
}, |
||||
}; |
||||
|
||||
const { validateUsername } = proxyquire.noCallThru().load('../../../../../../app/lib/server/functions/validateUsername', { |
||||
'../../../settings/server': proxySettings, |
||||
}); |
||||
|
||||
beforeEach(() => { |
||||
getStub.reset(); |
||||
}); |
||||
|
||||
afterEach(() => { |
||||
sinon.restore(); |
||||
}); |
||||
|
||||
describe('with default settings', () => { |
||||
beforeEach(() => { |
||||
getStub.withArgs('UTF8_User_Names_Validation').returns('[0-9a-zA-Z-_.]+'); |
||||
}); |
||||
|
||||
it('should return true for a valid username', () => { |
||||
const result = validateUsername('valid_username.123'); |
||||
expect(result).to.be.true; |
||||
}); |
||||
|
||||
it('should return false for an invalid username containing special HTML tags', () => { |
||||
const result = validateUsername('username<div>$</div>'); |
||||
expect(result).to.be.false; |
||||
}); |
||||
|
||||
it('should return false for an empty username', () => { |
||||
const result = validateUsername(''); |
||||
expect(result).to.be.false; |
||||
}); |
||||
|
||||
it('should return false for a username with invalid characters', () => { |
||||
const result = validateUsername('invalid*username!'); |
||||
expect(result).to.be.false; |
||||
}); |
||||
|
||||
it('should return true for a username with allowed special characters', () => { |
||||
const result = validateUsername('username-_.'); |
||||
expect(result).to.be.true; |
||||
}); |
||||
}); |
||||
|
||||
describe('with custom regex settings', () => { |
||||
beforeEach(() => { |
||||
getStub.withArgs('UTF8_User_Names_Validation').returns('[a-zA-Z]+'); |
||||
}); |
||||
|
||||
it('should return true for a username matching the custom regex', () => { |
||||
const result = validateUsername('ValidUsername'); |
||||
expect(result).to.be.true; |
||||
}); |
||||
|
||||
it('should return false for a username that does not match the custom regex', () => { |
||||
const result = validateUsername('username123'); |
||||
expect(result).to.be.false; |
||||
}); |
||||
}); |
||||
|
||||
describe('with null regex settings', () => { |
||||
beforeEach(() => { |
||||
getStub.withArgs('UTF8_User_Names_Validation').returns(null); |
||||
}); |
||||
|
||||
it('should fallback to the default regex pattern if the settings value is null', () => { |
||||
const result = validateUsername('username'); |
||||
expect(result).to.be.true; |
||||
}); |
||||
}); |
||||
|
||||
describe('with invalid regex settings', () => { |
||||
beforeEach(() => { |
||||
getStub.withArgs('UTF8_User_Names_Validation').returns('invalid['); |
||||
}); |
||||
|
||||
it('should fallback to the default regex pattern if the settings value is invalid', () => { |
||||
const result = validateUsername('username'); |
||||
expect(result).to.be.true; |
||||
}); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue