Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape user names. #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/watchlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export default class WatchList extends LentilBase {
}

_getRegexForUser(user) {
return new RegExp(`(^|\\s)${user}($|:|-|\\s)`, 'i');
// Escape regexp special characters allowed in IRC nicks: \^|{}[]
const escapedUser = user.replace(/[\\^|{}[\]]/g, '\\$&');
return new RegExp(`(^|\\s)${escapedUser}($|[-:\\s])`, 'i');
}

_buildRegexCache() {
Expand Down
72 changes: 52 additions & 20 deletions tests/lib/watchlist_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ describe('WatchList', function () {
dave: {
email: 'dave@reddwarf.com',
},
'dave|wfh': {
email: 'dave+wfh@reddwarf.com',
},
};

watchlist.configFilesLoader = {
Expand Down Expand Up @@ -65,28 +68,57 @@ describe('WatchList', function () {
});
});

it('#_getRegexForUser should match a user name in a message correctly', function () {
const regex = watchlist._getRegexForUser('dave');
const validMessages = [
'hello dave',
'dave',
'dave hello',
'dave: hello',
'hello dave hello',
'hello dave- hello',
];
const invalidMessage = [
'daveaskdjh',
'asdkajdave',
'asdaddaveasdas',
];

validMessages.forEach((message) => {
chai.assert.isOk(regex.test(message), `Expected "${message}" to match`);
describe('#_getRegexForUser', function () {
it('should match a user name in a message correctly', function () {
const regex = watchlist._getRegexForUser('dave');
const validMessages = [
'hello dave',
'dave',
'dave hello',
'dave: hello',
'hello dave hello',
'hello dave- hello',
];
const invalidMessage = [
'daveaskdjh',
'asdkajdave',
'asdaddaveasdas',
];

validMessages.forEach((message) => {
chai.assert.isOk(regex.test(message), `Expected "${message}" to match`);
});

invalidMessage.forEach((message) => {
chai.assert.isNotOk(regex.test(message), `Expected "${message}" not to match`);
});
});

invalidMessage.forEach((message) => {
chai.assert.isNotOk(regex.test(message), `Expected "${message}" not to match`);
it('should escape user names', function () {
const regex = watchlist._getRegexForUser('dave|wfh');
const validMessages = [
'hello dave|wfh',
'dave|wfh',
'dave|wfh hello',
'dave|wfh: hello',
'hello dave|wfh hello',
'hello dave|wfh- hello',
];
const invalidMessage = [
'dave|wfhaskdjh',
'asdkajdave|wfh',
'asdaddave|wfhasdas',
'hello dave',
'hello wfh',
];

validMessages.forEach((message) => {
chai.assert.isOk(regex.test(message), `Expected "${message}" to match`);
});

invalidMessage.forEach((message) => {
chai.assert.isNotOk(regex.test(message), `Expected "${message}" not to match`);
});
});
});

Expand Down