概述
点击劫持通过隐藏/半透明 iframe 诱导用户误点。Content-Security-Policy: frame-ancestors 指定允许嵌入当前页面的来源,现代方案优于过时的 X-Frame-Options,并支持更灵活的来源匹配与子域控制。
用法与示例
禁止任何嵌入
Content-Security-Policy: frame-ancestors 'none'
仅允许同源与特定父站
Content-Security-Policy: frame-ancestors 'self' https://partner.example
迁移建议:移除 X-Frame-Options,统一使用 CSP frame-ancestors
验证与检测
- 使用浏览器开发者工具与报表端点(
report-to/report-uri)观察被拒绝的嵌入尝试。 - 在受控测试页通过
进行验证。
工程建议
- 最小权限:默认
none或仅允许可信父页面;避免通配过度放宽。 - 与 SSO/后台协作:在确需嵌入的业务场景列出白名单并定期审查。
- 监控:逐步灰度收紧策略,记录影响并修复。
参考与验证
- MDN CSP frame-ancestors 文档:https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors
- web.dev CSP 指南:https://web.dev/articles/csp
- OWASP 点击劫持防护建议:https://owasp.org/www-community/attacks/Clickjacking

发表评论 取消回复