fix: escapa HTML do conteúdo no destaque de busca do chat
O destaque montava HTML com o texto cru da mensagem e injetava via dangerouslySetInnerHTML: <img onerror> numa mensagem executava script ao buscar um termo presente nela.
This commit is contained in:
1 parent
da9b5e1865
commit
9a0e0205dc
1 file changed
+13
-3
@@ -18,6 +18,16 @@ interface ChatMessageProps {
|
||||
messageIndex?: number;
|
||||
}
|
||||
|
||||
// O destaque de busca vira HTML (dangerouslySetInnerHTML): o texto da mensagem precisa ser escapado.
|
||||
function escapeHtml(text: string): string {
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
/**
|
||||
* Componente de mensagem do chat otimizado com React.memo
|
||||
*
|
||||
@@ -96,7 +106,7 @@ function ChatMessageComponent({ message, isStreaming = false, highlightTerm, hig
|
||||
|
||||
matches.forEach((match, idx) => {
|
||||
// Adicionar texto antes do match
|
||||
highlighted += content.substring(lastIndex, match.start);
|
||||
highlighted += escapeHtml(content.substring(lastIndex, match.start));
|
||||
|
||||
// Adicionar match com highlight (laranja para atual, amarelo para outros)
|
||||
const isCurrent = highlightIndex !== undefined && idx === highlightIndex;
|
||||
@@ -104,12 +114,12 @@ function ChatMessageComponent({ message, isStreaming = false, highlightTerm, hig
|
||||
? 'bg-orange-400/60 dark:bg-orange-500/60 px-0.5 rounded'
|
||||
: 'bg-yellow-200/50 dark:bg-yellow-800/50 px-0.5 rounded';
|
||||
|
||||
highlighted += `<mark class="${className}">${match.text}</mark>`;
|
||||
highlighted += `<mark class="${className}">${escapeHtml(match.text)}</mark>`;
|
||||
lastIndex = match.end;
|
||||
});
|
||||
|
||||
// Adicionar texto restante
|
||||
highlighted += content.substring(lastIndex);
|
||||
highlighted += escapeHtml(content.substring(lastIndex));
|
||||
return highlighted;
|
||||
}, [processedContent, highlightTerm, highlightIndex, message.role, isStreaming]);
|
||||
|
||||
|
||||
Reference in new issue
Block a user