Skip to content

Commit 4b59dfd

Browse files
author
Peter Bengtsson
authored
Search results content HTML is not escaped (#22782)
Part of #1207
1 parent 2b98629 commit 4b59dfd

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

lib/search/lunr-search.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function field(matchData, record, name) {
248248
if (!positions.length) return text
249249

250250
// Highlight the text
251-
return positions
251+
const highlighted = positions
252252
.map(([prev, start, end], i) => [
253253
text.slice(prev, start),
254254
mark(text.slice(start, end)),
@@ -257,6 +257,16 @@ function field(matchData, record, name) {
257257
.flat()
258258
.filter(Boolean)
259259
.join('')
260+
261+
// We can't HTML escape the content until AFTER all the matchData positions
262+
// have been processed otherwise, the positions should shift.
263+
// The only HTML that is OK to keep is <mark> and </mark>.
264+
return highlighted
265+
.replace(/&/g, '&amp;')
266+
.replace(/</g, '&lt;')
267+
.replace(/>/g, '&gt;')
268+
.replace(/&lt;mark&gt;/g, '<mark>')
269+
.replace(/&lt;\/mark&gt;/g, '</mark>')
260270
}
261271

262272
function mark(text) {

0 commit comments

Comments
 (0)