fixers.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # -*- coding: utf-8 -*-
  2. """
  3. werkzeug.contrib.fixers
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. .. versionadded:: 0.5
  6. This module includes various helpers that fix bugs in web servers. They may
  7. be necessary for some versions of a buggy web server but not others. We try
  8. to stay updated with the status of the bugs as good as possible but you have
  9. to make sure whether they fix the problem you encounter.
  10. If you notice bugs in webservers not fixed in this module consider
  11. contributing a patch.
  12. :copyright: Copyright 2009 by the Werkzeug Team, see AUTHORS for more details.
  13. :license: BSD, see LICENSE for more details.
  14. """
  15. try:
  16. from urllib import unquote
  17. except ImportError:
  18. from urllib.parse import unquote
  19. from werkzeug.http import parse_options_header, parse_cache_control_header, \
  20. parse_set_header
  21. from werkzeug.useragents import UserAgent
  22. from werkzeug.datastructures import Headers, ResponseCacheControl
  23. class CGIRootFix(object):
  24. """Wrap the application in this middleware if you are using FastCGI or CGI
  25. and you have problems with your app root being set to the cgi script's path
  26. instead of the path users are going to visit
  27. .. versionchanged:: 0.9
  28. Added `app_root` parameter and renamed from `LighttpdCGIRootFix`.
  29. :param app: the WSGI application
  30. :param app_root: Defaulting to ``'/'``, you can set this to something else
  31. if your app is mounted somewhere else.
  32. """
  33. def __init__(self, app, app_root='/'):
  34. self.app = app
  35. self.app_root = app_root
  36. def __call__(self, environ, start_response):
  37. # only set PATH_INFO for older versions of Lighty or if no
  38. # server software is provided. That's because the test was
  39. # added in newer Werkzeug versions and we don't want to break
  40. # people's code if they are using this fixer in a test that
  41. # does not set the SERVER_SOFTWARE key.
  42. if 'SERVER_SOFTWARE' not in environ or \
  43. environ['SERVER_SOFTWARE'] < 'lighttpd/1.4.28':
  44. environ['PATH_INFO'] = environ.get('SCRIPT_NAME', '') + \
  45. environ.get('PATH_INFO', '')
  46. environ['SCRIPT_NAME'] = self.app_root.strip('/')
  47. return self.app(environ, start_response)
  48. # backwards compatibility
  49. LighttpdCGIRootFix = CGIRootFix
  50. class PathInfoFromRequestUriFix(object):
  51. """On windows environment variables are limited to the system charset
  52. which makes it impossible to store the `PATH_INFO` variable in the
  53. environment without loss of information on some systems.
  54. This is for example a problem for CGI scripts on a Windows Apache.
  55. This fixer works by recreating the `PATH_INFO` from `REQUEST_URI`,
  56. `REQUEST_URL`, or `UNENCODED_URL` (whatever is available). Thus the
  57. fix can only be applied if the webserver supports either of these
  58. variables.
  59. :param app: the WSGI application
  60. """
  61. def __init__(self, app):
  62. self.app = app
  63. def __call__(self, environ, start_response):
  64. for key in 'REQUEST_URL', 'REQUEST_URI', 'UNENCODED_URL':
  65. if key not in environ:
  66. continue
  67. request_uri = unquote(environ[key])
  68. script_name = unquote(environ.get('SCRIPT_NAME', ''))
  69. if request_uri.startswith(script_name):
  70. environ['PATH_INFO'] = request_uri[len(script_name):] \
  71. .split('?', 1)[0]
  72. break
  73. return self.app(environ, start_response)
  74. class ProxyFix(object):
  75. """This middleware can be applied to add HTTP proxy support to an
  76. application that was not designed with HTTP proxies in mind. It
  77. sets `REMOTE_ADDR`, `HTTP_HOST` from `X-Forwarded` headers. While
  78. Werkzeug-based applications already can use
  79. :py:func:`werkzeug.wsgi.get_host` to retrieve the current host even if
  80. behind proxy setups, this middleware can be used for applications which
  81. access the WSGI environment directly.
  82. If you have more than one proxy server in front of your app, set
  83. `num_proxies` accordingly.
  84. Do not use this middleware in non-proxy setups for security reasons.
  85. The original values of `REMOTE_ADDR` and `HTTP_HOST` are stored in
  86. the WSGI environment as `werkzeug.proxy_fix.orig_remote_addr` and
  87. `werkzeug.proxy_fix.orig_http_host`.
  88. :param app: the WSGI application
  89. :param num_proxies: the number of proxy servers in front of the app.
  90. """
  91. def __init__(self, app, num_proxies=1):
  92. self.app = app
  93. self.num_proxies = num_proxies
  94. def get_remote_addr(self, forwarded_for):
  95. """Selects the new remote addr from the given list of ips in
  96. X-Forwarded-For. By default it picks the one that the `num_proxies`
  97. proxy server provides. Before 0.9 it would always pick the first.
  98. .. versionadded:: 0.8
  99. """
  100. if len(forwarded_for) >= self.num_proxies:
  101. return forwarded_for[-self.num_proxies]
  102. def __call__(self, environ, start_response):
  103. getter = environ.get
  104. forwarded_proto = getter('HTTP_X_FORWARDED_PROTO', '')
  105. forwarded_for = getter('HTTP_X_FORWARDED_FOR', '').split(',')
  106. forwarded_host = getter('HTTP_X_FORWARDED_HOST', '')
  107. environ.update({
  108. 'werkzeug.proxy_fix.orig_wsgi_url_scheme': getter('wsgi.url_scheme'),
  109. 'werkzeug.proxy_fix.orig_remote_addr': getter('REMOTE_ADDR'),
  110. 'werkzeug.proxy_fix.orig_http_host': getter('HTTP_HOST')
  111. })
  112. forwarded_for = [x for x in [x.strip() for x in forwarded_for] if x]
  113. remote_addr = self.get_remote_addr(forwarded_for)
  114. if remote_addr is not None:
  115. environ['REMOTE_ADDR'] = remote_addr
  116. if forwarded_host:
  117. environ['HTTP_HOST'] = forwarded_host
  118. if forwarded_proto:
  119. environ['wsgi.url_scheme'] = forwarded_proto
  120. return self.app(environ, start_response)
  121. class HeaderRewriterFix(object):
  122. """This middleware can remove response headers and add others. This
  123. is for example useful to remove the `Date` header from responses if you
  124. are using a server that adds that header, no matter if it's present or
  125. not or to add `X-Powered-By` headers::
  126. app = HeaderRewriterFix(app, remove_headers=['Date'],
  127. add_headers=[('X-Powered-By', 'WSGI')])
  128. :param app: the WSGI application
  129. :param remove_headers: a sequence of header keys that should be
  130. removed.
  131. :param add_headers: a sequence of ``(key, value)`` tuples that should
  132. be added.
  133. """
  134. def __init__(self, app, remove_headers=None, add_headers=None):
  135. self.app = app
  136. self.remove_headers = set(x.lower() for x in (remove_headers or ()))
  137. self.add_headers = list(add_headers or ())
  138. def __call__(self, environ, start_response):
  139. def rewriting_start_response(status, headers, exc_info=None):
  140. new_headers = []
  141. for key, value in headers:
  142. if key.lower() not in self.remove_headers:
  143. new_headers.append((key, value))
  144. new_headers += self.add_headers
  145. return start_response(status, new_headers, exc_info)
  146. return self.app(environ, rewriting_start_response)
  147. class InternetExplorerFix(object):
  148. """This middleware fixes a couple of bugs with Microsoft Internet
  149. Explorer. Currently the following fixes are applied:
  150. - removing of `Vary` headers for unsupported mimetypes which
  151. causes troubles with caching. Can be disabled by passing
  152. ``fix_vary=False`` to the constructor.
  153. see: http://support.microsoft.com/kb/824847/en-us
  154. - removes offending headers to work around caching bugs in
  155. Internet Explorer if `Content-Disposition` is set. Can be
  156. disabled by passing ``fix_attach=False`` to the constructor.
  157. If it does not detect affected Internet Explorer versions it won't touch
  158. the request / response.
  159. """
  160. # This code was inspired by Django fixers for the same bugs. The
  161. # fix_vary and fix_attach fixers were originally implemented in Django
  162. # by Michael Axiak and is available as part of the Django project:
  163. # http://code.djangoproject.com/ticket/4148
  164. def __init__(self, app, fix_vary=True, fix_attach=True):
  165. self.app = app
  166. self.fix_vary = fix_vary
  167. self.fix_attach = fix_attach
  168. def fix_headers(self, environ, headers, status=None):
  169. if self.fix_vary:
  170. header = headers.get('content-type', '')
  171. mimetype, options = parse_options_header(header)
  172. if mimetype not in ('text/html', 'text/plain', 'text/sgml'):
  173. headers.pop('vary', None)
  174. if self.fix_attach and 'content-disposition' in headers:
  175. pragma = parse_set_header(headers.get('pragma', ''))
  176. pragma.discard('no-cache')
  177. header = pragma.to_header()
  178. if not header:
  179. headers.pop('pragma', '')
  180. else:
  181. headers['Pragma'] = header
  182. header = headers.get('cache-control', '')
  183. if header:
  184. cc = parse_cache_control_header(header,
  185. cls=ResponseCacheControl)
  186. cc.no_cache = None
  187. cc.no_store = False
  188. header = cc.to_header()
  189. if not header:
  190. headers.pop('cache-control', '')
  191. else:
  192. headers['Cache-Control'] = header
  193. def run_fixed(self, environ, start_response):
  194. def fixing_start_response(status, headers, exc_info=None):
  195. headers = Headers(headers)
  196. self.fix_headers(environ, headers, status)
  197. return start_response(status, headers.to_wsgi_list(), exc_info)
  198. return self.app(environ, fixing_start_response)
  199. def __call__(self, environ, start_response):
  200. ua = UserAgent(environ)
  201. if ua.browser != 'msie':
  202. return self.app(environ, start_response)
  203. return self.run_fixed(environ, start_response)