对于“Refused to execute script from ” because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled.”问题的解决办法

前几天帮客户处理网站漏洞风险,上级部门要求处理这个问题。

远程Web应用程序不采取措施来减轻一类Web应用程序漏洞。

远程网络应用程序不设置X-Content-Options响应头。
X-Content-Options是Microsoft提出的一种缓解MIME类型攻击的方式,并且已经在Chrome和Safari中实现。

HTTP相应头X-Content-Options:nosniff

按照风险报告上的建议在nginx返回头中增加了:
X-Content-Type-Options:nosniff

对于“Refused to execute script from '' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.”问题的解决办法

今天早上客户找到我说网站上几个功能异常,看了一下都是统计以及广告之类的动态数据,chome浏览器报错如下:对于“Refused to execute script from '' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.”问题的解决办法

问题内容:“Refused to execute script from '' because its MIME ('text/html') is not executable, and strict MIME checking is enabled.”其实出错就是字面上的意思javascript请求的内容返回的MIME类型为text/html,不是可执行的文件,严格MIME类型检查被启用。

查看请求头,进行分析:

对于“Refused to execute script from '' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.”问题的解决办法

可以看到,请求正常响应,但是发现明明是javascript返回的 Content-Type却是text/html,但是引入了X-Content-Type-Options为nosniff。

分析此问题是因为这些js调用的接口是php接口,里面是直接使用了echo输出了返回结果。但是呢,由于上面我们开启了X-Content-Type-Options:nosniff,导致了自动识别MIME被关闭了,无法自动识别。jascript请求默认是text/javascript,而返回的php默认返回的是text/html,导致类型不匹配。因此我们有2个选择:

1:关闭X-Content-Type-Options:nosniff,这个所谓的漏洞没啥影响,但是别个既然提出解决,那么就一定要解决。所以只有严格代码,看第二个解决办法。

2:程序是php的,解决办法很简单

header("-type:text/javascript; charset=uft-8");

给php输出前加入Content-Type类型,OK解决了,脚本正常执行,一切搞定!