Skip to content

Commit ed1120d

Browse files
committed
MBS-10672: Fix AI chat HTML code output with XSS protection
1 parent b692f27 commit ed1120d

File tree

2 files changed

+447
-1
lines changed

2 files changed

+447
-1
lines changed

classes/base_purpose.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,14 @@ public function format_output(string $output): string {
175175
$output = str_replace('\\)', '\\\\)', $output);
176176
$output = str_replace('\\[', '\\\\[', $output);
177177
$output = str_replace('\\]', '\\\\]', $output);
178-
return format_text($output, FORMAT_MARKDOWN, ['filter' => false]);
178+
179+
// Use Moodle's core markdown_to_html() function.
180+
// It uses MarkdownExtra which already escapes HTML inside code blocks by default.
181+
$html = markdown_to_html($output);
182+
183+
// Final security layer: remove any dangerous HTML elements outside code blocks.
184+
// This prevents XSS from raw HTML that the LLM might return outside of code blocks.
185+
return purify_html($html);
179186
}
180187

181188
/**

0 commit comments

Comments
 (0)