Skip to content

Commit 17d4baf

Browse files
committed
fix(discord): respect escaped inline-code delimiters
1 parent 1b30f70 commit 17d4baf

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ describe("rewriteDiscordKnownMentions", () => {
101101
input: "outside @alice then ``inside @alice",
102102
expected: "outside <@123456789> then ``inside @alice",
103103
},
104+
{
105+
name: "escaped literal backticks",
106+
input: "literal \\` outside @alice",
107+
expected: "literal \\` outside <@123456789>",
108+
},
109+
{
110+
name: "backticks after an even number of backslashes",
111+
input: "literal \\\\` inside @alice",
112+
expected: "literal \\\\` inside @alice",
113+
},
114+
{
115+
name: "escaped backticks before real unterminated code",
116+
input: "literal \\` outside @alice then `inside @alice",
117+
expected: "literal \\` outside <@123456789> then `inside @alice",
118+
},
104119
])("does not rewrite mentions inside $name", ({ input, expected }) => {
105120
rememberDiscordDirectoryUser({
106121
accountId: "default",

‎extensions/discord/src/mentions.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,16 @@ function findNextMarkdownCodeSegment(
155155
text: string,
156156
startIndex: number,
157157
): { startIndex: number; endIndex: number } | null {
158-
const segmentStart = text.indexOf("`", startIndex);
159-
if (segmentStart === -1) {
158+
const segmentOffset = text.slice(startIndex).search(/(?<=(?:^|[^\\])(?:\\\\)*)`/);
159+
if (segmentOffset === -1) {
160160
return null;
161161
}
162+
const segmentStart = startIndex + segmentOffset;
162163
const runLength = countBacktickRun(text, segmentStart);
163-
const inlineEndIndex = findSameLineBacktickRun(text, segmentStart + runLength, runLength);
164164
return {
165165
startIndex: segmentStart,
166166
endIndex:
167-
inlineEndIndex ??
167+
findSameLineBacktickRun(text, segmentStart + runLength, runLength) ??
168168
(runLength >= 3 ? findFenceEnd(text, segmentStart, runLength) : text.length),
169169
};
170170
}

‎extensions/discord/src/send.sends-basic-channel-messages.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ describe("sendMessageDiscord", () => {
683683
it.each([
684684
{ input: "ping @Alice", expected: "ping <@123456789012345678>" },
685685
{ input: "Run `notify @Alice", expected: "Run `notify @Alice" },
686+
{ input: "literal \\` ping @Alice", expected: "literal \\` ping <@123456789012345678>" },
687+
{ input: "literal \\\\` inside @Alice", expected: "literal \\\\` inside @Alice" },
686688
])(
687689
"rewrites cached @username mentions only outside code: $input",
688690
async ({ input, expected }) => {

‎extensions/discord/src/send.webhook.proxy.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,18 @@ describe("sendWebhookMessageDiscord proxy support", () => {
153153
globalFetchMock.mockRestore();
154154
});
155155

156-
it("does not rewrite webhook mentions inside unterminated inline code", async () => {
156+
it.each([
157+
{ input: "Run `notify @OpsLead", expected: "Run `notify @OpsLead" },
158+
{
159+
input: "literal \\` ping @OpsLead",
160+
expected: "literal \\` ping <@123456789012345678>",
161+
},
162+
])("only rewrites webhook mentions outside inline code: $input", async ({ input, expected }) => {
157163
const globalFetchMock = vi
158164
.spyOn(globalThis, "fetch")
159165
.mockResolvedValue(new Response(JSON.stringify({ id: "msg-code" }), { status: 200 }));
160166

161-
await sendWebhookMessageDiscord("Run `notify @OpsLead", {
167+
await sendWebhookMessageDiscord(input, {
162168
cfg: {
163169
channels: {
164170
discord: {
@@ -173,7 +179,9 @@ describe("sendWebhookMessageDiscord proxy support", () => {
173179
wait: true,
174180
});
175181

176-
expect(globalFetchMock.mock.calls[0]?.[1]?.body).toContain('"content":"Run `notify @OpsLead"');
182+
expect(globalFetchMock.mock.calls[0]?.[1]?.body).toContain(
183+
`"content":${JSON.stringify(expected)}`,
184+
);
177185
});
178186

179187
it("accepts Discord's no-body webhook response when wait is false", async () => {

0 commit comments

Comments
 (0)