JavaScript 頁(yè)面重定向

2021-06-04 12:00 更新

1.3.1【必須】限定跳轉(zhuǎn)目標(biāo)地址

  • 使用白名單,限定重定向地址的協(xié)議前綴(默認(rèn)只允許HTTP、HTTPS)、域名(默認(rèn)只允許公司根域),或指定為固定值;

  • 適用場(chǎng)景包括,使用函數(shù)方法:location.href、window.open()、location.assign()、location.replace();賦值或更新HTML屬性:a.hrefifame.src、form.action、embed.src、object.data、link.href、area.href、input.formaction、button.formaction;

  1. // bad: 跳轉(zhuǎn)至外部可控的不可信地址
  2. const sTargetUrl = getURLParam("target");
  3. location.replace(sTargetUrl);
  4. // good: 白名單限定重定向地址
  5. function validURL(sUrl) {
  6. return !!((/^(https?:\/\/)?[\w\-.]+\.(qq|tencent)\.com($|\/|\\)/i).test(sUrl) || (/^[\w][\w/.\-_%]+$/i).test(sUrl) || (/^[/\\][^/\\]/i).test(sUrl));
  7. }
  8. const sTargetUrl = getURLParam("target");
  9. if (validURL(sTargetUrl)) {
  10. location.replace(sTargetUrl);
  11. }
  12. // good: 制定重定向地址為固定值
  13. const sTargetUrl = "http://www.qq.com";
  14. location.replace(sTargetUrl);
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)