From 389110834a4641171f056ef5a7f27c9b97b2df7a Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 30 May 2023 15:54:18 +0100 Subject: [PATCH 1/4] Remove spec non-compliant extended glob format --- spec/unit/utils.spec.ts | 17 +++++++++++++++++ src/models/invites-ignorer.ts | 2 +- src/utils.ts | 10 ++-------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/spec/unit/utils.spec.ts b/spec/unit/utils.spec.ts index 13fffb93cd0..5c2db9e9c88 100644 --- a/spec/unit/utils.spec.ts +++ b/spec/unit/utils.spec.ts @@ -30,6 +30,8 @@ import { sortEventsByLatestContentTimestamp, safeSet, MapWithDefault, + globToRegexp, + escapeRegExp, } from "../../src/utils"; import { logger } from "../../src/logger"; import { mkMessage } from "../test-utils/test-utils"; @@ -725,4 +727,19 @@ describe("utils", function () { await utils.immediate(); }); }); + + describe("escapeRegExp", () => { + it("should escape XYZ", () => { + expect(escapeRegExp("[FIT-Connect Zustelldienst \\(Testumgebung\\)]")).toMatchInlineSnapshot( + `"\\[FIT-Connect Zustelldienst \\\\\\(Testumgebung\\\\\\)\\]"`, + ); + }); + }); + + describe("globToRegexp", () => { + it("should not explode when given regexes as globs", () => { + const result = globToRegexp("[FIT-Connect Zustelldienst \\(Testumgebung\\)]"); + expect(result).toMatchInlineSnapshot(`"\\[FIT-Connect Zustelldienst \\\\\\(Testumgebung\\\\\\)\\]"`); + }); + }); }); diff --git a/src/models/invites-ignorer.ts b/src/models/invites-ignorer.ts index 173ba620d9b..bb18cf0797f 100644 --- a/src/models/invites-ignorer.ts +++ b/src/models/invites-ignorer.ts @@ -186,7 +186,7 @@ export class IgnoredInvites { } let regexp: RegExp; try { - regexp = new RegExp(globToRegexp(glob, false)); + regexp = new RegExp(globToRegexp(glob)); } catch (ex) { // Assume invalid event. continue; diff --git a/src/utils.ts b/src/utils.ts index e2b29bdd4d1..a73658e3205 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -357,7 +357,7 @@ export function escapeRegExp(string: string): string { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } -export function globToRegexp(glob: string, extended = false): string { +export function globToRegexp(glob: string): string { // From // https://github.com/matrix-org/synapse/blob/abbee6b29be80a77e05730707602f3bbfc3f38cb/synapse/push/__init__.py#L132 // Because micromatch is about 130KB with dependencies, @@ -366,13 +366,7 @@ export function globToRegexp(glob: string, extended = false): string { [/\\\*/g, ".*"], [/\?/g, "."], ]; - if (!extended) { - replacements.push([ - /\\\[(!|)(.*)\\]/g, - (_match: string, neg: string, pat: string): string => - ["[", neg ? "^" : "", pat.replace(/\\-/, "-"), "]"].join(""), - ]); - } + return replacements.reduce( // https://github.com/microsoft/TypeScript/issues/30134 (pat, args) => (args ? pat.replace(args[0], args[1] as any) : pat), From 4ffd3e776487912f287d073785986e3073f0a4d6 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 30 May 2023 15:56:46 +0100 Subject: [PATCH 2/4] Simplify --- src/utils.ts | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index a73658e3205..1dba236c9c5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -357,21 +357,14 @@ export function escapeRegExp(string: string): string { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } +/** + * Converts Matrix glob-style string to a regular expression + * https://spec.matrix.org/v1.7/appendices/#glob-style-matching + * @param glob - Matrix glob-style string + * @returns regular expression + */ export function globToRegexp(glob: string): string { - // From - // https://github.com/matrix-org/synapse/blob/abbee6b29be80a77e05730707602f3bbfc3f38cb/synapse/push/__init__.py#L132 - // Because micromatch is about 130KB with dependencies, - // and minimatch is not much better. - const replacements: [RegExp, string | ((substring: string, ...args: any[]) => string)][] = [ - [/\\\*/g, ".*"], - [/\?/g, "."], - ]; - - return replacements.reduce( - // https://github.com/microsoft/TypeScript/issues/30134 - (pat, args) => (args ? pat.replace(args[0], args[1] as any) : pat), - escapeRegExp(glob), - ); + return escapeRegExp(glob).replace(/\\\*/g, ".*").replace(/\?/g, "."); } export function ensureNoTrailingSlash(url: string): string; From eefd69940c82906d4308085fc48e75457a29a198 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 30 May 2023 15:59:56 +0100 Subject: [PATCH 3/4] Remove tests for non spec compliant behaviour --- spec/unit/pushprocessor.spec.ts | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/spec/unit/pushprocessor.spec.ts b/spec/unit/pushprocessor.spec.ts index 36153e7deea..ffd21f431c0 100644 --- a/spec/unit/pushprocessor.spec.ts +++ b/spec/unit/pushprocessor.spec.ts @@ -289,39 +289,6 @@ describe("NotificationService", function () { expect(actions.tweaks.highlight).toEqual(true); }); - // TODO: This is not spec compliant behaviour. - // - // See https://spec.matrix.org/v1.5/client-server-api/#conditions-1 which - // describes pattern should glob: - // - // 1. * matches 0 or more characters; - // 2. ? matches exactly one character - it("should bing on character group ([abc]) bing words.", function () { - testEvent.event.content!.body = "Ping!"; - let actions = pushProcessor.actionsForEvent(testEvent); - expect(actions.tweaks.highlight).toEqual(true); - testEvent.event.content!.body = "Pong!"; - actions = pushProcessor.actionsForEvent(testEvent); - expect(actions.tweaks.highlight).toEqual(true); - }); - - // TODO: This is not spec compliant behaviour. (See above.) - it("should bing on character range ([a-z]) bing words.", function () { - testEvent.event.content!.body = "I ate 6 pies"; - const actions = pushProcessor.actionsForEvent(testEvent); - expect(actions.tweaks.highlight).toEqual(true); - }); - - // TODO: This is not spec compliant behaviour. (See above.) - it("should bing on character negation ([!a]) bing words.", function () { - testEvent.event.content!.body = "boke"; - let actions = pushProcessor.actionsForEvent(testEvent); - expect(actions.tweaks.highlight).toEqual(true); - testEvent.event.content!.body = "bake"; - actions = pushProcessor.actionsForEvent(testEvent); - expect(actions.tweaks.highlight).toEqual(false); - }); - it("should not bing on room server ACL changes", function () { testEvent = utils.mkEvent({ type: EventType.RoomServerAcl, From 4d69b231ac6846d183f0e5514fd86ef4c94da997 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 30 May 2023 16:04:53 +0100 Subject: [PATCH 4/4] Remove stale rules --- spec/unit/pushprocessor.spec.ts | 45 --------------------------------- 1 file changed, 45 deletions(-) diff --git a/spec/unit/pushprocessor.spec.ts b/spec/unit/pushprocessor.spec.ts index ffd21f431c0..c381c5e6406 100644 --- a/spec/unit/pushprocessor.spec.ts +++ b/spec/unit/pushprocessor.spec.ts @@ -97,51 +97,6 @@ describe("NotificationService", function () { pattern: "foo*bar", rule_id: "foobar", }, - { - actions: [ - "notify", - { - set_tweak: "sound", - value: "default", - }, - { - set_tweak: "highlight", - }, - ], - enabled: true, - pattern: "p[io]ng", - rule_id: "pingpong", - }, - { - actions: [ - "notify", - { - set_tweak: "sound", - value: "default", - }, - { - set_tweak: "highlight", - }, - ], - enabled: true, - pattern: "I ate [0-9] pies", - rule_id: "pies", - }, - { - actions: [ - "notify", - { - set_tweak: "sound", - value: "default", - }, - { - set_tweak: "highlight", - }, - ], - enabled: true, - pattern: "b[!ai]ke", - rule_id: "bakebike", - }, ], override: [ {