这些 javascript 方法可以被认为是 XSS 安全的吗?

fro*_*dev 5 html javascript xss single-page-application

我正在实现一个所谓的“单页应用程序”,它接受 JSON 作为输入。这也意味着所有 HTML 都在浏览器中呈现,而且所有模板(我使用淘汰赛)似乎都不受用户输入的影响,因为模板不是由后端动态构建的,而是静态嵌入在客户端中的。换句话说,我不做这样的事情:

echo '<input type="text" value="$var">'
Run Code Online (Sandbox Code Playgroud)

所以所有用户内容的渲染本质上都归结为这些 JS 方法:

document.createTextNode(userVar); // for displaying static text
inputElement.value = userVar; // for populating input fields
document.title = userVar; // some user input can be reflected in the doc title
window.history.pushState = ... // no user input is set here directly, but there are URIs where this could be set using an outside link
Run Code Online (Sandbox Code Playgroud)

所以现在的问题是:这些方法都是100% XSS 安全的吗?或者还有什么方法可以触发 XSS 攻击——如果“是”,那怎么做?

how*_*rek 3

我相信这四个功能是安全的。该document.createTextElement方法看起来是安全的,并且其他方法都无法将对象添加到 DOM。

为了发起 XSS 攻击,攻击者必须能够劫持现有脚本来运行任意代码(为什么 eval 是邪恶的)或通过<script>标签等向量插入自己的脚本。由于您使用的方法无法将元素添加到 DOM,也无法操作事件处理程序,所以我认为您是安全的。

然而,我们还需要能够看到更多的后端代码才能进行该调用,但在前端看起来还不错。