1.3.1【必須】限定跳轉(zhuǎn)目標(biāo)地址
location.href
、window.open()
、location.assign()
、location.replace()
;賦值或更新HTML屬性:a.href
、ifame.src
、form.action
、embed.src
、object.data
、link.href
、area.href
、input.formaction
、button.formaction
;// bad: 跳轉(zhuǎn)至外部可控的不可信地址
const sTargetUrl = getURLParam("target");
location.replace(sTargetUrl);
// good: 白名單限定重定向地址
function validURL(sUrl) {
return !!((/^(https?:\/\/)?[\w\-.]+\.(qq|tencent)\.com($|\/|\\)/i).test(sUrl) || (/^[\w][\w/.\-_%]+$/i).test(sUrl) || (/^[/\\][^/\\]/i).test(sUrl));
}
const sTargetUrl = getURLParam("target");
if (validURL(sTargetUrl)) {
location.replace(sTargetUrl);
}
// good: 制定重定向地址為固定值
const sTargetUrl = "http://www.qq.com";
location.replace(sTargetUrl);
更多建議: