Skip to content

Commit 1e92c13

Browse files
authored
Improve PushProcessor::getPushRuleGlobRegex (#4764)
* Improve PushProcessor::getPushRuleGlobRegex Fix cache key not taking non-pattern parameters into account Use lookarounds to ensure the word boundary isn't treated as part of the match Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Add tests Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent 8061fa9 commit 1e92c13

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

spec/unit/pushprocessor.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
import * as utils from "../test-utils/test-utils";
218
import { type IActionsObject, PushProcessor } from "../../src/pushprocessor";
319
import {
@@ -1004,3 +1020,25 @@ describe("rewriteDefaultRules", () => {
10041020
]);
10051021
});
10061022
});
1023+
1024+
describe("getPushRuleGlobRegex", () => {
1025+
it("should not confuse flags in cache", () => {
1026+
const pattern = "Test";
1027+
const regex1 = PushProcessor.getPushRuleGlobRegex(pattern, false, "i");
1028+
const regex2 = PushProcessor.getPushRuleGlobRegex(pattern, false, "g");
1029+
const regex3 = PushProcessor.getPushRuleGlobRegex(pattern, false, "i");
1030+
1031+
expect(regex1.flags).toBe("i");
1032+
expect(regex2.flags).toBe("g");
1033+
1034+
expect(regex1).not.toEqual(regex2);
1035+
expect(regex1).toEqual(regex3);
1036+
});
1037+
1038+
it("should not include word boundary in match", () => {
1039+
const pattern = "@room";
1040+
const regex = PushProcessor.getPushRuleGlobRegex(pattern, true);
1041+
const input = "Foo @room Bar";
1042+
expect(input.split(regex)).toEqual(["Foo ", "@room", " Bar"]);
1043+
});
1044+
});

src/pushprocessor.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,21 @@ export class PushProcessor {
313313
* No cache invalidation is present currently,
314314
* as this will be inherently bounded to the size of the user's own push rules.
315315
* @param pattern - the glob pattern to convert to a RegExp
316-
* @param alignToWordBoundary - whether to align the pattern to word boundaries, as specified for `content.body` matches
316+
* @param alignToWordBoundary - whether to align the pattern to word boundaries,
317+
* as specified for `content.body` matches, will use lookaround assertions to ensure the match only includes the pattern
317318
* @param flags - the flags to pass to the RegExp constructor, defaults to case-insensitive
318319
*/
319320
public static getPushRuleGlobRegex(pattern: string, alignToWordBoundary = false, flags = "i"): RegExp {
320-
const [prefix, suffix] = alignToWordBoundary ? ["(^|\\W)", "(\\W|$)"] : ["^", "$"];
321+
const [prefix, suffix] = alignToWordBoundary ? ["(?<=^|\\W)", "(?=\\W|$)"] : ["^", "$"];
322+
const cacheKey = `${alignToWordBoundary}-${flags}-${pattern}`;
321323

322-
if (!PushProcessor.cachedGlobToRegex[pattern]) {
323-
PushProcessor.cachedGlobToRegex[pattern] = new RegExp(
324+
if (!PushProcessor.cachedGlobToRegex[cacheKey]) {
325+
PushProcessor.cachedGlobToRegex[cacheKey] = new RegExp(
324326
prefix + "(" + globToRegexp(pattern) + ")" + suffix,
325327
flags,
326328
);
327329
}
328-
return PushProcessor.cachedGlobToRegex[pattern];
330+
return PushProcessor.cachedGlobToRegex[cacheKey];
329331
}
330332

331333
/**

0 commit comments

Comments
 (0)