Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): XSS risk when displaying the message

Elian Doran948f160d1479
diff --git a/apps/client/package.json b/apps/client/package.json
index 8010438651..54569f7588 100644
--- a/apps/client/package.json
+++ b/apps/client/package.json
@@ -50,6 +50,7 @@
"clsx": "2.1.1",
"color": "5.0.3",
"debounce": "3.0.0",
+ "dompurify": "3.3.3",
"draggabilly": "3.0.0",
"force-graph": "1.51.2",
"globals": "17.4.0",
@@ -80,6 +81,7 @@
"@ckeditor/ckeditor5-inspector": "5.0.0",
"@prefresh/vite": "2.4.12",
"@types/bootstrap": "5.2.10",
+ "@types/dompurify": "3.2.0",
"@types/jquery": "4.0.0",
"@types/leaflet": "1.9.21",
"@types/leaflet-gpx": "1.3.8",
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 630868668c..8cbf27d4d9 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1642,7 +1642,9 @@
"note_context_enabled": "Click to disable note context: {{title}}",
"note_context_disabled": "Click to include current note in context",
"no_provider_message": "No AI provider configured. Add one to start chatting.",
- "add_provider": "Add AI Provider"
+ "add_provider": "Add AI Provider",
+ "role_user": "You",
+ "role_assistant": "Assistant"
},
"sidebar_chat": {
"title": "AI Chat",
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
index 3441f3c84f..c7560bf7a6 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
@@ -1,5 +1,6 @@
import "./LlmChat.css";
+import DOMPurify from "dompurify";
import { marked } from "marked";
import { useMemo } from "preact/hooks";
@@ -19,6 +20,12 @@ marked.setOptions({
gfm: true // GitHub Flavored Markdown
});
+/** Parse markdown and sanitize the resulting HTML to prevent XSS. */
+function renderMarkdown(markdown: string): string {
+ const raw = marked.parse(markdown) as string;
+ return DOMPurify.sanitize(raw);
+}
+
interface Props {
message: StoredMessage;
isStreaming?: boolean;
@@ -65,7 +72,7 @@ function ToolCallCard({ toolCall }: { toolCall: ToolCall }) {
function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {
return blocks.map((block, idx) => {
if (block.type === "text") {
- const html = marked.parse(block.content) as string;
+ const html = renderMarkdown(block.content);
return (
<div key={idx}>
<div
@@ -92,7 +99,7 @@ export default function ChatMessage({ message, isStreaming }: Props) {
// Render markdown for assistant messages with legacy string content
const renderedContent = useMemo(() => {
if (message.role === "assistant" && !isError && !isThinking && typeof message.content === "string") {
- return marked.parse(message.content) as string;
+ return renderMarkdown(message.content);
}
return null;
}, [message.content, message.role, isError, isThinking]);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index a0fbf58129..bf6f26d116 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -287,6 +287,9 @@ importers:
debounce:
specifier: 3.0.0
version: 3.0.0
+ dompurify:
+ specifier: 3.3.3
+ version: 3.3.3
draggabilly:
specifier: 3.0.0
version: 3.0.0
@@ -372,6 +375,9 @@ importers:
'@types/bootstrap':
specifier: 5.2.10
version: 5.2.10
+ '@types/dompurify':
+ specifier: 3.2.0
+ version: 3.2.0
'@types/jquery':
specifier: 4.0.0
version: 4.0.0
@@ -6060,6 +6066,10 @@ packages:
resolution: {integrity: sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-Fgg31wv9QbLDA0SpTOXO3MaxySc4DKGLi8sna4/Utjo4r3ZRPdCt4UQee8BWr+Q5z21yifghREPJGYaEOEIACg==}
+ deprecated: This is a stub types definition. dompurify provides its own type definitions, so you do not need this installed.
+
resolution: {integrity: sha512-nv+GSx77ZtXiJzwKdsASqi+YQ5Z7vwHsTP0JY2SiQgjGckkBRKZnk8nIM+7oUZ1VCtuTz0+By4qVR7fqzp/Dfg==}
@@ -16993,8 +17003,6 @@ snapshots:
'@ckeditor/ckeditor5-utils': 47.6.1
'@ckeditor/ckeditor5-widget': 47.6.1
es-toolkit: 1.39.5
- transitivePeerDependencies:
- - supports-color
'@ckeditor/[email protected]':
dependencies:
@@ -17014,8 +17022,6 @@ snapshots:
'@ckeditor/ckeditor5-ui': 47.6.1
'@ckeditor/ckeditor5-utils': 47.6.1
ckeditor5: 47.6.1
- transitivePeerDependencies:
- - supports-color
'@ckeditor/[email protected]':
dependencies:
@@ -17513,8 +17519,6 @@ snapshots:
'@ckeditor/ckeditor5-widget': 47.6.1
ckeditor5: 47.6.1
es-toolkit: 1.39.5
- transitivePeerDependencies:
- - supports-color
'@ckeditor/[email protected]':
dependencies:
@@ -17539,8 +17543,6 @@ snapshots:
'@ckeditor/ckeditor5-utils': 47.6.1
ckeditor5: 47.6.1
es-toolkit: 1.39.5
- transitivePeerDependencies:
- - supports-color
'@ckeditor/[email protected]':
dependencies:
@@ -17578,8 +17580,6 @@ snapshots:
'@ckeditor/ckeditor5-utils': 47.6.1
'@ckeditor/ckeditor5-widget': 47.6.1
ckeditor5: 47.6.1
- transitivePeerDependencies:
- - supports-color
'@ckeditor/[email protected](patch_hash=5981fb59ba35829e4dff1d39cf771000f8a8fdfa7a34b51d8af9549541f2d62d)':
dependencies:
@@ -17589,8 +17589,6 @@ snapshots:
'@ckeditor/ckeditor5-utils': 47.6.1
ckeditor5: 47.6.1
es-toolkit: 1.39.5
- transitivePeerDependencies:
- - supports-color
'@ckeditor/[email protected]':
dependencies:
@@ -22177,6 +22175,10 @@ snapshots:
'@types/[email protected]': {}
+ '@types/[email protected]':
+ dependencies:
+ dompurify: 3.3.3
+
'@types/[email protected]': {}
'@types/[email protected]': {}
@@ -25371,8 +25373,6 @@ snapshots:
dependencies:
'@ckeditor/ckeditor5-collaboration-core': 47.6.1
- transitivePeerDependencies:
- - supports-color
dependencies: