Skip to content

Commit 9330e86

Browse files
author
daewoongoh
committed
fix(history): escape task titles when the search matches only the prompt
highlightFzfMatch returned the raw text when there were no match positions. Since the search also covers the original prompt, a prompt-only match passed an unescaped title to dangerouslySetInnerHTML.
1 parent 3b6f2c1 commit 9330e86

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎webview-ui/src/components/history/__tests__/useTaskSearch.spec.tsx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,37 @@ describe("useTaskSearch", () => {
192192
expect(result.current.tasks).toEqual([expect.objectContaining({ id: "task-2" })])
193193
})
194194

195+
describe("with the real highlighter", () => {
196+
beforeEach(async () => {
197+
const actual = await vi.importActual<typeof import("@/utils/highlight")>("@/utils/highlight")
198+
mockHighlightFzfMatch.mockImplementation(actual.highlightFzfMatch)
199+
})
200+
201+
afterEach(() => {
202+
mockHighlightFzfMatch.mockImplementation((text: string) => `<mark>${text}</mark>`)
203+
})
204+
205+
it.each([
206+
{ name: "only the original prompt", query: "unit tests" },
207+
{ name: "the title", query: "Docs" },
208+
])("escapes an HTML-bearing title when the search matches $name", ({ query }) => {
209+
mockUseExtensionState.mockReturnValue({
210+
taskHistory: [{ ...mockTaskHistory[1], autoTitle: '<a href="x">Docs</a>' }],
211+
cwd: "/workspace/project1",
212+
})
213+
const { result } = renderHook(() => useTaskSearch())
214+
215+
act(() => {
216+
result.current.setSearchQuery(query)
217+
})
218+
219+
expect(result.current.tasks).toEqual([expect.objectContaining({ id: "task-2" })])
220+
const highlight = (result.current.tasks[0] as { highlight?: string }).highlight
221+
expect(highlight).toContain("&lt;a href=&quot;x&quot;&gt;")
222+
expect(highlight).not.toContain("<a ")
223+
})
224+
})
225+
195226
it("keeps match positions from a custom title that is longer than the task", () => {
196227
// "Write unit tests" (16 chars) vs "Coverage sprint with deep tests" (31 chars):
197228
// with the old task.length bound, every position of "deep" (21-24) would be filtered out.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { highlightFzfMatch } from "../highlight"
2+
3+
describe("highlightFzfMatch", () => {
4+
it.each([
5+
{ name: "no match positions", positions: [], expected: "&lt;b&gt;x&lt;/b&gt;" },
6+
{
7+
name: "match positions",
8+
positions: [3],
9+
expected: '&lt;b&gt;<span class="history-item-highlight">x</span>&lt;/b&gt;',
10+
},
11+
])("escapes HTML in the text with $name", ({ positions, expected }) => {
12+
expect(highlightFzfMatch("<b>x</b>", positions)).toBe(expected)
13+
})
14+
15+
it("wraps matched characters in the highlight class", () => {
16+
expect(highlightFzfMatch("abc", [0, 2], "hl")).toBe('<span class="hl">a</span>b<span class="hl">c</span>')
17+
})
18+
})

‎webview-ui/src/utils/highlight.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export function highlightFzfMatch(
2929
positions: number[],
3030
highlightClassName: string = "history-item-highlight",
3131
) {
32-
if (!positions.length) return text
32+
// The result is rendered as HTML, so unmatched text must be escaped too.
33+
if (!positions.length) return escapeHtml(text)
3334

3435
const parts: { text: string; highlight: boolean }[] = []
3536
let lastIndex = 0

0 commit comments

Comments
 (0)