Merge branch 'main' into login_page
This commit is contained in:
@@ -4,6 +4,8 @@ import { onMounted, ref, nextTick, onUnmounted, onUpdated, watch } from "vue";
|
||||
import { downKnowledgeDetails } from "@/api/knowledge-base/index";
|
||||
import { MessagePlugin } from "tdesign-vue-next";
|
||||
import picturePreview from '@/components/picture-preview.vue';
|
||||
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL } from '@/utils/security';
|
||||
|
||||
marked.use({
|
||||
mangle: false,
|
||||
headerIds: false,
|
||||
@@ -37,10 +39,16 @@ const checkImage = (url) => {
|
||||
});
|
||||
};
|
||||
renderer.image = function (href, title, text) {
|
||||
// 自定义HTML结构,图片展示带标题
|
||||
// 安全地处理图片链接
|
||||
if (!isValidImageURL(href)) {
|
||||
return `<p>无效的图片链接</p>`;
|
||||
}
|
||||
|
||||
// 使用安全的图片创建函数
|
||||
const safeImage = createSafeImage(href, text || '', title || '');
|
||||
return `<figure>
|
||||
<img class="markdown-image" src="${href}" alt="${title}" title="${text}">
|
||||
<figcaption style="text-align: left;">${text}</figcaption>
|
||||
${safeImage}
|
||||
<figcaption style="text-align: left;">${text || ''}</figcaption>
|
||||
</figure>`;
|
||||
};
|
||||
const props = defineProps(["visible", "details"]);
|
||||
@@ -66,14 +74,23 @@ watch(() => props.details.md, (newVal) => {
|
||||
deep: true
|
||||
})
|
||||
|
||||
// 处理 Markdown 中的图片
|
||||
// 安全地处理 Markdown 内容
|
||||
const processMarkdown = (markdownText) => {
|
||||
// 自定义渲染器处理图片
|
||||
if (!markdownText || typeof markdownText !== 'string') {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 首先对 Markdown 内容进行安全处理
|
||||
const safeMarkdown = safeMarkdownToHTML(markdownText);
|
||||
|
||||
// 使用安全的渲染器
|
||||
marked.use({ renderer });
|
||||
let html = marked.parse(markdownText);
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(html, 'text/html');
|
||||
return doc.body.innerHTML;
|
||||
let html = marked.parse(safeMarkdown);
|
||||
|
||||
// 使用 DOMPurify 进行最终的安全清理
|
||||
const sanitizedHTML = sanitizeHTML(html);
|
||||
|
||||
return sanitizedHTML;
|
||||
};
|
||||
const closePreImg = () => {
|
||||
reviewImg.value = false
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
/**
|
||||
* 安全工具类 - 防止 XSS 攻击
|
||||
*/
|
||||
|
||||
import DOMPurify from 'dompurify';
|
||||
|
||||
// 配置 DOMPurify 的安全策略
|
||||
const DOMPurifyConfig = {
|
||||
// 允许的标签
|
||||
ALLOWED_TAGS: [
|
||||
'p', 'br', 'strong', 'em', 'u', 's', 'del', 'ins',
|
||||
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
||||
'ul', 'ol', 'li', 'blockquote', 'pre', 'code',
|
||||
'a', 'img', 'table', 'thead', 'tbody', 'tr', 'th', 'td',
|
||||
'div', 'span', 'figure', 'figcaption'
|
||||
],
|
||||
// 允许的属性
|
||||
ALLOWED_ATTR: [
|
||||
'href', 'title', 'alt', 'src', 'class', 'id', 'style',
|
||||
'target', 'rel', 'width', 'height'
|
||||
],
|
||||
// 允许的协议
|
||||
ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i,
|
||||
// 禁止的标签和属性
|
||||
FORBID_TAGS: ['script', 'object', 'embed', 'form', 'input', 'button'],
|
||||
FORBID_ATTR: ['onerror', 'onload', 'onclick', 'onmouseover', 'onfocus', 'onblur'],
|
||||
// 其他安全配置
|
||||
KEEP_CONTENT: true,
|
||||
RETURN_DOM: false,
|
||||
RETURN_DOM_FRAGMENT: false,
|
||||
RETURN_DOM_IMPORT: false,
|
||||
SANITIZE_DOM: true,
|
||||
SANITIZE_NAMED_PROPS: true,
|
||||
WHOLE_DOCUMENT: false,
|
||||
// 自定义钩子函数
|
||||
HOOKS: {
|
||||
// 在清理前处理
|
||||
beforeSanitizeElements: (currentNode: Element) => {
|
||||
// 移除所有 script 标签
|
||||
if (currentNode.tagName === 'SCRIPT') {
|
||||
currentNode.remove();
|
||||
return null;
|
||||
}
|
||||
// 移除所有事件处理器
|
||||
const eventAttrs = ['onclick', 'onload', 'onerror', 'onmouseover', 'onfocus', 'onblur'];
|
||||
eventAttrs.forEach(attr => {
|
||||
if (currentNode.hasAttribute(attr)) {
|
||||
currentNode.removeAttribute(attr);
|
||||
}
|
||||
});
|
||||
},
|
||||
// 在清理后处理
|
||||
afterSanitizeElements: (currentNode: Element) => {
|
||||
// 确保所有链接都有 rel="noopener noreferrer"
|
||||
if (currentNode.tagName === 'A') {
|
||||
const href = currentNode.getAttribute('href');
|
||||
if (href && href.startsWith('http')) {
|
||||
currentNode.setAttribute('rel', 'noopener noreferrer');
|
||||
currentNode.setAttribute('target', '_blank');
|
||||
}
|
||||
}
|
||||
// 确保所有图片都有 alt 属性
|
||||
if (currentNode.tagName === 'IMG') {
|
||||
if (!currentNode.getAttribute('alt')) {
|
||||
currentNode.setAttribute('alt', '');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 安全地清理 HTML 内容
|
||||
* @param html 需要清理的 HTML 字符串
|
||||
* @returns 清理后的安全 HTML 字符串
|
||||
*/
|
||||
export function sanitizeHTML(html: string): string {
|
||||
if (!html || typeof html !== 'string') {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
return DOMPurify.sanitize(html, DOMPurifyConfig);
|
||||
} catch (error) {
|
||||
console.error('HTML sanitization failed:', error);
|
||||
// 如果清理失败,返回转义的纯文本
|
||||
return escapeHTML(html);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转义 HTML 特殊字符
|
||||
* @param text 需要转义的文本
|
||||
* @returns 转义后的文本
|
||||
*/
|
||||
export function escapeHTML(text: string): string {
|
||||
if (!text || typeof text !== 'string') {
|
||||
return '';
|
||||
}
|
||||
|
||||
const map: { [key: string]: string } = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
'/': '/',
|
||||
'`': '`',
|
||||
'=': '='
|
||||
};
|
||||
|
||||
return text.replace(/[&<>"'`=\/]/g, (s) => map[s]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证 URL 是否安全
|
||||
* @param url 需要验证的 URL
|
||||
* @returns 是否为安全 URL
|
||||
*/
|
||||
export function isValidURL(url: string): boolean {
|
||||
if (!url || typeof url !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const urlObj = new URL(url);
|
||||
// 只允许 http 和 https 协议
|
||||
return ['http:', 'https:'].includes(urlObj.protocol);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全地处理 Markdown 内容
|
||||
* @param markdown Markdown 文本
|
||||
* @returns 安全的 HTML 字符串
|
||||
*/
|
||||
export function safeMarkdownToHTML(markdown: string): string {
|
||||
if (!markdown || typeof markdown !== 'string') {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 首先转义可能的 HTML 标签
|
||||
const escapedMarkdown = markdown
|
||||
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
|
||||
.replace(/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi, '')
|
||||
.replace(/<object\b[^<]*(?:(?!<\/object>)<[^<]*)*<\/object>/gi, '')
|
||||
.replace(/<embed\b[^<]*(?:(?!<\/embed>)<[^<]*)*<\/embed>/gi, '');
|
||||
|
||||
return escapedMarkdown;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理用户输入
|
||||
* @param input 用户输入
|
||||
* @returns 清理后的安全输入
|
||||
*/
|
||||
export function sanitizeUserInput(input: string): string {
|
||||
if (!input || typeof input !== 'string') {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 移除控制字符
|
||||
let cleaned = input.replace(/[\x00-\x1F\x7F-\x9F]/g, '');
|
||||
|
||||
// 限制长度
|
||||
if (cleaned.length > 10000) {
|
||||
cleaned = cleaned.substring(0, 10000);
|
||||
}
|
||||
|
||||
return cleaned.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证图片 URL 是否安全
|
||||
* @param url 图片 URL
|
||||
* @returns 是否为安全的图片 URL
|
||||
*/
|
||||
export function isValidImageURL(url: string): boolean {
|
||||
if (!isValidURL(url)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否为图片文件
|
||||
const imageExtensions = /\.(jpg|jpeg|png|gif|webp|svg|bmp|ico)(\?.*)?$/i;
|
||||
return imageExtensions.test(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建安全的图片元素
|
||||
* @param src 图片源
|
||||
* @param alt 替代文本
|
||||
* @param title 标题
|
||||
* @returns 安全的图片 HTML
|
||||
*/
|
||||
export function createSafeImage(src: string, alt: string = '', title: string = ''): string {
|
||||
if (!isValidImageURL(src)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const safeSrc = escapeHTML(src);
|
||||
const safeAlt = escapeHTML(alt);
|
||||
const safeTitle = escapeHTML(title);
|
||||
|
||||
return `<img src="${safeSrc}" alt="${safeAlt}" title="${safeTitle}" class="markdown-image" style="max-width: 100%; height: auto;">`;
|
||||
}
|
||||
@@ -23,6 +23,8 @@ import { marked } from 'marked';
|
||||
import docInfo from './docInfo.vue';
|
||||
import deepThink from './deepThink.vue';
|
||||
import picturePreview from '@/components/picture-preview.vue';
|
||||
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL } from '@/utils/security';
|
||||
|
||||
marked.use({
|
||||
mangle: false,
|
||||
headerIds: false,
|
||||
@@ -89,36 +91,36 @@ const checkImage = (url) => {
|
||||
img.src = url;
|
||||
});
|
||||
};
|
||||
// 处理 Markdown 中的图片
|
||||
// 安全地处理 Markdown 内容
|
||||
const processMarkdown = (markdownText) => {
|
||||
// 自定义渲染器处理图片
|
||||
if (!markdownText || typeof markdownText !== 'string') {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 首先对 Markdown 内容进行安全处理
|
||||
const safeMarkdown = safeMarkdownToHTML(markdownText);
|
||||
|
||||
// 自定义安全的渲染器处理图片
|
||||
const renderer = {
|
||||
image(href, title, text) {
|
||||
return `<img src="${href}" alt="${text}" title="${title || ''}" class="markdown-image" style="max-width: 708px;height: 230px;">`;
|
||||
// 验证图片 URL 是否安全
|
||||
if (!isValidImageURL(href)) {
|
||||
return `<p>无效的图片链接</p>`;
|
||||
}
|
||||
// 使用安全的图片创建函数
|
||||
return createSafeImage(href, text || '', title || '');
|
||||
}
|
||||
};
|
||||
|
||||
marked.use({ renderer });
|
||||
|
||||
// 第一次渲染
|
||||
let html = marked.parse(markdownText);
|
||||
// 安全地渲染 Markdown
|
||||
let html = marked.parse(safeMarkdown);
|
||||
|
||||
// 创建虚拟 DOM 来操作
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(html, 'text/html');
|
||||
|
||||
// 检查所有图片
|
||||
// const images = doc.querySelectorAll('img');
|
||||
// images.forEach(async item => {
|
||||
// const isValid = await checkImage(item.src);
|
||||
// if (!isValid) {
|
||||
// item.remove();
|
||||
// }
|
||||
// });
|
||||
// if (props.isFirstEnter) {
|
||||
// emit('scroll-bottom')
|
||||
// }
|
||||
return doc.body.innerHTML;
|
||||
// 使用 DOMPurify 进行最终的安全清理
|
||||
const sanitizedHTML = sanitizeHTML(html);
|
||||
|
||||
return sanitizedHTML;
|
||||
};
|
||||
const handleImg = async (newVal) => {
|
||||
let index = newVal.lastIndexOf('![');
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
</template>
|
||||
<script setup>
|
||||
import { onMounted, watch, computed, ref, reactive, defineProps } from 'vue';
|
||||
import { sanitizeHTML } from '@/utils/security';
|
||||
|
||||
const isFold = ref(true)
|
||||
const props = defineProps({
|
||||
@@ -51,7 +52,6 @@ const showHide = () => {
|
||||
}
|
||||
const handlePanelChange = (val) => {
|
||||
isFold.value = !val.length ? true : false;
|
||||
|
||||
}
|
||||
</script>
|
||||
<style lang="less" scoped>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
trigger="click">
|
||||
<template #content>
|
||||
<div class="doc_content">
|
||||
<div v-html="item.content.replace(/\n/g, '<br/>')"></div>
|
||||
<div v-html="safeProcessContent(item.content)"></div>
|
||||
</div>
|
||||
</template>
|
||||
<span class="doc">
|
||||
@@ -28,6 +28,7 @@
|
||||
</template>
|
||||
<script setup>
|
||||
import { onMounted, defineProps, computed, ref, reactive } from "vue";
|
||||
import { sanitizeHTML } from '@/utils/security';
|
||||
const props = defineProps({
|
||||
// 必填项
|
||||
content: {
|
||||
@@ -44,6 +45,14 @@ const referBoxSwitch = () => {
|
||||
showReferBox.value = !showReferBox.value;
|
||||
};
|
||||
|
||||
// 安全地处理内容
|
||||
const safeProcessContent = (content) => {
|
||||
if (!content) return '';
|
||||
// 先进行安全清理,然后处理换行
|
||||
const sanitized = sanitizeHTML(content);
|
||||
return sanitized.replace(/\n/g, '<br/>');
|
||||
};
|
||||
|
||||
</script>
|
||||
<style lang="less" scoped>
|
||||
.refer {
|
||||
|
||||
Reference in New Issue
Block a user