如何保护 location.href 免受 JavaScript 中的跨站点脚本攻击?

taj*_*hal 7 javascript cross-site location-href fortify

在我的 javascript 函数中,我使用 location.href ,如下所示, location.href = "../Floder1/result.jsp";它工作正常,但是当我使用 fortify 工具时,它显示跨站点脚本which can result in the browser executing malicious code. 如何保护它免受跨站点脚本的影响。非常感谢您,我们将非常感谢您的回答。

Sna*_*rak 0

例如,当用户可以将数据放入网页或获取会话数据时,就会发生跨站点脚本

如何保护

永远不允许在您的网页中注入代码。因此,如果您有表单,请在服务器中检查它并在页面中打印之前对其进行解析。

您不应该允许页面内容被更改href. 你总是escape之前的数据!

阅读此答案location.href /sf/answers/1686254531/

样本

你有一个 iframe,它会随着 GET 变量而改变:

sample.tld/index.jsp?iframe=none.jsp
Run Code Online (Sandbox Code Playgroud)

我可以将 a 注入script到您的 iframe 中,因此您应该使用转义字符来保护它:

// Escape the characters in the server and send it to the client.
// So the variable GET iframe will be valid
Run Code Online (Sandbox Code Playgroud)