|
- import DOMPurify from 'dompurify';
-
- const ALLOWED_TAGS = [
- 'a',
- 'b',
- 'blockquote',
- 'br',
- 'caption',
- 'col',
- 'colgroup',
- 'div',
- 'em',
- 'h1',
- 'h2',
- 'h3',
- 'h4',
- 'h5',
- 'h6',
- 'hr',
- 'i',
- 'img',
- 'li',
- 'ol',
- 'p',
- 'span',
- 'strong',
- 'style',
- 'sub',
- 'sup',
- 'table',
- 'tbody',
- 'td',
- 'tfoot',
- 'th',
- 'thead',
- 'tr',
- 'u',
- 'ul'
- ];
-
- const ALLOWED_ATTR = ['href', 'title', 'target', 'rel', 'src', 'alt', 'width', 'height', 'colspan', 'rowspan', 'scope', 'class', 'style'];
-
- if (typeof window !== 'undefined') {
- DOMPurify.addHook('afterSanitizeAttributes', (node) => {
- if (node.tagName === 'A' && node.getAttribute('target') === '_blank') {
- node.setAttribute('rel', 'noopener noreferrer');
- }
- });
- }
-
- /**
- * Strict allow-list sanitize for API / i18n HTML before innerHTML.
- * Scripts, event handlers, iframes, and javascript: URLs are dropped.
- */
- export function sanitizeHtml(dirty) {
- if (dirty == null) {
- return '';
- }
- return DOMPurify.sanitize(String(dirty), {
- ALLOWED_TAGS,
- ALLOWED_ATTR,
- ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto):|[^a-z]|[a-z+.-]+(?:[^a-z+.-:]|$))/i
- });
- }
|