Skip to content

Commit

Permalink
Fix regex escape for prefix patterns
Browse files Browse the repository at this point in the history
Our regex escape function escapes proper regexes, however
it isn't meant to be shoved into a char class via string interpolation.

We need to also escape '-' if we do so.
  • Loading branch information
brunnre8 committed Jul 4, 2022
1 parent d72d869 commit d6e1af0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 9 additions & 2 deletions client/js/helpers/ircmessageparser/findChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export type ChannelPart = Part & {
channel: string;
};

// escapes a regex in a way that's compatible to shove it in
// a regex char set (meaning it also escapes -)
function escapeRegExpCharSet(raw: string): string {
const escaped: string = escapeRegExp(raw);
return escaped.replace("-", "\\-");
}

// Given an array of channel prefixes (such as "#" and "&") and an array of user
// modes (such as "@" and "+"), this function extracts channels and nicks from a
// text.
Expand All @@ -18,8 +25,8 @@ function findChannels(text: string, channelPrefixes: string[], userModes: string
// For example, a voiced user in #thelounge will have a /whois response of:
// > foo is on the following channels: +#thelounge
// We need to explicitly ignore user modes to parse such channels correctly.
const userModePattern = userModes.map(escapeRegExp).join("");
const channelPrefixPattern = channelPrefixes.map(escapeRegExp).join("");
const userModePattern = userModes.map(escapeRegExpCharSet).join("");
const channelPrefixPattern = channelPrefixes.map(escapeRegExpCharSet).join("");
const channelPattern = `(?:^|\\s)[${userModePattern}]*([${channelPrefixPattern}][^ \u0007]+)`;
const channelRegExp = new RegExp(channelPattern, "g");

Expand Down
15 changes: 15 additions & 0 deletions test/client/js/helpers/ircmessageparser/findChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ describe("findChannels", () => {
expect(actual).to.deep.equal(expected);
});

it("should work with - in usermodes", () => {
const input = "-#a some -text";
const expected = [
{
channel: "#a",
start: 1,
end: 3,
},
];

const actual = findChannels(input, ["#"], ["#", "+", "-"]);

expect(actual).to.deep.equal(expected);
});

it("should handle multiple channelPrefix correctly", () => {
const input = "##test";
const expected = [
Expand Down

0 comments on commit d6e1af0

Please sign in to comment.