来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
113 lines
3.3 KiB
TypeScript
113 lines
3.3 KiB
TypeScript
/**
|
|
* SVG 清洗工具:管理员配置的自定义菜单图标(iconSvg)会通过
|
|
* dangerouslySetInnerHTML 渲染,必须先移除可执行内容,防止存储型 XSS。
|
|
*
|
|
* 清洗规则:
|
|
* - 移除 <script>、<foreignObject> 等危险元素
|
|
* - 移除所有 on* 事件属性
|
|
* - 移除 href / xlink:href 中的 javascript: 等危险协议
|
|
* - 移除 <use> 的外部引用(仅允许 #fragment 内部引用)
|
|
*/
|
|
|
|
const DANGEROUS_TAGS = new Set([
|
|
'script',
|
|
'foreignobject',
|
|
'iframe',
|
|
'object',
|
|
'embed',
|
|
'link',
|
|
'meta',
|
|
'base',
|
|
'form',
|
|
'input',
|
|
'style',
|
|
'animate',
|
|
'set',
|
|
'animatemotion',
|
|
'animatetransform',
|
|
]);
|
|
|
|
const URL_ATTRIBUTES = new Set(['href', 'xlink:href', 'src', 'data', 'action', 'formaction']);
|
|
|
|
function isSafeUrl(value: string): boolean {
|
|
// 故意剥离 C0 控制字符,避免 javascript: 等协议绕过
|
|
// eslint-disable-next-line no-control-regex -- strip C0 controls from URLs
|
|
const normalized = value.replace(/[\s\u0000-\u001f]+/g, '').toLowerCase();
|
|
return !(
|
|
normalized.startsWith('javascript:') ||
|
|
normalized.startsWith('vbscript:') ||
|
|
normalized.startsWith('data:')
|
|
);
|
|
}
|
|
|
|
function sanitizeElement(el: Element): void {
|
|
const tag = el.tagName.toLowerCase();
|
|
|
|
if (DANGEROUS_TAGS.has(tag)) {
|
|
el.remove();
|
|
return;
|
|
}
|
|
|
|
for (const attr of [...el.attributes]) {
|
|
const name = attr.name.toLowerCase();
|
|
const value = attr.value || '';
|
|
|
|
if (name.startsWith('on')) {
|
|
el.removeAttribute(attr.name);
|
|
continue;
|
|
}
|
|
|
|
if (URL_ATTRIBUTES.has(name) && !isSafeUrl(value)) {
|
|
el.removeAttribute(attr.name);
|
|
continue;
|
|
}
|
|
|
|
// <use> 只允许文档内引用(#id),禁止加载外部资源
|
|
if (tag === 'use' && (name === 'href' || name === 'xlink:href') && !value.trim().startsWith('#')) {
|
|
el.removeAttribute(attr.name);
|
|
}
|
|
}
|
|
|
|
for (const child of [...el.children]) {
|
|
sanitizeElement(child);
|
|
}
|
|
}
|
|
|
|
/** 基于正则的兜底清洗(DOMParser 不可用时,如测试环境) */
|
|
function sanitizeSvgWithRegex(svg: string): string {
|
|
return svg
|
|
.replace(/<\s*(script|foreignObject|iframe|object|embed|style)[\s\S]*?<\s*\/\s*\1\s*>/gi, '')
|
|
.replace(/<\s*(script|foreignObject|iframe|object|embed|style)[^>]*\/?>/gi, '')
|
|
.replace(/\son[a-z]+\s*=\s*"[^"]*"/gi, '')
|
|
.replace(/\son[a-z]+\s*=\s*'[^']*'/gi, '')
|
|
.replace(/\son[a-z]+\s*=\s*[^\s>]+/gi, '')
|
|
.replace(/((?:xlink:)?href)\s*=\s*(["'])\s*(?:javascript|vbscript|data):[^"']*\2/gi, '$1=$2$2');
|
|
}
|
|
|
|
/**
|
|
* 清洗管理员配置的 SVG 图标字符串。
|
|
* 返回可安全渲染的 SVG,非法或非 SVG 内容返回空字符串。
|
|
*/
|
|
export function sanitizeSvg(svg: string | undefined | null): string {
|
|
if (!svg || typeof svg !== 'string') return '';
|
|
const trimmed = svg.trim();
|
|
if (!trimmed.toLowerCase().includes('<svg')) return '';
|
|
|
|
if (typeof DOMParser === 'undefined') {
|
|
return sanitizeSvgWithRegex(trimmed);
|
|
}
|
|
|
|
try {
|
|
const doc = new DOMParser().parseFromString(trimmed, 'image/svg+xml');
|
|
if (doc.querySelector('parsererror')) {
|
|
return sanitizeSvgWithRegex(trimmed);
|
|
}
|
|
const root = doc.documentElement;
|
|
if (!root || root.tagName.toLowerCase() !== 'svg') return '';
|
|
sanitizeElement(root);
|
|
return new XMLSerializer().serializeToString(root);
|
|
} catch {
|
|
return sanitizeSvgWithRegex(trimmed);
|
|
}
|
|
}
|