URL任意跳转漏洞,又叫开放重定向漏洞,是一种常见的安全漏洞。当访问时用widow.href或者window.localtion指定跳转地址时,会报此漏洞。
网上的解决方案大致分为两种。
方法一:种是对跳转URL进行加密,使用方法
function validateURL(surl) {
var url = parseURL(surl);
var urlHostname = url.hostname.trim();
if (urlHostname == '') {
return true;
}
else {
if (urlHostname.toUpperCase() == location.hostname.trim().toUpperCase()) {
return true;
}
else
return false;
}
}
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':', ''),
hostname: a.hostname,
host: a.host,
port: a.port,
query: a.search,
params: (function () {
var ret = {},
seg = a.search.replace(/^\?/, '').split('&'),
len = seg.length, i = 0, s;
for (; i < len; i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
hash: a.hash.replace('#', ''),
path: a.pathname.replace(/^([^\/])/, '/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
segments: a.pathname.replace(/^\//, '').split('/')
};
}
此方法参考链接https://www.c-sharpcorner.com/article/preventing-open-redirection-attacks-in-c-sharp-and-javascript/
方法二:修改方案为模拟a标签点击跳转,同时也需要对URL进行加密处理可生效。
function createAElement(url) {
let a = document.createElement('a')//建立一个a标签
let urlObj = new URL(url, window.location.href);
let href = (encodeUrlParams(urlObj));
a.setAttribute("href", href);
a.setAttribute('target', "_self")//设置target属性,_blank打开新标签,_self在本标签打开// 模拟点击事件
a.click();
document.body.appendChild(a);
a.remove();
}
function encodeUrlParams(url) {
// 解析 URL
let urlObj = new URL(url);
// 获取查询参数
let params = new URLSearchParams(urlObj.search);
// 遍历查询参数
for (let [key, value] of params.entries()) {
params.delete(key);
params.set(encodeURIComponent(key), encodeURIComponent(value));
}
// 更新 URL 的查询部分
urlObj.search = params.toString();
// 返回更新后的 URL
return urlObj.href;
}
使用方法二的处理方案,成功解决了fotify扫描的问题。