server/utils/sanitization.ts

77 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-04-07 06:16:54 +02:00
import { config } from "config-manager";
2024-04-07 16:05:06 +02:00
// import { sanitize } from "isomorphic-dompurify";
2023-10-17 00:03:29 +02:00
export const sanitizeHtml = async (html: string) => {
2024-04-07 16:05:06 +02:00
// TEMP: Allow all tags and attributes
return html;
/* const sanitizedHtml = sanitize(html, {
2024-04-07 07:30:49 +02:00
ALLOWED_TAGS: [
"a",
"p",
"br",
"b",
"i",
"em",
"strong",
"del",
"code",
"u",
"pre",
"ul",
"ol",
"li",
"blockquote",
],
ALLOWED_ATTR: [
"href",
"target",
"title",
"rel",
"class",
"start",
"reversed",
"value",
],
ALLOWED_URI_REGEXP: new RegExp(
`/^(?:(?:${config.validation.url_scheme_whitelist.join(
"|",
)}):|[^a-z]|[a-z+.-]+(?:[^a-z+.-:]|$))/i`,
),
USE_PROFILES: {
mathMl: true,
},
});
2023-10-17 00:03:29 +02:00
2024-04-07 07:30:49 +02:00
// Check text to only allow h-*, p-*, u-*, dt-*, e-*, mention, hashtag, ellipsis, invisible classes
const allowedClasses = [
"h-",
"p-",
"u-",
"dt-",
"e-",
"mention",
"hashtag",
"ellipsis",
"invisible",
];
2023-10-17 00:03:29 +02:00
2024-04-07 07:30:49 +02:00
return await new HTMLRewriter()
.on("*[class]", {
element(element) {
const classes = element.getAttribute("class")?.split(" ") ?? [];
2023-10-17 00:03:29 +02:00
2024-04-07 07:30:49 +02:00
for (const className of classes) {
if (
!allowedClasses.some((allowedClass) =>
className.startsWith(allowedClass),
)
) {
element.removeAttribute("class");
}
}
},
})
.transform(new Response(sanitizedHtml))
2024-04-07 16:05:06 +02:00
.text(); */
2023-10-17 00:03:29 +02:00
};