fix memory leak by only hooking dompurify once

This commit is contained in:
localhost 2026-06-29 08:42:54 +02:00
parent 865e4ad42c
commit 23d3cb7036
1 changed files with 28 additions and 28 deletions

View File

@ -8,6 +8,34 @@ import redis from '@/utils/redis';
const app = new Elysia() const app = new Elysia()
DOMPurify.addHook('afterSanitizeAttributes', function (node) {
if (node.tagName === 'A') {
const disallowedPatterns: RegExp[] = [
/\/playlist/i,
/\/hashtag\//i,
/\/live\//i,
/\/user\//i,
/\/shorts\//i,
/\/c\//i,
/\/@[^\/]/i
];
const href = node.getAttribute('href') || '';
const shouldConvertToSpan = disallowedPatterns.some(pattern =>
pattern.test(href)
);
if (shouldConvertToSpan) {
const span = node.ownerDocument.createElement('span');
span.innerHTML = node.innerHTML;
if (node.className) span.className = node.className;
node.parentNode?.replaceChild(span, node);
} else {
node.setAttribute('rel', 'nofollow noopener noreferrer');
}
}
})
interface processedVideo { interface processedVideo {
id: string; id: string;
title: string; title: string;
@ -74,34 +102,6 @@ app.get('/watch', async ({ query: { v }, set, redirect, error }) => {
.executeTakeFirst() .executeTakeFirst()
: null : null
DOMPurify.addHook('afterSanitizeAttributes', function (node) {
if (node.tagName === 'A') {
const disallowedPatterns: RegExp[] = [
/\/playlist/i,
/\/hashtag\//i,
/\/live\//i,
/\/user\//i,
/\/shorts\//i,
/\/c\//i,
/\/@[^\/]/i
];
const href = node.getAttribute('href') || '';
const shouldConvertToSpan = disallowedPatterns.some(pattern =>
pattern.test(href)
);
if (shouldConvertToSpan) {
const span = node.ownerDocument.createElement('span');
span.innerHTML = node.innerHTML;
if (node.className) span.className = node.className;
node.parentNode?.replaceChild(span, node);
} else {
node.setAttribute('rel', 'nofollow noopener noreferrer');
}
}
})
const html = await m(eta.render('./watch', { const html = await m(eta.render('./watch', {
transparency, transparency,
file, file,