Add option to forbid external images

This commit is contained in:
Cohee
2024-01-24 15:47:54 +02:00
parent 625a07ac1f
commit 4823bcf4ff
3 changed files with 31 additions and 0 deletions

View File

@ -296,6 +296,25 @@ DOMPurify.addHook('uponSanitizeAttribute', (_, data, config) => {
}
});
DOMPurify.addHook('uponSanitizeElement', (node, _, config) => {
if (!config['MESSAGE_SANITIZE']) {
return;
}
switch (node.tagName) {
case 'IMG': {
const isExternalUrl = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) && !url.startsWith(window.location.origin);
const src = node.getAttribute('src');
if (power_user.forbid_external_images && isExternalUrl(src)) {
console.warn('External image blocked', src);
node.remove();
}
}
break;
}
});
// API OBJECT FOR EXTERNAL WIRING
window['SillyTavern'] = {};