浏览代码

HUE-5717 [backend] Some operating system incorrectly detect javascript mime-type as text/x-js instead of application/javascript

Prakash Ranade 8 年之前
父节点
当前提交
34388da
共有 2 个文件被更改,包括 21 次插入1 次删除
  1. 20 1
      desktop/core/src/desktop/middleware.py
  2. 1 0
      desktop/core/src/desktop/settings.py

+ 20 - 1
desktop/core/src/desktop/middleware.py

@@ -20,6 +20,7 @@ from __future__ import absolute_import
 import inspect
 import json
 import logging
+import mimetypes
 import os.path
 import re
 import tempfile
@@ -683,4 +684,22 @@ class ContentSecurityPolicyMiddleware(object):
     if self.secure_content_security_policy and not 'Content-Security-Policy' in response:
       response["Content-Security-Policy"] = self.secure_content_security_policy
 
-    return response
+    return response
+
+
+class MimeTypeJSFileFixStreamingMiddleware(object):
+  """
+  Middleware to detect and fix ".js" mimetype. SLES 11SP4 as example OS which detect js file
+  as "text/x-js" and if strict X-Content-Type-Options=nosniff is set then browser fails to
+  execute javascript file.
+  """
+  def __init__(self):
+    jsmimetypes = ['application/javascript', 'application/ecmascript']
+    if mimetypes.guess_type("dummy.js")[0] in jsmimetypes:
+      LOG.info('Unloading MimeTypeJSFileFixStreamingMiddleware')
+      raise exceptions.MiddlewareNotUsed
+
+  def process_response(self, request, response):
+    if request.path_info.endswith('.js'):
+      response['Content-Type'] = "application/javascript"
+    return response

+ 1 - 0
desktop/core/src/desktop/settings.py

@@ -162,6 +162,7 @@ MIDDLEWARE_CLASSES = [
 
     'django.middleware.http.ConditionalGetMiddleware',
     'axes.middleware.FailedLoginMiddleware',
+    'desktop.middleware.MimeTypeJSFileFixStreamingMiddleware',
 ]
 
 # if os.environ.get(ENV_DESKTOP_DEBUG):