Skip to content

Commit 6656c71

Browse files
vincentkocopenclaw-clownfish[bot]rohitjavvadi
authored
fix(discord): protect mention aliases in code fences (#93662)
Co-authored-by: openclaw-clownfish[bot] <280122609+openclaw-clownfish[bot]@users.noreply.github.com> Co-authored-by: Rohit <[email protected]>
1 parent 8c7690b commit 6656c71

2 files changed

Lines changed: 85 additions & 7 deletions

File tree

‎extensions/discord/src/mentions.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ describe("rewriteDiscordKnownMentions", () => {
103103
expect(rewritten).toBe("inline `@alice` fence ```\n@alice\n``` text <@123456789>");
104104
});
105105

106+
it("does not end longer code fences at triple-backtick literals inside the body", () => {
107+
rememberDiscordDirectoryUser({
108+
accountId: "default",
109+
userId: "123456789",
110+
handles: ["alice"],
111+
});
112+
const text = '````ts\nconst fence = "```";\n@alice\n```` text @alice';
113+
const rewritten = rewriteDiscordKnownMentions(text, {
114+
accountId: "default",
115+
});
116+
expect(rewritten).toBe('````ts\nconst fence = "```";\n@alice\n```` text <@123456789>');
117+
});
118+
106119
it("is account-scoped", () => {
107120
rememberDiscordDirectoryUser({
108121
accountId: "ops",

‎extensions/discord/src/mentions.ts‎

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { resolveDiscordDirectoryUserId } from "./directory-cache.js";
88

99
type DiscordMentionAliasesConfig = Record<string, string>;
1010

11-
const MARKDOWN_CODE_SEGMENT_PATTERN = /```[\s\S]*?```|`[^`\n]*`/g;
1211
const MENTION_CANDIDATE_PATTERN = /(^|[\s([{"'.,;:!?])@([a-z0-9_.-]{2,32}(?:#[0-9]{4})?)/gi;
1312
const DISCORD_RESERVED_MENTIONS = new Set(["everyone", "here"]);
1413
const DISCORD_DISCRIMINATOR_SUFFIX = /#\d{4}$/;
@@ -126,6 +125,72 @@ function rewritePlainTextMentions(
126125
});
127126
}
128127

128+
function countBacktickRun(text: string, index: number): number {
129+
let cursor = index;
130+
while (text[cursor] === "`") {
131+
cursor += 1;
132+
}
133+
return cursor - index;
134+
}
135+
136+
function findSameLineBacktickRun(
137+
text: string,
138+
startIndex: number,
139+
runLength: number,
140+
): number | null {
141+
const delimiter = "`".repeat(runLength);
142+
const newlineIndex = text.indexOf("\n", startIndex);
143+
const lineEnd = newlineIndex === -1 ? text.length : newlineIndex;
144+
const closeIndex = text.indexOf(delimiter, startIndex);
145+
return closeIndex !== -1 && closeIndex < lineEnd ? closeIndex + runLength : null;
146+
}
147+
148+
function findFenceEnd(text: string, startIndex: number, runLength: number): number {
149+
let searchIndex = startIndex + runLength;
150+
while (searchIndex < text.length) {
151+
const newlineIndex = text.indexOf("\n", searchIndex);
152+
if (newlineIndex === -1) {
153+
return text.length;
154+
}
155+
let lineCursor = newlineIndex + 1;
156+
while (text[lineCursor] === " " && lineCursor - newlineIndex <= 3) {
157+
lineCursor += 1;
158+
}
159+
const closingRunLength = countBacktickRun(text, lineCursor);
160+
if (closingRunLength >= runLength) {
161+
return lineCursor + closingRunLength;
162+
}
163+
searchIndex = lineCursor + Math.max(closingRunLength, 1);
164+
}
165+
return text.length;
166+
}
167+
168+
function findNextMarkdownCodeSegment(
169+
text: string,
170+
startIndex: number,
171+
): { startIndex: number; endIndex: number } | null {
172+
let searchIndex = startIndex;
173+
while (searchIndex < text.length) {
174+
const segmentStart = text.indexOf("`", searchIndex);
175+
if (segmentStart === -1) {
176+
return null;
177+
}
178+
const runLength = countBacktickRun(text, segmentStart);
179+
const inlineEndIndex = findSameLineBacktickRun(text, segmentStart + runLength, runLength);
180+
if (inlineEndIndex !== null) {
181+
return { startIndex: segmentStart, endIndex: inlineEndIndex };
182+
}
183+
if (runLength >= 3) {
184+
return {
185+
startIndex: segmentStart,
186+
endIndex: findFenceEnd(text, segmentStart, runLength),
187+
};
188+
}
189+
searchIndex = segmentStart + runLength;
190+
}
191+
return null;
192+
}
193+
129194
export function rewriteDiscordKnownMentions(
130195
text: string,
131196
params: {
@@ -138,12 +203,12 @@ export function rewriteDiscordKnownMentions(
138203
}
139204
let rewritten = "";
140205
let offset = 0;
141-
MARKDOWN_CODE_SEGMENT_PATTERN.lastIndex = 0;
142-
for (const match of text.matchAll(MARKDOWN_CODE_SEGMENT_PATTERN)) {
143-
const matchIndex = match.index ?? 0;
144-
rewritten += rewritePlainTextMentions(text.slice(offset, matchIndex), params);
145-
rewritten += match[0];
146-
offset = matchIndex + match[0].length;
206+
let segment = findNextMarkdownCodeSegment(text, offset);
207+
while (segment) {
208+
rewritten += rewritePlainTextMentions(text.slice(offset, segment.startIndex), params);
209+
rewritten += text.slice(segment.startIndex, segment.endIndex);
210+
offset = segment.endIndex;
211+
segment = findNextMarkdownCodeSegment(text, offset);
147212
}
148213
rewritten += rewritePlainTextMentions(text.slice(offset), params);
149214
return rewritten;

0 commit comments

Comments
 (0)