eez*_*eze 8 html javascript xss
If I am going to be displaying user-generated input on my site, is it safe enough to just display it by doing Element.innerText = "user input" in javascript, or do I need to additionally filter the input to prevent XSS?
'innerText' 会阻止 XSS 吗?
并非在所有情况下!在下面的摘录从OWASP基金会关于不安全的用法innerText:
一个被认为是安全的属性的例子是innerText。一些论文或指南主张将其用作 innerHTML 的替代品,以减轻 innerHTML 中的 XSS。但是,根据应用了innerText 的标签,可以执行代码。
内容提供了以下示例(为了清晰起见,我已对其进行了修改)
const tag = document.createElement("script");
tag.innerText = `console.log('Inner Text Used')`;
document.body.appendChild(tag); //executes codeRun Code Online (Sandbox Code Playgroud)
但是,在大多数情况下,innerText 是您用来防止 XSS 的方法,并且也记录在 OWASP 上:
...使用innerText/textContent。这样就解决了问题,是修复基于DOM的XSS漏洞的正确方法