Skip to content

Commit 484a007

Browse files
ly-wang19claude
andcommitted
fix(feishu): replace longer mention keys first to avoid prefix collisions
Feishu assigns sequential, non-zero-padded mention keys (@_user_1, @_user_2, ... @_user_10, @_user_11). normalizeMentions replaced each key with a global regex, so once a message mentions 10+ users the @_user_1 replacement also matched the @_user_1 prefix inside @_user_10/@_user_11/..., corrupting the longer-keyed users. Replace longer keys first so a shorter key can never match inside a longer one (the longer key is already substituted by the time the shorter key's turn comes), which also correctly handles a key immediately followed by text. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent d3dc7aa commit 484a007

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Feishu mention normalization tests cover prefix collisions between non-zero-padded keys.
2+
import { describe, expect, it } from "vitest";
3+
import { normalizeMentions } from "./bot-content.js";
4+
5+
describe("normalizeMentions", () => {
6+
it("does not let a shorter mention key match inside a longer prefix-sharing key", () => {
7+
// Feishu emits @_user_1, @_user_10, @_user_11, ... (no zero padding) once 10+ users are
8+
// mentioned, so @_user_1 is a textual prefix of @_user_10/@_user_11.
9+
const result = normalizeMentions("Standup: @_user_1 @_user_10 @_user_11 done", [
10+
{ key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } },
11+
{ key: "@_user_10", name: "Judy", id: { open_id: "ou_judy" } },
12+
{ key: "@_user_11", name: "Kevin", id: { open_id: "ou_kevin" } },
13+
]);
14+
expect(result).toBe(
15+
'Standup: <at user_id="ou_alice">Alice</at> <at user_id="ou_judy">Judy</at> <at user_id="ou_kevin">Kevin</at> done',
16+
);
17+
});
18+
19+
it("replaces a longer key even when it is immediately followed by text", () => {
20+
const result = normalizeMentions("hi @_user_1, see @_user_10pinned", [
21+
{ key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } },
22+
{ key: "@_user_10", name: "Judy", id: { open_id: "ou_judy" } },
23+
]);
24+
expect(result).toBe(
25+
'hi <at user_id="ou_alice">Alice</at>, see <at user_id="ou_judy">Judy</at>pinned',
26+
);
27+
});
28+
});

‎extensions/feishu/src/bot-content.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,13 @@ export function normalizeMentions(
277277
}
278278
const escaped = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
279279
const escapeName = (value: string) => value.replace(/</g, "&lt;").replace(/>/g, "&gt;");
280+
// Feishu mention keys (@_user_1, @_user_10, ...) are not zero-padded, so a shorter key is a
281+
// textual prefix of a longer one. Replace longer keys first so a shorter key (@_user_1) can never
282+
// match inside a longer one (@_user_10): the longer key is already substituted by the time the
283+
// shorter key's turn comes, which also handles a key immediately followed by ASCII text.
280284
let result = text;
281-
for (const mention of mentions) {
285+
const orderedMentions = mentions.toSorted((a, b) => b.key.length - a.key.length);
286+
for (const mention of orderedMentions) {
282287
const mentionId = mention.id.open_id;
283288
const replacement =
284289
botStripId && mentionId === botStripId

0 commit comments

Comments
 (0)