【问题标题】:Refused to execute script because its MIME type ('application/gzip') is not executable, and strict MIME type checking is enabled拒绝执行脚本,因为它的 MIME 类型 ('application/gzip') 不可执行,并且启用了严格的 MIME 类型检查
【发布时间】:2020-06-30 16:45:07
【问题描述】:

我正在尝试在我的 react 应用程序生产服务器上设置头盔安全性。但每当我尝试点击 URL 时。我收到一条错误消息,提示 Refused to execute script from 'http://localhost:3000/static/js/app.378bd8b8eee930fb268c.js' because its MIME type ('application/gzip') is not executable, and strict MIME type checking is enabled.

对于压缩构建,我使用的是compression-webpack-plugin

当我删除 helmet 时,压缩构建工作得非常好。 头盔插件设置:

{"xssFilter": {"setOnOldIE": true}}

【问题讨论】:

  • 为什么要做XSS验证?我不明白那里的意思。你想做什么? http://localhost:3000/static/js/app.378bd8b8eee930fb268c.js 应该回复什么?
  • @AirOne 我正在使用它来保护我的应用程序免受 X-XSS-Protection
  • 正如在头盔文档中所说,XSS 过滤器并没有做太多的事情。我建议您不要使用头盔,而是手动将 X-XSS Protection 标头设置为 1; mode=block
  • @AirOne 你的想法奏效了(Y)。谢谢

标签: javascript node.js reactjs server helmet.js


【解决方案1】:

tl;dr:/static/js/app.378bd8b8eee930fb268c.js 正在发送 Content-Typeapplication/gzip,但它应该是 application/javascript


这里是 Helmet 的作者。这是因为 X-Content-Type-Options 标头,Helmet 自动将其设置为 nosniff。这告诉浏览器不要推断文件的类型,并信任服务器设置的Content-Type

正如您在屏幕截图中看到的那样,/static/js/app.378bd8b8eee930fb268c.jsContent-Typeapplication/gzip。浏览器拒绝将其解释为 JavaScript,因为它的 Content-Type 不是 application/javascript — 那是 X-Content-Type-Options 标头在起作用。

您可以通过解决该问题来解决您的问题——将您的 JavaScript 文件的 Content-Types 设置为 application/javascript,而不是 application/gzip

【讨论】:

  • 现在我得到:Refused to execute script from ‘http://localhost:3000/static/js/vendor.99d99f68deb71905f4ce.js’ because its MIME type (‘application/json’) is not executable, and strict MIME type checking is enabled.
  • 抱歉,我打错字了——应该是application/javascript,而不是application/json
猜你喜欢
  • 2014-09-28
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 2018-09-20
  • 2017-10-02
  • 2020-04-23
  • 2020-01-30
相关资源
最近更新 更多