如何防止XSS攻击角应用

发布于 2025-01-23 03:51:44 字数 240 浏览 2 评论 0原文

我的响应中有这个脚本< img src = x onerror =“ alert('taction')” ,在我执行此操作的.ts文件中 document.getElementById(“ help”)。innerhtml = this.help; 它正在前端执行并发出警报。建议如何停止此操作 使用内容安全性用作 但是没有用..有什么建议吗?请帮忙

I have this script in my response <img src=x onerror="alert('attack')" and in my .ts file i'm doing this
document.getElementById("help").innerHTML=this.help;
and it is executing in frontend and giving alert .. suggest how to stop this
using content-security-policy as

but no use.. any suggestions? please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2025-01-30 03:51:44

使用这样的消毒剂

constructor(private sanitizer: DomSanitizer) {
}    

public sanitize(value: string) {
    return this.sanitizer.bypassSecurityTrustHtml(value);
}

Use sanitizer, like this

constructor(private sanitizer: DomSanitizer) {
}    

public sanitize(value: string) {
    return this.sanitizer.bypassSecurityTrustHtml(value);
}
桃扇骨 2025-01-30 03:51:44

对于OnError事件属性,您需要使用事件侦听器重写,最好是在单独的JS文件中而不是内联。

如果它是未格式化的文本,则您的目标是使用InnerHTML插入,您应该使用TextContent方法,因为它是XSS安全的。另一个选择也是使用dompurify使其安全。

For the onerror event attribute you'll need to rewrite using an event listener instead, preferably in a separate js file and not inline.

If it is unformatted text you aim to insert with innerHTML you should use textContent method instead as it is XSS safe. Another option is also to use DOMPurify to make it XSS safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文