/** * Sanitizes HTML content by removing dangerous attributes and tags. * This is used to comply with CSP policies and prevent XSS. */ export function sanitizeHTML(html: string): string { if (!html) return ''; // 1. Remove script tags completely let sanitized = html.replace(/]*>[\s\S]*?<\/script>/gi, ''); // 2. Remove all "on*" event handler attributes (onclick, onload, etc.) // This handles attributes like onclick="alert(1)" or onclick='alert(1)' or onclick=alert(1) sanitized = sanitized.replace(/\s+on\w+\s*=\s*(?:'[^']*'|"[^"]*"|[^\s>]+)/gi, ''); // 3. Remove "javascript:" pseudo-protocols in href or src sanitized = sanitized.replace(/(href|src)\s*=\s*(?:'javascript:[^']*'|"javascript:[^"]*"|javascript:[^\s>]+)/gi, '$1="#"'); // 4. Remove