Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): end a link label at its first unescaped bracket

marcotebbdc85f6840ad
diff --git a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
index edf30d91e..a59b89d8b 100644
--- a/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
+++ b/mcp-host/src/workflow/__tests__/inlineMarkup.test.ts
@@ -216,6 +216,21 @@ describe('inline markdown', () => {
)
})
+ it('ends a link label at its first unescaped bracket, so an escaped "](" names no target', () => {
+ expect(inlineSpans('[a \\](b](https://x.com/a)')).toEqual([
+ { text: 'a ](b', link: 'https://x.com/a' },
+ ])
+ expect(inlineSpans('[a \\](file:///etc/passwd](https://x.com/a)')).toEqual([
+ { text: 'a ](file:///etc/passwd', link: 'https://x.com/a' },
+ ])
+ expect(inlineSpans('<a href="https://x.com/a">a](javascript:alert(1)</a>')).toEqual([
+ { text: 'a](javascript:alert(1)', link: 'https://x.com/a' },
+ ])
+ expect(inlineSpans('<a href="https://x.com/a">![i](p.png) \\[1\\]</a>')).toEqual([
+ { text: '![i](p.png) [1]', link: 'https://x.com/a' },
+ ])
+ })
+
it('keeps balanced parentheses in a link target', () => {
expect(inlineSpans('[wiki](https://en.wikipedia.org/wiki/Foo_(bar)) end')).toEqual([
{ text: 'wiki', link: 'https://en.wikipedia.org/wiki/Foo_(bar)' },
diff --git a/mcp-host/src/workflow/inlineMarkup.ts b/mcp-host/src/workflow/inlineMarkup.ts
index 9951edb52..d7e321f1e 100644
--- a/mcp-host/src/workflow/inlineMarkup.ts
+++ b/mcp-host/src/workflow/inlineMarkup.ts
@@ -631,6 +631,16 @@ function withoutMarks(text: string): string {
return text.replace(NONCHARACTERS, '')
}
+/**
+ * Where the label of the link `token` ends: at its first bracket not escaped,
+ * so an escaped "](" inside the label is not read as the start of the target.
+ */
+function linkLabelEnd(token: string): number {
+ let at = 1
+ while (at < token.length && token[at] !== ']') at += token[at] === '\\' ? 2 : 1
+ return at
+}
+
/** The atom `token` as a span, before the emphasis around it is applied. */
function atomSpan(token: string): InlineSpan {
if (token.startsWith('![')) {
@@ -641,7 +651,7 @@ function atomSpan(token: string): InlineSpan {
}
}
if (token.startsWith('[')) {
- const split = token.indexOf('](')
+ const split = linkLabelEnd(token)
return {
text: decodeEntities(unescapeMarkdown(withoutMarks(token.slice(1, split)))),
link: decodeEntities(token.slice(split + 2, -1)),