Skip to content

Commit dbc1d29

Browse files
committed
fix: guard countMarkdownHeadings against non-string input
When a subtask returns to the main task, message.text can be a non-string truthy value (e.g. array or object). The existing falsy check (!text) lets these through, causing a TypeError on .replace(). Add a typeof check so non-string values safely return 0. Fixes #11881
1 parent 0892455 commit dbc1d29

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

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

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

11+
it("returns 0 for non-string truthy values", () => {
12+
// When subtasks return to main task, message.text can be a non-string value
13+
expect(countMarkdownHeadings(["# heading"] as unknown as string)).toBe(0)
14+
expect(countMarkdownHeadings({ text: "# heading" } as unknown as string)).toBe(0)
15+
expect(countMarkdownHeadings(42 as unknown as string)).toBe(0)
16+
expect(countMarkdownHeadings(true as unknown as string)).toBe(0)
17+
})
18+
1119
it("counts single and multiple headings", () => {
1220
expect(countMarkdownHeadings("# One")).toBe(1)
1321
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)