我正在实现一个名为“单页应用”的程序,它接受 JSON 格式的输入。这也意味着所有 HTML 都在浏览器中渲染,所有模板(我使用的是 Knockout)似乎不受用户输入的影响,这意味着模板不是通过后端动态构建的,而是静态地嵌入到客户端中。换句话说,我不会像下面这样做:
echo '<input type="text" value="$var">'
因此,所有用户内容的呈现基本上都可以归结为这些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
那么现在的问题是:这些方法是否完全安全,不会发生XSS攻击?还是仍然有可能触发XSS攻击 - 如果是的话,如何进行?