Add style sanitizer to message renderer

This commit is contained in:
Cohee
2023-12-20 17:03:37 +02:00
parent c212a71425
commit 041b9d4b01
4 changed files with 849 additions and 7 deletions

View File

@@ -189,7 +189,7 @@ import { getBackgrounds, initBackgrounds, loadBackgroundSettings, background_set
import { hideLoader, showLoader } from './scripts/loader.js';
import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay.js';
import { loadMancerModels, loadOllamaModels, loadTogetherAIModels } from './scripts/textgen-models.js';
import { appendFileContent, hasPendingFileAttachment, populateFileAttachment } from './scripts/chats.js';
import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags } from './scripts/chats.js';
import { replaceVariableMacros } from './scripts/variables.js';
import { initPresetManager } from './scripts/preset-manager.js';
@@ -275,6 +275,22 @@ DOMPurify.addHook('afterSanitizeAttributes', function (node) {
}
});
DOMPurify.addHook("uponSanitizeAttribute", (_, data, config) => {
if (!config['MESSAGE_SANITIZE']) {
return;
}
switch (data.attrName) {
case 'class': {
if (data.attrValue) {
data.attrValue = data.attrValue.split(' ').map((v) => {
return "custom-" + v;
}).join(' ');
}
break;
}
}
});
// API OBJECT FOR EXTERNAL WIRING
window['SillyTavern'] = {};
@@ -1550,7 +1566,11 @@ function messageFormatting(mes, ch_name, isSystem, isUser) {
mes = mes.replace(new RegExp(`(^|\n)${ch_name}:`, 'g'), '$1');
}
mes = DOMPurify.sanitize(mes, { FORBID_TAGS: ['style'] });
/** @type {any} */
const config = { MESSAGE_SANITIZE: true, ADD_TAGS: ['custom-style'] };
mes = encodeStyleTags(mes);
mes = DOMPurify.sanitize(mes, config);
mes = decodeStyleTags(mes);
return mes;
}
@@ -3634,11 +3654,11 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
mesSendString = addChatsPreamble(mesSendString);
let combinedPrompt = beforeScenarioAnchor +
storyString +
afterScenarioAnchor +
mesExmString +
mesSendString +
generatedPromptCache;
storyString +
afterScenarioAnchor +
mesExmString +
mesSendString +
generatedPromptCache;
combinedPrompt = combinedPrompt.replace(/\r/gm, '');