我有一个页面,它从查询字符串中的参数重定向到一个 url,例如:
page.html?redirectUrl=index.html
在页面内我有这样的代码:
window.localtion.href = redirectUrl;
要求通过参数使用重定向url。该页面包含安全的敏感数据。有人可以使用 javascript 制作 url,例如:
page.html?redirectUrl=javascript:alert(document.getElementById("password").value)
安全数据可能被盗。
如何防止绕过 javascript 代码到 window.localtion.href?
最佳答案
您可以尝试将 URL 放在 anchor element 中并检查协议(protocol):
var anchor = document.createElement("a");
anchor.href = redirectUrl;
if(anchor.protocol != "javascript:") {
window.localtion.href = redirectUrl;
}
但是,我不确定浏览器对此的支持有多好,因为 MDN 将其列为 HTML5 功能。
关于javascript - 防止在 window.location 中使用 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14219238/