Skip to content

Commit bb0004e

Browse files
committed
fix(matrix): recognize full user ID command mentions
1 parent 4da7051 commit bb0004e

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎extensions/matrix/src/matrix/monitor/handler.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,8 @@ describe("matrix monitor handler pairing account scope", () => {
954954

955955
it.each([
956956
{ label: "full Matrix user ID", body: "hello @bot:example.org" },
957+
{ label: "colon-delimited full Matrix user ID", body: "@bot:example.org: help" },
958+
{ label: "Unicode-whitespace-colon-delimited full ID", body: "@bot:example.org:\u2003help" },
957959
{ label: "localpart shorthand", body: "hello @bot" },
958960
])("processes native plain-text $label without configured mention patterns", async ({ body }) => {
959961
const getMemberDisplayName = vi.fn(async () => "sender");
@@ -982,6 +984,9 @@ describe("matrix monitor handler pairing account scope", () => {
982984

983985
it.each([
984986
{ label: "another homeserver", body: "hello @bot:evil.example" },
987+
{ label: "an unexpected homeserver port", body: "@bot:example.org:8448: help" },
988+
{ label: "an invisible full-ID command separator", body: "@bot:example.org:\ufeffhelp" },
989+
{ label: "a room-alias full-ID command collision", body: "#@bot:example.org: help" },
985990
{ label: "a longer Unicode localpart", body: "hello @boté" },
986991
{ label: "a historical exclamation localpart", body: "hello @bot!evil:evil.example" },
987992
{ label: "a historical percent localpart", body: "hello @bot%evil:evil.example" },

‎extensions/matrix/src/matrix/monitor/mentions.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ describe("resolveMentions", () => {
4141
mentionedUserId: "@bot:matrix.org",
4242
body: "hello @bot:matrix.org",
4343
},
44+
{
45+
label: "colon-delimited full Matrix user ID",
46+
mentionedUserId: "@bot:matrix.org",
47+
body: "@bot:matrix.org: help",
48+
},
49+
{
50+
label: "Unicode-whitespace-colon-delimited full Matrix user ID",
51+
mentionedUserId: "@bot:matrix.org",
52+
body: "@bot:matrix.org:\u2003help",
53+
},
54+
{
55+
label: "colon-delimited full Matrix user ID with a homeserver port",
56+
mentionedUserId: "@bot:matrix.org:8448",
57+
body: "@bot:matrix.org:8448: help",
58+
},
4459
{
4560
label: "localpart shorthand",
4661
mentionedUserId: "@bot:matrix.org",
@@ -86,6 +101,11 @@ describe("resolveMentions", () => {
86101
mentionedUserId: "@bot:[2001:db8::1]:8448",
87102
body: "hello @bot:[2001:db8::1]:8448",
88103
},
104+
{
105+
label: "colon-delimited bracketed IPv6 homeserver and port",
106+
mentionedUserId: "@bot:[2001:db8::1]:8448",
107+
body: "@bot:[2001:db8::1]:8448:\u2003help",
108+
},
89109
])(
90110
"detects native plain-text $label without configured mention patterns",
91111
({ mentionedUserId, body }) => {
@@ -111,6 +131,25 @@ describe("resolveMentions", () => {
111131
{ label: "case-different homeserver", body: "hello @bot:MATRIX.ORG" },
112132
{ label: "extended DNS homeserver", body: "hello @bot:matrix.org.evil" },
113133
{ label: "unexpected homeserver port", body: "hello @bot:matrix.org:8448" },
134+
{ label: "unexpected homeserver port before a command", body: "@bot:matrix.org:8448: help" },
135+
{ label: "unexpected repeated homeserver colon", body: "@bot:matrix.org::8448" },
136+
{ label: "full Matrix user ID followed by colon alone", body: "@bot:matrix.org:" },
137+
{
138+
label: "full Matrix user ID followed by invisible BOM",
139+
body: "@bot:matrix.org:\ufeffhelp",
140+
},
141+
{
142+
label: "full Matrix user ID followed by invisible zero-width text",
143+
body: "@bot:matrix.org:\u200bhelp",
144+
},
145+
{
146+
label: "full Matrix user ID followed by bidirectional formatting",
147+
body: "@bot:matrix.org:\u202ehelp",
148+
},
149+
{
150+
label: "full Matrix user ID followed by a hidden control",
151+
body: "@bot:matrix.org:\u0001help",
152+
},
114153
{ label: "alternate IPv6 homeserver", body: "hello @bot:[::1]" },
115154
{ label: "extended dotted localpart", body: "hello @bot.extra" },
116155
{ label: "extended plus localpart", body: "hello @bot+evil" },
@@ -133,6 +172,7 @@ describe("resolveMentions", () => {
133172
{ label: "ambiguous Markdown-wrapped shorthand", body: "hello **@bot**" },
134173
{ label: "ambiguous hash-wrapped shorthand", body: "hello #@bot#" },
135174
{ label: "Matrix room-alias account collision", body: "hello #@bot:matrix.org" },
175+
{ label: "Matrix room-alias colon-command collision", body: "#@bot:matrix.org: help" },
136176
{ label: "Matrix room-ID account collision", body: "hello !@bot:matrix.org" },
137177
{ label: "Matrix event-ID account collision", body: "hello $@bot:matrix.org" },
138178
{ label: "unseparated Unicode punctuation", body: "hello (@bot),thanks" },

‎extensions/matrix/src/matrix/monitor/mentions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function hasVisibleNativeMatrixUserMention(text: string | undefined, userId: str
4747
// Historical localparts can end in any punctuation, so shorthand must stay
4848
// bare; colon plus visible whitespace is safe because localparts forbid colon.
4949
const pattern = new RegExp(
50-
String.raw`(?:^|\p{White_Space})(?:${escapeRegExp(userId)}(?=$|\p{White_Space}|[,!?;](?=$|\p{White_Space}))|${escapeRegExp(`@${localpart}`)}(?=$|\p{White_Space}|:\p{White_Space}))`,
50+
String.raw`(?:^|\p{White_Space})(?:${escapeRegExp(userId)}(?=$|\p{White_Space}|:\p{White_Space}|[,!?;](?=$|\p{White_Space}))|${escapeRegExp(`@${localpart}`)}(?=$|\p{White_Space}|:\p{White_Space}))`,
5151
"u",
5252
);
5353
return pattern.test(text);

0 commit comments

Comments
 (0)