Skip to content

Commit

Permalink
Escape user names.
Browse files Browse the repository at this point in the history
  • Loading branch information
y120 committed Dec 29, 2017
1 parent c631b40 commit c75b65c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
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

0 comments on commit c75b65c

Please sign in to comment.