Skip to content

Commit 9337af5

Browse files
committed
Fix(web): render search highlights as text nodes to prevent XSS
SearchResult inserted project text (file names, model descriptions) into the DOM via dangerouslySetInnerHTML after wrapping matches in <b> tags, so markup in that text could execute in the UI origin. highlightMatch now returns plain text parts, and a HighlightedText component renders them as React text nodes with matches wrapped in <b> elements. Both dangerouslySetInnerHTML assignments are removed. Signed-off-by: mday-io <mdaytn@gmail.com>
1 parent f6527ff commit 9337af5

3 files changed

Lines changed: 107 additions & 15 deletions

File tree

‎web/client/src/library/components/search/SearchList.tsx‎

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,31 @@ export default function SearchList<
281281
)
282282
}
283283

284+
export function HighlightedText({
285+
source,
286+
search,
287+
}: {
288+
source: string
289+
search: string
290+
}): JSX.Element {
291+
return (
292+
<>
293+
{highlightMatch(source, search).map(({ text, isMatch }, idx) =>
294+
isMatch ? (
295+
<b
296+
key={idx}
297+
className="inline-block text-brand-500 bg-brand-10"
298+
>
299+
{text}
300+
</b>
301+
) : (
302+
<Fragment key={idx}>{text}</Fragment>
303+
),
304+
)}
305+
</>
306+
)
307+
}
308+
284309
function SearchResult<T extends Record<string, any> = Record<string, any>>({
285310
item,
286311
index,
@@ -322,21 +347,23 @@ function SearchResult<T extends Record<string, any> = Record<string, any>>({
322347
<TBKModelName text={item[displayBy]}></TBKModelName>
323348
)}
324349
</span>
325-
<small
326-
className="block text-neutral-600 italic overflow-hidden whitespace-nowrap overflow-ellipsis"
327-
dangerouslySetInnerHTML={{
328-
__html: highlightMatch(index, search),
329-
}}
330-
></small>
350+
<small className="block text-neutral-600 italic overflow-hidden whitespace-nowrap overflow-ellipsis">
351+
<HighlightedText
352+
source={index}
353+
search={search}
354+
/>
355+
</small>
331356
</>
332357
) : (
333358
<span
334359
title={item[displayBy]}
335360
className="font-bold"
336-
dangerouslySetInnerHTML={{
337-
__html: highlightMatch(item[displayBy], search),
338-
}}
339-
></span>
361+
>
362+
<HighlightedText
363+
source={item[displayBy]}
364+
search={search}
365+
/>
366+
</span>
340367
)}
341368
{isNotNil(descriptionBy) && (
342369
<small className="block text-neutral-600 italic overflow-hidden whitespace-nowrap overflow-ellipsis">

‎web/client/src/library/components/search/help.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ export { filterListBy, highlightMatch }
33
export const EMPTY_STRING = ''
44
const MATCHED_LENGTH_TO_DISPLAY = 40
55

6-
function highlightMatch(source: string, match: string): string {
7-
return source.replaceAll(
8-
match,
9-
`<b class="inline-block text-brand-500 bg-brand-10">${match}</b>`,
10-
)
6+
export interface HighlightPart {
7+
text: string
8+
isMatch: boolean
9+
}
10+
11+
// NOTE: Returns plain text parts so callers can render them as React text nodes
12+
function highlightMatch(source: string, match: string): HighlightPart[] {
13+
if (match === EMPTY_STRING) return [{ text: source, isMatch: false }]
14+
15+
return source
16+
.split(match)
17+
.flatMap((text, idx) =>
18+
idx === 0
19+
? [{ text, isMatch: false }]
20+
: [
21+
{ text: match, isMatch: true },
22+
{ text, isMatch: false },
23+
],
24+
)
25+
.filter(part => part.text !== EMPTY_STRING)
1126
}
1227

1328
// NOTE: This function is used to filter the list of models in the search bar
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { render } from '../../../../tests/utils'
3+
import { HighlightedText } from '../SearchList'
4+
import { highlightMatch } from '../help'
5+
6+
describe('highlightMatch', () => {
7+
it('should split source into matched and unmatched parts', () => {
8+
expect(highlightMatch('abcab', 'ab')).toEqual([
9+
{ text: 'ab', isMatch: true },
10+
{ text: 'c', isMatch: false },
11+
{ text: 'ab', isMatch: true },
12+
])
13+
})
14+
15+
it('should return the whole source when there is no match', () => {
16+
expect(highlightMatch('abc', '')).toEqual([{ text: 'abc', isMatch: false }])
17+
expect(highlightMatch('abc', 'x')).toEqual([
18+
{ text: 'abc', isMatch: false },
19+
])
20+
})
21+
})
22+
23+
describe('HighlightedText', () => {
24+
it('should render project text as text, not HTML', () => {
25+
const source = '<img src=x onerror="alert(1)">model'
26+
const { container } = render(
27+
<HighlightedText
28+
source={source}
29+
search="model"
30+
/>,
31+
)
32+
33+
expect(container.querySelector('img')).toBeNull()
34+
expect(container.textContent).toBe(source)
35+
expect(container.querySelector('b')?.textContent).toBe('model')
36+
})
37+
38+
it('should not interpret markup in the search string', () => {
39+
const search = '<img src=x onerror="alert(1)">'
40+
const { container } = render(
41+
<HighlightedText
42+
source={`a${search}b`}
43+
search={search}
44+
/>,
45+
)
46+
47+
expect(container.querySelector('img')).toBeNull()
48+
expect(container.querySelector('b')?.textContent).toBe(search)
49+
})
50+
})

0 commit comments

Comments
 (0)