一、背景

需要从当前页面的url里取得params,使用window.location.search()拿到参数,本地运行正常。
但在Checkmarx进行质量检测的时候被识别为有Reflected File Download安全漏洞的代码,无法pass。

二、调研

Reflected File Download是一种网络安全漏洞,它可能使攻击者通过从受信任的域(如 Google.com 和 Bing.com)虚拟下载文件来获得对受害者计算机的完全访问权限。
例如:

http://www.baidu.com/view?thisisfakeurl.bat

当用户点击这个连接后,浏览器可能会自动解析并下载.bat文件到用户电脑,从而造成危险。

三、解决

  1. 通过检索关键字,发现一种解决办法是从后端拿参数,避免window.location的字眼,但这样过于麻烦;
  2. searchParams.get(Key)

通过URL的api拿到query的固定字段来获值并避免RFD风险:

  1. new URL(location.href).searchParams.get(yourField)
  2. // Returns 2008 for href = "http://localhost/search.php?year=2008".
  3. // Or in two steps:
  4. const params = new URL(location.href).searchParams;
  5. const year = params.get('year');

然后可以顺利通过Checkmarx检测。