Skip to content

Commit

Permalink
fix: Reintroduce escapeHTML to prevent raw HTML injection
Browse files Browse the repository at this point in the history
- Properly escape user-generated HTML to avoid DOM injection vulnerabilities.
- Ensures KaTeX rendering remains unaffected while blocking malicious content.
  • Loading branch information
calycekr committed Jan 13, 2025
1 parent 501bd5f commit 6a22478
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/components/chat/MarkdownRenderer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@
},
};
function escapeHTML(content: string) {
return content.replace(
/[<>&"']/g,
(x) =>
({
"<": "&lt;",
">": "&gt;",
"&": "&amp;",
"'": "&#39;",
'"': "&quot;",
}[x] || x)
);
}
function addInlineCitations(md: string, webSearchSources: WebSearchSource[] = []): string {
const linkStyle =
"color: rgb(59, 130, 246); text-decoration: none; hover:text-decoration: underline;";
Expand Down Expand Up @@ -153,6 +167,7 @@
},
extensions: [katexBlockExtension, katexInlineExtension],
renderer: {
html: (html) => escapeHTML(html),
link: (href, title, text) =>
`<a href="${href?.replace(/>$/, "")}" target="_blank" rel="noreferrer">${text}</a>`,
},
Expand Down

0 comments on commit 6a22478

Please sign in to comment.