feat(document-preview): enhance security by sanitizing HTML and Markdown content

- Integrated sanitizeHTML and safeMarkdownToHTML functions to ensure that rendered HTML and Markdown content is safe from XSS vulnerabilities.
- Updated the rendering logic for Excel and Markdown to sanitize the output, improving overall security in document previews.
This commit is contained in:
wizardchen
2026-04-16 15:15:20 +08:00
parent a1a07e6818
commit fb714ea883
2 changed files with 5 additions and 6 deletions
+5 -2
View File
@@ -6,6 +6,7 @@ import { MessagePlugin } from 'tdesign-vue-next';
import hljs from 'highlight.js'; import hljs from 'highlight.js';
import 'highlight.js/styles/github.css'; import 'highlight.js/styles/github.css';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { sanitizeHTML, safeMarkdownToHTML } from '@/utils/security';
const VueOfficePptx = defineAsyncComponent(() => import('@vue-office/pptx')); const VueOfficePptx = defineAsyncComponent(() => import('@vue-office/pptx'));
@@ -174,7 +175,7 @@ async function renderExcel(blob: Blob, fileType?: string) {
html += sheetHtml; html += sheetHtml;
html += `</div>`; html += `</div>`;
}); });
excelHtml.value = html; excelHtml.value = sanitizeHTML(html);
} }
async function renderText(blob: Blob, fileType: string) { async function renderText(blob: Blob, fileType: string) {
@@ -223,7 +224,9 @@ async function renderMarkdown(blob: Blob) {
return `<pre><code class="hljs">${highlighted}</code></pre>`; return `<pre><code class="hljs">${highlighted}</code></pre>`;
}; };
marked.use({ renderer }); marked.use({ renderer });
markdownHtml.value = marked.parse(text); const safeText = safeMarkdownToHTML(text);
const rawHtml = marked.parse(safeText);
markdownHtml.value = sanitizeHTML(rawHtml);
} }
function onImageLoad(e: Event) { function onImageLoad(e: Event) {
@@ -189,10 +189,6 @@ const renderToken = (token) => {
} }
}; };
const myMarkdown = (res) => {
return marked.parse(res, { renderer })
}
// 获取实际内容 // 获取实际内容
const getActualContent = () => { const getActualContent = () => {
return (props.content || props.session?.content || '').trim(); return (props.content || props.session?.content || '').trim();