From 417376dcb65e4ecda40932a89fb96e341e60a60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Fri, 31 May 2024 15:00:36 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9AdefenseXSS=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95=E6=8F=90?= =?UTF-8?q?=E4=BE=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/utils/index.js | 49 ++++++++++++++++++++++++++++++ simple-mind-map/src/utils/xss.js | 48 ----------------------------- 2 files changed, 49 insertions(+), 48 deletions(-) delete mode 100644 simple-mind-map/src/utils/xss.js diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index b192b932..842e3cea 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -1476,3 +1476,52 @@ export const formatGetNodeGeneralization = data => { return [] } } + +/** + * 防御 XSS 攻击,过滤恶意 HTML 标签和属性 + * @param {string} text 需要过滤的文本 + * @returns {string} 过滤后的文本 + */ +export const defenseXSS = text => { + text = String(text) + + // 初始化结果变量 + let result = text + + // 使用正则表达式匹配 HTML 标签 + const match = text.match(/<(\S*?)[^>]*>.*?|<.*? \/>/g) + if (match == null) { + // 如果没有匹配到任何标签,则直接返回原始文本 + return text + } + + // 遍历匹配到的标签 + for (let value of match) { + // 定义白名单属性正则表达式(style、target、href) + const whiteAttrRegex = new RegExp(/(style|target|href)=["'][^"']*["']/g) + + // 定义黑名单href正则表达式(javascript:) + const aHrefBlackRegex = new RegExp(/href=["']javascript:/g) + + // 过滤 HTML 标签 + const filterHtml = value.replace( + // 匹配属性键值对(如:key="value") + /([a-zA-Z-]+)\s*=\s*["']([^"']*)["']/g, + text => { + // 如果属性值包含黑名单href或不在白名单中,则删除该属性 + if (aHrefBlackRegex.test(text) || !whiteAttrRegex.test(text)) { + return '' + } + + // 否则,保留该属性 + return text + } + ) + + // 将过滤后的标签替换回原始文本 + result = result.replace(value, filterHtml) + } + + // 返回最终结果 + return result +} diff --git a/simple-mind-map/src/utils/xss.js b/simple-mind-map/src/utils/xss.js deleted file mode 100644 index 91064208..00000000 --- a/simple-mind-map/src/utils/xss.js +++ /dev/null @@ -1,48 +0,0 @@ -/** - * 防御 XSS 攻击,过滤恶意 HTML 标签和属性 - * @param {string} text 需要过滤的文本 - * @returns {string} 过滤后的文本 - */ -export function defenseXSS(text) { - text = String(text) - - // 初始化结果变量 - let result = text; - - // 使用正则表达式匹配 HTML 标签 - const match = text.match(/<(\S*?)[^>]*>.*?|<.*? \/>/g); - if (match == null) { - // 如果没有匹配到任何标签,则直接返回原始文本 - return text; - } - - // 遍历匹配到的标签 - for (let value of match) { - // 定义白名单属性正则表达式(style、target、href) - const whiteAttrRegex = new RegExp(/(style|target|href)=["'][^"']*["']/g); - - // 定义黑名单href正则表达式(javascript:) - const aHrefBlackRegex = new RegExp(/href=["']javascript:/g); - - // 过滤 HTML 标签 - const filterHtml = value.replace( - // 匹配属性键值对(如:key="value") - /([a-zA-Z-]+)\s*=\s*["']([^"']*)["']/g, - (text) => { - // 如果属性值包含黑名单href或不在白名单中,则删除该属性 - if (aHrefBlackRegex.test(text) || !whiteAttrRegex.test(text)) { - return ""; - } - - // 否则,保留该属性 - return text; - } - ); - - // 将过滤后的标签替换回原始文本 - result = result.replace(value, filterHtml); - } - - // 返回最终结果 - return result; -} \ No newline at end of file