Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 10ac9ff

Browse files
committed
fix: guard countMarkdownHeadings against non-string input
When returning from a subtask to the parent task, message.text can be a non-string truthy value at runtime. The existing `if (!text)` guard only catches falsy values, so `.replace()` throws TypeError on non-strings. Add a `typeof text !== "string"` check to handle this defensively. Fixes #11879
1 parent 0892455 commit 10ac9ff

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

webview-ui/src/utils/__tests__/markdown.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ describe("markdown heading helpers", () => {
88
expect(countMarkdownHeadings("")).toBe(0)
99
})
1010

11+
it("returns 0 for non-string values", () => {
12+
// At runtime, message.text can be a non-string truthy value (e.g. when
13+
// returning from a subtask). The function must handle this gracefully.
14+
expect(countMarkdownHeadings(42 as unknown as string)).toBe(0)
15+
expect(countMarkdownHeadings([] as unknown as string)).toBe(0)
16+
expect(countMarkdownHeadings({} as unknown as string)).toBe(0)
17+
expect(countMarkdownHeadings(true as unknown as string)).toBe(0)
18+
expect(hasComplexMarkdown(123 as unknown as string)).toBe(false)
19+
})
20+
1121
it("counts single and multiple headings", () => {
1222
expect(countMarkdownHeadings("# One")).toBe(1)
1323
expect(countMarkdownHeadings("# One\nContent")).toBe(1)

webview-ui/src/utils/markdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Code fences are stripped before matching to avoid false positives.
55
*/
66
export function countMarkdownHeadings(text: string | undefined): number {
7-
if (!text) return 0
7+
if (!text || typeof text !== "string") return 0
88

99
// Remove fenced code blocks to avoid counting headings inside code
1010
const withoutCodeBlocks = text.replace(/```[\s\S]*?```/g, "")

0 commit comments

Comments
 (0)