Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion src/components/shared/MarkdownRenderer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect } from 'vitest';
import { markdownToHtml, clusterMarkdownLines } from './MarkdownRenderer';
import { markdownToHtml } from './MarkdownRenderer';
import DOMPurify from 'dompurify';

// Tests cover the pure markdownToHtml function (no DOM/React needed).

Expand Down Expand Up @@ -194,6 +195,84 @@ describe('markdownToHtml', () => {
expect(html).toContain('<th>A</th>');
expect(html).toContain('<td>1</td>');
});

// ── DOMPurify sanitization ───────────────────────────────────────────────────

it('sanitizes malicious HTML with DOMPurify using component config', () => {
const md = '<img src="x" onerror="alert(1)">';
const raw = markdownToHtml(md);
const sanitized = DOMPurify.sanitize(raw, {
ALLOWED_TAGS: [
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'p',
'br',
'strong',
'em',
'del',
'code',
'pre',
'ul',
'ol',
'li',
'blockquote',
'hr',
'a',
'img',
'table',
'thead',
'tbody',
'tr',
'th',
'td',
'input',
],
ALLOWED_ATTR: ['href', 'src', 'alt', 'class', 'target', 'rel', 'type', 'checked', 'disabled'],
});
expect(sanitized).not.toContain('onerror');
});

it('sanitizes script tags with DOMPurify using component config', () => {
const md = '<script>alert("XSS")</script>';
const raw = markdownToHtml(md);
const sanitized = DOMPurify.sanitize(raw, {
ALLOWED_TAGS: [
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'p',
'br',
'strong',
'em',
'del',
'code',
'pre',
'ul',
'ol',
'li',
'blockquote',
'hr',
'a',
'img',
'table',
'thead',
'tbody',
'tr',
'th',
'td',
'input',
],
ALLOWED_ATTR: ['href', 'src', 'alt', 'class', 'target', 'rel', 'type', 'checked', 'disabled'],
});
expect(sanitized).not.toContain('<script>');
});
});

// ── Clustering algorithm ───────────────────────────────────────────────────
Expand Down
1 change: 0 additions & 1 deletion src/components/shared/MarkdownRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ export interface MarkdownRendererProps {
export function MarkdownRenderer({ content, className = '' }: MarkdownRendererProps) {
const sanitizedHtml = useMemo(() => {
const raw = markdownToHtml(content);
if (typeof window === 'undefined') return raw;
return DOMPurify.sanitize(raw, {
ALLOWED_TAGS: [
'h1',
Expand Down
Loading