背景
旧项目虽然是Vue的架子,但还是引入了Jquery,然后第三方的检测程序扫出了一个安全漏洞,就这样
解决
JQuery会自动执行返回的脚本,这个时候读取cookie、sessionStorage、localStorage,再以请求的方式发出去...
要想解决问题,首先要复现问题,这里借助一个mock服务器,github.com/xiaodun/sf-…
首先返回的content-type需要是一个"脚本",例如:
"Content-Type": "text/javascript",
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="height: 100px; border: 1px solid #000">12</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$.ajax("http://192.168.10.177:9192/example/api/1");
</script>
</html>
接口返回内容
然后就自动执行了
JQuery默认配置是这样的
jQuery.ajaxSetup( {
accepts: {
script: "text/javascript, application/javascript, " +
"application/ecmascript, application/x-ecmascript"
},
converters: {
"text script": function( text ) {
jQuery.globalEval( text );
return text;
}
}
} );
关键在于jQuery.globalEval( text );,只要去掉就好了,例如:
$.ajaxSetup({
converters: {
"text script": function (text) {
return text;
},
},
});
就可以解決掉这个问题了。