Skip to content

fix: guard countMarkdownHeadings against non-string input#11880

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/11878-markdown-heading-type-guard
Draft

fix: guard countMarkdownHeadings against non-string input#11880
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/11878-markdown-heading-type-guard

Conversation

@roomote-v0
Copy link
Contributor

@roomote-v0 roomote-v0 bot commented Mar 7, 2026

Related GitHub Issue

Closes: #11878

Description

This PR attempts to address Issue #11878. The crash (TypeError: n.replace is not a function) occurs in countMarkdownHeadings() when message.text is a non-string truthy value (e.g., an array) at runtime. The existing guard (if (!text)) only catches falsy values, so a truthy non-string passes through and .replace() throws.

Fix: Added a typeof text !== "string" check to the existing guard in countMarkdownHeadings(), so any non-string value returns 0 early.

Feedback and guidance are welcome.

Test Procedure

  • Added test cases in webview-ui/src/utils/__tests__/markdown.spec.ts that pass non-string values (number, array, object, boolean) cast as unknown as string to countMarkdownHeadings() and verify they return 0
  • All 6 tests pass: cd webview-ui && npx vitest run src/utils/__tests__/markdown.spec.ts

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue.
  • Scope: Changes are focused on the linked issue.
  • Self-Review: Performed a self-review.
  • Testing: New tests added covering non-string input scenarios.
  • Documentation Impact: No documentation updates required.
  • Contribution Guidelines: Read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

Minimal, defensive fix -- only two files changed.

Interactively review PR in Roo Code Cloud

…n-string input

When message.text is a non-string truthy value (e.g. an array), calling
.replace() on it throws TypeError. This adds a typeof check so non-string
values are handled safely.

Fixes #11878
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]

1 participant