Skip to content

Commit 293d464

Browse files
author
Bertan Ari
committed
fix(vscode-lm): fail closed on nested parameter tags and malformed backtick fences
Reject a leaked parameter value that itself contains parameter markup, and do not open a backtick fence whose info string contains a backtick (CommonMark 0.31.2). Adds a regression test pinning that an inline-code run before the wrapper is not a fence.
1 parent 7714c76 commit 293d464

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎src/api/providers/__tests__/vscode-lm.spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,20 @@ describe("leaked tool-call parser contracts", () => {
18061806
expect(callsOf(wrapLines("~~~\n```\n" + todo() + "\n"))).toHaveLength(0)
18071807
})
18081808

1809+
it("does not treat a closed inline-code run before the wrapper as a fence", () => {
1810+
const text = "text ```x``` " + `<function${"_calls"}>` + "\n" + todo()
1811+
1812+
expect(callsOf(text)).toHaveLength(1)
1813+
})
1814+
1815+
it("does not open a backtick fence whose info string contains a backtick", () => {
1816+
expect(callsOf("```lang`value\nmore\n" + wrapLines(todo()))).toHaveLength(1)
1817+
})
1818+
1819+
it("still opens a tilde fence whose info string contains a backtick", () => {
1820+
expect(callsOf("~~~lang`value\nmore\n" + wrapLines(todo()))).toHaveLength(0)
1821+
})
1822+
18091823
it("suppresses on an odd backtick count earlier in the line", () => {
18101824
expect(callsOf(wrapLines("see `" + todo()))).toHaveLength(0)
18111825
})
@@ -1874,6 +1888,21 @@ describe("leaked tool-call parser contracts", () => {
18741888
expect(convert({ value: { type: "object" } }, "5")).toHaveLength(0)
18751889
})
18761890

1891+
it("fails closed on a nested unclosed parameter tag and passes the text through", () => {
1892+
const schemas = schemaFor({ a: { type: "string" }, b: { type: "string" } })
1893+
const body = `<param${"eter"} name="a">` + param("b", "1") + "\n"
1894+
const text = wrapLines(`<in${"voke"} name="update_todo_list">\n${body}</in${"voke"}>`)
1895+
const { calls, leftoverText } = extractLeakedToolCalls(text, schemas)
1896+
1897+
expect(calls).toEqual([])
1898+
expect(leftoverText).toBe(text)
1899+
})
1900+
1901+
it("also rejects a declared-string value containing literal parameter markup", () => {
1902+
// Deliberate narrowing: failing closed beats dispatching a wrongly-parsed argument.
1903+
expect(convert({ value: { type: "string" } }, `see <param${"eter"} name="b">`)).toHaveLength(0)
1904+
})
1905+
18771906
it("rejects a number for a declared boolean", () => {
18781907
expect(convert({ value: { type: "boolean" } }, "5")).toHaveLength(0)
18791908
})

‎src/api/providers/vscode-lm.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ class QuotingScanState {
163163
}
164164
const marker = fenceMatch[1][0]
165165
const width = fenceMatch[1].length
166+
const suffix = this.sameLineBefore.slice(fenceMatch[0].length)
166167
if (!this.openFence) {
167-
return { marker, width }
168+
// CommonMark: a backtick opening fence's info string may not contain a backtick.
169+
return marker === "`" && suffix.includes("`") ? null : { marker, width }
168170
}
169171
// CommonMark allows an info string only on an opening fence, never a closing one.
170-
const suffix = this.sameLineBefore.slice(fenceMatch[0].length)
171172
const closesFence = marker === this.openFence.marker && width >= this.openFence.width && suffix.trim() === ""
172173
return closesFence ? null : this.openFence
173174
}
@@ -394,6 +395,11 @@ function parseLeakedInvokeParams(
394395
let consumedUpTo = 0
395396
for (const match of body.matchAll(paramPattern)) {
396397
const name = match[1]
398+
// A nested unclosed `<parameter` inside the captured value means the lazy pattern swallowed
399+
// markup as data and dropped the inner parameter, so fail closed rather than dispatch it.
400+
if (/<(?:antml:)?parameter\b/i.test(match[2])) {
401+
return undefined
402+
}
397403
const converted = convertLeakedParamValue(match[2].trim(), declaredParamType(schema, name))
398404
if (!converted) {
399405
return undefined

0 commit comments

Comments
 (0)