news 2026/2/17 4:12:34

从头说下DOM XSS

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
从头说下DOM XSS

Demo此问题

1. 写个html 叫test.html吧

<!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>DOM XSS test:test.html</title> </head> <body> <h1>DOM XSS test:test.html</h1> <div id="app"></div> <script> const params = new URLSearchParams(location.search); const msg = params.get('msg') || 'hello'; console.log('msg =', msg); // ← 自检:确认读到 < ...> document.getElementById('app').innerHTML = `<p>${msg}</p>`; </script> </body> </html>

2 powershell 启动web

python -m http.server 8000

3 浏览器访问 localhost:8000/test.html?msg=<img%20src=x%20οnerrοr=alert(1)>

4 看到个弹窗

原因

它直接将用户可控的 URL 参数(msg)拼接到 innerHTML,未做任何转义或过滤。攻击者可以通过构造恶意的 msg 参数注入 JavaScript 代码,从而执行任意脚本。

运行中debug所见

修复方式

1. 输入验证与输出编码

  • 输入验证:对来自location,document.URL,document.referrer,window.name等的值进行严格校验,只允许预期格式(如数字、固定字符串)。
  • 输出编码:在插入 HTML 时使用合适的编码:
    • HTML 内容 →textContentinnerText
    • 属性值 →setAttribute()
    • URL → 使用encodeURIComponent()

2. 避免危险的 DOM API

  • 禁止使用
    • innerHTML,outerHTML,document.write()
  • 替代方案
    • 使用textContentcreateElement()+appendChild()来构建安全 DOM。
<script> const params = new URLSearchParams(location.search); const msg = params.get('msg') || 'hello'; const mode = (params.get('mode') || 'text').toLowerCase(); const app = document.getElementById('app'); // 方案 1:纯文本渲染(默认) function renderText(s){ const p = document.createElement('p'); p.textContent = s; // ✅ 不解析为 HTML app.replaceChildren(p); } // 方案 2:白名单消毒(示例实现,生产用成熟库更好) function renderSanitized(html){ const allowedTags = new Set(['b','i','em','strong','u','a','code','pre','br']); const allowedAttrs = { 'a': new Set(['href','title']) }; const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); const fragment = document.createDocumentFragment(); const walk = (node, outParent) => { if (node.nodeType === Node.TEXT_NODE) { outParent.appendChild(node.cloneNode()); return; } if (node.nodeType === Node.ELEMENT_NODE) { const tag = node.tagName.toLowerCase(); if (!allowedTags.has(tag)) { Array.from(node.childNodes).forEach(child => walk(child, outParent)); // 剥离不安全标签,仅保留文本/安全子节点 return; } const el = document.createElement(tag); for (const attr of Array.from(node.attributes)) { const name = attr.name.toLowerCase(), value = attr.value; if (name.startsWith('on')) continue; // 禁止事件属性 const allowSet = allowedAttrs[tag]; if (allowSet && !allowSet.has(name)) continue; if (tag === 'a' && name === 'href') { try { const u = new URL(value, location.origin); if (!['http:', 'https:', 'mailto:'].includes(u.protocol.toLowerCase())) continue; // 禁止 javascript:/data:/file:/vbscript: } catch (e) { continue; } } el.setAttribute(name, value); } Array.from(node.childNodes).forEach(child => walk(child, el)); outParent.appendChild(el); } }; Array.from(doc.body.childNodes).forEach(n => walk(n, fragment)); app.replaceChildren(fragment); } if (mode === 'sanitize') renderSanitized(msg); else renderText(msg); </script>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/16 3:30:42

YOLOv8深度性能评测:全面解析FPS、延迟与多维度效率指标评估策略

购买即可解锁300+YOLO优化文章,并且还有海量深度学习复现项目,价格仅需两杯奶茶的钱,别人有的本专栏也有! 文章目录 YOLOv8模型性能评估完全指南:FPS、推理时间与多维度指标精确测算 核心性能指标深度解析 FPS(帧率)与推理时间精确测算 多维度精度评估体系 完整代码实现…

作者头像 李华
网站建设 2026/2/15 21:09:55

**YOLOv12低照度检测革新:将SCINet作为可训练预处理主干的全链路指南

购买即可解锁300+YOLO优化文章,并且还有海量深度学习复现项目,价格仅需两杯奶茶的钱,别人有的本专栏也有! 文章目录 **YOLOv12低照度检测革新:将SCINet作为可训练预处理主干的全链路指南** **一、核心机制:SCINet如何为YOLOv12赋予“夜视仪”能力** **二、实现步骤:将S…

作者头像 李华
网站建设 2026/2/7 9:22:49

为什么你的多模态Agent测试总失败?Docker环境变量配置的4个致命误区

第一章&#xff1a;多模态 Agent 测试失败的根源剖析在构建和部署多模态 Agent 的过程中&#xff0c;测试阶段频繁出现不可预期的失败。这些失败往往并非源于单一模块的缺陷&#xff0c;而是系统各组件之间复杂交互所引发的连锁反应。深入分析其根本原因&#xff0c;有助于提升…

作者头像 李华