games_app/backend/src/utils/sanitizeHtml.ts
2026-07-07 15:03:17 +02:00

46 lines
986 B
TypeScript

import sanitizeHtml from 'sanitize-html';
const FAQ_ALLOWED_TAGS = [
'p',
'br',
'strong',
'b',
'em',
'i',
'u',
'ul',
'ol',
'li',
'a',
'img',
'video',
'iframe',
'h2',
'h3',
'blockquote',
];
const FAQ_ALLOWED_ATTRIBUTES: Record<string, string[]> = {
a: ['href', 'title', 'target', 'rel'],
img: ['src', 'alt', 'title'],
video: ['src', 'controls'],
iframe: ['src', 'allowfullscreen', 'title'],
};
/** Sanitizes staff-authored FAQ HTML before persistence or display. */
export function sanitizeFaqHtml(html: string): string {
return sanitizeHtml(html, {
allowedTags: FAQ_ALLOWED_TAGS,
allowedAttributes: FAQ_ALLOWED_ATTRIBUTES,
allowedSchemes: ['http', 'https', 'mailto'],
allowedSchemesByTag: {
iframe: ['http', 'https'],
img: ['http', 'https'],
video: ['http', 'https'],
},
transformTags: {
a: sanitizeHtml.simpleTransform('a', { rel: 'noopener noreferrer', target: '_blank' }),
},
});
}