-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate username before registering user (#32743)
- Loading branch information
1 parent
1e1e849
commit dd37ea1
Showing
9 changed files
with
161 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
apps/meteor/tests/unit/app/lib/server/functions/validateUsername.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters