加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

防止在window.location中使用javascript

发布时间:2020-12-14 05:27:20 所属栏目:Windows 来源:网络整理
导读:我有一个页面,它从查询字符串中的参数重定向到url,如: page.html中?的redirectUrl =的index.html 在页面内我有这样的代码: window.localtion.href = redirectUrl; 通过参数使用重定向url是必须的.该页面包含安全的敏感数据.有人可以用 javascript制作网址
我有一个页面,它从查询字符串中的参数重定向到url,如:
page.html中?的redirectUrl =的index.html
在页面内我有这样的代码:
window.localtion.href = redirectUrl;
通过参数使用重定向url是必须的.该页面包含安全的敏感数据.有人可以用 javascript制作网址:
?page.html中的redirectUrl = JavaScript的:警报(的document.getElementById( “密码”)值)
安全数据可能被盗.

如何防止绕过javascript代码到window.localtion.href?

解决方法

您可以尝试将URL放在 anchor element中并检查协议:

var anchor = document.createElement("a");
anchor.href = redirectUrl;

if(anchor.protocol != "javascript:") {
    window.localtion.href = redirectUrl;
}

但是,我不确定浏览器支持对此有多好,因为MDN将其列为HTML5功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读