METADATA 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. Metadata-Version: 2.0
  2. Name: django-cors-headers
  3. Version: 2.1.0
  4. Summary: django-cors-headers is a Django application for handling the server headers required for Cross-Origin Resource Sharing (CORS).
  5. Home-page: https://github.com/ottoyiu/django-cors-headers
  6. Author: Otto Yiu
  7. Author-email: otto@live.ca
  8. License: MIT License
  9. Keywords: django cors middleware rest api
  10. Platform: any
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Environment :: Web Environment
  13. Classifier: Framework :: Django
  14. Classifier: Framework :: Django :: 1.8
  15. Classifier: Framework :: Django :: 1.9
  16. Classifier: Framework :: Django :: 1.10
  17. Classifier: Framework :: Django :: 1.11
  18. Classifier: Intended Audience :: Developers
  19. Classifier: License :: OSI Approved :: MIT License
  20. Classifier: Operating System :: OS Independent
  21. Classifier: Programming Language :: Python
  22. Classifier: Programming Language :: Python :: 2
  23. Classifier: Programming Language :: Python :: 2.7
  24. Classifier: Programming Language :: Python :: 3
  25. Classifier: Programming Language :: Python :: 3.5
  26. Classifier: Programming Language :: Python :: 3.6
  27. Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
  28. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  29. django-cors-headers
  30. ===================
  31. A Django App that adds CORS (Cross-Origin Resource Sharing) headers to
  32. responses.
  33. Although JSON-P is useful, it is strictly limited to GET requests. CORS
  34. builds on top of ``XmlHttpRequest`` to allow developers to make cross-domain
  35. requests, similar to same-domain requests. Read more about it here:
  36. http://www.html5rocks.com/en/tutorials/cors/
  37. .. image:: https://travis-ci.org/ottoyiu/django-cors-headers.svg?branch=master
  38. :target: https://travis-ci.org/ottoyiu/django-cors-headers
  39. Requirements
  40. ------------
  41. Tested with all combinations of:
  42. * Python: 2.7, 3.6
  43. * Django: 1.8, 1.9, 1.10, 1.11
  44. Setup
  45. -----
  46. Install from **pip**:
  47. .. code-block:: sh
  48. pip install django-cors-headers
  49. and then add it to your installed apps:
  50. .. code-block:: python
  51. INSTALLED_APPS = (
  52. ...
  53. 'corsheaders',
  54. ...
  55. )
  56. You will also need to add a middleware class to listen in on responses:
  57. .. code-block:: python
  58. MIDDLEWARE = [ # Or MIDDLEWARE_CLASSES on Django < 1.10
  59. ...
  60. 'corsheaders.middleware.CorsMiddleware',
  61. 'django.middleware.common.CommonMiddleware',
  62. ...
  63. ]
  64. ``CorsMiddleware`` should be placed as high as possible, especially before any
  65. middleware that can generate responses such as Django's ``CommonMiddleware`` or
  66. Whitenoise's ``WhiteNoiseMiddleware``. If it is not before, it will not be able
  67. to add the CORS headers to these responses.
  68. Also if you are using ``CORS_REPLACE_HTTPS_REFERER`` it should be placed before
  69. Django's ``CsrfViewMiddleware`` (see more below).
  70. Configuration
  71. -------------
  72. Configure the middleware's behaviour in your Django settings. You must add the
  73. hosts that are allowed to do cross-site requests to
  74. ``CORS_ORIGIN_WHITELIST``, or set ``CORS_ORIGIN_ALLOW_ALL`` to ``True``
  75. to allow all hosts.
  76. ``CORS_ORIGIN_ALLOW_ALL``
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~
  78. If ``True``, the whitelist will not be used and all origins will be accepted.
  79. Defaults to ``False``.
  80. ``CORS_ORIGIN_WHITELIST``
  81. ~~~~~~~~~~~~~~~~~~~~~~~~~
  82. A list of origin hostnames that are authorized to make cross-site HTTP
  83. requests. The value ``'null'`` can also appear in this list, and will match the
  84. ``Origin: null`` header that is used in `"privacy-sensitive contexts"
  85. <https://tools.ietf.org/html/rfc6454#section-6>`_, such as when the client is
  86. running from a ``file://`` domain. Defaults to ``[]``.
  87. Example:
  88. .. code-block:: python
  89. CORS_ORIGIN_WHITELIST = (
  90. 'google.com',
  91. 'hostname.example.com',
  92. 'localhost:8000',
  93. '127.0.0.1:9000'
  94. )
  95. ``CORS_ORIGIN_REGEX_WHITELIST``
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. A list of regexes that match origin regex list of origin hostnames that are
  98. authorized to make cross-site HTTP requests. Defaults to ``[]``. Useful when
  99. ``CORS_ORIGIN_WHITELIST`` is impractical, such as when you have a large
  100. number of subdomains.
  101. Example:
  102. .. code-block:: python
  103. CORS_ORIGIN_REGEX_WHITELIST = (r'^(https?://)?(\w+\.)?google\.com$', )
  104. --------------
  105. The following are optional settings, for which the defaults probably suffice.
  106. ``CORS_URLS_REGEX``
  107. ~~~~~~~~~~~~~~~~~~~
  108. A regex which restricts the URL's for which the CORS headers will be sent.
  109. Defaults to ``r'^.*$'``, i.e. match all URL's. Useful when you only need CORS
  110. on a part of your site, e.g. an API at ``/api/``.
  111. Example:
  112. .. code-block:: python
  113. CORS_URLS_REGEX = r'^/api/.*$'
  114. ``CORS_ALLOW_METHODS``
  115. ~~~~~~~~~~~~~~~~~~~~~~
  116. A list of HTTP verbs that are allowed for the actual request. Defaults to:
  117. .. code-block:: python
  118. CORS_ALLOW_METHODS = (
  119. 'DELETE',
  120. 'GET',
  121. 'OPTIONS',
  122. 'PATCH',
  123. 'POST',
  124. 'PUT',
  125. )
  126. The default can be imported as ``corsheaders.defaults.default_methods`` so you
  127. can just extend it with your custom methods. This allows you to keep up to date
  128. with any future changes. For example:
  129. .. code-block:: python
  130. from corsheaders.defaults import default_methods
  131. CORS_ALLOW_METHODS = default_methods + (
  132. 'POKE',
  133. )
  134. ``CORS_ALLOW_HEADERS``
  135. ~~~~~~~~~~~~~~~~~~~~~~
  136. The list of non-standard HTTP headers that can be used when making the actual
  137. request. Defaults to:
  138. .. code-block:: python
  139. CORS_ALLOW_HEADERS = (
  140. 'accept',
  141. 'accept-encoding',
  142. 'authorization',
  143. 'content-type',
  144. 'dnt',
  145. 'origin',
  146. 'user-agent',
  147. 'x-csrftoken',
  148. 'x-requested-with',
  149. )
  150. The default can be imported as ``corsheaders.defaults.default_headers`` so you
  151. can extend it with your custom headers. This allows you to keep up to date with
  152. any future changes. For example:
  153. .. code-block:: python
  154. from corsheaders.defaults import default_headers
  155. CORS_ALLOW_HEADERS = default_headers + (
  156. 'my-custom-header',
  157. )
  158. ``CORS_EXPOSE_HEADERS``
  159. ~~~~~~~~~~~~~~~~~~~~~~~
  160. The list of HTTP headers that are to be exposed to the browser. Defaults to
  161. ``[]``.
  162. ``CORS_PREFLIGHT_MAX_AGE``
  163. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  164. The number of seconds a client/browser can cache the preflight response. If
  165. this is 0 (or any falsey value), no max age header will be sent. Defaults to
  166. ``86400`` (one day).
  167. **Note:** A preflight request is an extra request that is made when making a
  168. "not-so-simple" request (e.g. ``Content-Type`` is not
  169. ``application/x-www-form-urlencoded``) to determine what requests the server
  170. actually accepts. Read more about it in the `HTML 5 Rocks CORS tutorial
  171. <https://www.html5rocks.com/en/tutorials/cors/>`_.
  172. ``CORS_ALLOW_CREDENTIALS``
  173. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  174. If ``True``, cookies will be allowed to be included in cross-site HTTP
  175. requests. Defaults to ``False``.
  176. ``CORS_MODEL``
  177. ~~~~~~~~~~~~~~
  178. If set, this should be the path to a model to look up allowed origins, in the
  179. form ``app.modelname``. Defaults to ``None``.
  180. The model should inherit from ``corsheaders.models.AbstractCorsModel`` and specify
  181. the allowed origin in the ``CharField`` called ``cors``.
  182. CSRF Integration
  183. ----------------
  184. Most sites will need to take advantage of the `Cross-Site Request Forgery
  185. protection <https://docs.djangoproject.com/en/dev/ref/csrf/>`_ that Django
  186. offers. CORS and CSRF are separate, and Django has no way of using your CORS
  187. configuration to exempt sites from the ``Referer`` checking that it does on
  188. secure requests. The way to do that is with its `CSRF_TRUSTED_ORIGINS setting
  189. <https://docs.djangoproject.com/en/dev/ref/settings/#csrf-trusted-origins>`_.
  190. For example:
  191. .. code-block:: python
  192. CORS_ORIGIN_WHITELIST = (
  193. 'read.only.com',
  194. 'change.allowed.com',
  195. )
  196. CSRF_TRUSTED_ORIGINS = (
  197. 'change.allowed.com',
  198. )
  199. ``CORS_REPLACE_HTTPS_REFERER``
  200. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  201. ``CSRF_TRUSTED_ORIGINS`` was introduced in Django 1.9, so users of earlier
  202. versions will need an alternate solution. If ``CORS_REPLACE_HTTPS_REFERER`` is
  203. ``True``, ``CorsMiddleware`` will change the ``Referer`` header to something
  204. that will pass Django's CSRF checks whenever the CORS checks pass. Defaults to
  205. ``False``.
  206. Note that unlike ``CSRF_TRUSTED_ORIGINS``, this setting does not allow you to
  207. distinguish between domains that are trusted to *read* resources by CORS and
  208. domains that are trusted to *change* resources by avoiding CSRF protection.
  209. With this feature enabled you should also add
  210. ``corsheaders.middleware.CorsPostCsrfMiddleware`` after
  211. ``django.middleware.csrf.CsrfViewMiddleware`` in your ``MIDDLEWARE_CLASSES`` to
  212. undo the ``Referer`` replacement:
  213. .. code-block:: python
  214. MIDDLEWARE_CLASSES = [
  215. ...
  216. 'corsheaders.middleware.CorsMiddleware',
  217. ...
  218. 'django.middleware.csrf.CsrfViewMiddleware',
  219. 'corsheaders.middleware.CorsPostCsrfMiddleware',
  220. ...
  221. ]
  222. Signals
  223. -------
  224. If you have a use case that requires more than just the above configuration,
  225. you can attach code to check if a given request should be allowed. For example,
  226. this can be used to read the list of origins you allow from a model. Attach any
  227. number of handlers to the ``check_request_enabled``
  228. `Django signal <https://docs.djangoproject.com/en/1.10/ref/signals/>`_, which
  229. provides the ``request`` argument (use ``**kwargs`` in your handler to protect
  230. against any future arguments being added). If any handler attached to the
  231. signal returns a truthy value, the request will be allowed.
  232. For example you might define a handler like this:
  233. .. code-block:: python
  234. # myapp/handlers.py
  235. from corsheaders.signals import check_request_enabled
  236. from .models import MySite
  237. def cors_allow_mysites(sender, request, **kwargs):
  238. return MySite.objects.filter(host=request.host).exists()
  239. check_request_enabled.connect(cors_allow_mysites)
  240. Then connect it at app ready time using a `Django AppConfig
  241. <https://docs.djangoproject.com/en/1.10/ref/applications/>`_:
  242. .. code-block:: python
  243. # myapp/__init__.py
  244. default_app_config = 'myapp.apps.MyAppConfig'
  245. .. code-block:: python
  246. # myapp/apps.py
  247. from django.apps import AppConfig
  248. class MyAppConfig(AppConfig):
  249. name = 'myapp'
  250. def ready(self):
  251. # Makes sure all signal handlers are connected
  252. from . import handlers # noqa
  253. A common use case for the signal is to allow *all* origins to access a subset
  254. of URL's, whilst allowing a normal set of origins to access *all* URL's. This
  255. isn't possible using just the normal configuration, but it can be achieved with
  256. a signal handler.
  257. First set ``CORS_ORIGIN_WHITELIST`` to the list of trusted origins that are
  258. allowed to access every URL, and then add a handler to
  259. ``check_request_enabled`` to allow CORS regardless of the origin for the
  260. unrestricted URL's. For example:
  261. .. code-block:: python
  262. # myapp/handlers.py
  263. from corsheaders.signals import check_request_enabled
  264. def cors_allow_api_to_everyone(sender, request, **kwargs):
  265. return request.path.startswith('/api/')
  266. check_request_enabled.connect(cors_allow_api_to_everyone)
  267. Credits
  268. -------
  269. ``django-cors-headers`` was created by Otto Yiu (`@ottoyiu
  270. <https://github.com/ottoyiu>`_) and has been worked on by `25+ contributors
  271. <https://github.com/ottoyiu/django-cors-headers/graphs/contributors>`_.
  272. Thanks to every contributor, and if you want to get involved please don't
  273. hesitate to make a pull request.
  274. History
  275. =======
  276. Pending
  277. -------
  278. * New release notes go here.
  279. 2.1.0 (2017-05-28)
  280. ------------------
  281. * Django 1.11 compatibility. There were no changes to the actual library code,
  282. so previous versions probably work, though they weren't properly tested on
  283. 1.11.
  284. 2.0.2 (2017-02-06)
  285. ------------------
  286. * Fix when the check for ``CORS_MODEL`` is done to allow it to properly add
  287. the headers and respond to ``OPTIONS`` requests.
  288. 2.0.1 (2017-01-29)
  289. ------------------
  290. * Add support for specifying 'null' in ``CORS_ORIGIN_WHITELIST``.
  291. 2.0.0 (2017-01-07)
  292. ------------------
  293. * Remove previously undocumented ``CorsModel`` as it was causing migration
  294. issues. For backwards compatibility, any users previously using ``CorsModel``
  295. should create a model in their own app that inherits from the new
  296. ``AbstractCorsModel``, and to keep using the same data, set the model's
  297. ``db_table`` to 'corsheaders_corsmodel'. Users not using ``CorsModel``
  298. will find they have an unused table that they can drop.
  299. * Make sure that ``Access-Control-Allow-Credentials`` is in the response if the
  300. client asks for it.
  301. 1.3.1 (2016-11-09)
  302. ------------------
  303. * Fix a bug with the single check if CORS enabled added in 1.3.0: on Django
  304. < 1.10 shortcut responses could be generated by middleware above
  305. ``CorsMiddleware``, before it processed the request, failing with an
  306. ``AttributeError`` for ``request._cors_enabled``. Also clarified the docs
  307. that ``CorsMiddleware`` should be kept as high as possible in your middleware
  308. stack, above any middleware that can generate such responses.
  309. 1.3.0 (2016-11-06)
  310. ------------------
  311. * Add checks to validate the types of the settings.
  312. * Add the 'Do Not Track' header ``'DNT'`` to the default for
  313. ``CORS_ALLOW_HEADERS``.
  314. * Add 'Origin' to the 'Vary' header of outgoing requests when not allowing all
  315. origins, as per the CORS spec. Note this changes the way HTTP caching works
  316. with your CORS-enabled responses.
  317. * Check whether CORS should be enabled on a request only once. This has had a
  318. minor change on the conditions where any custom signals will be called -
  319. signals will now always be called *before* ``HTTP_REFERER`` gets replaced,
  320. whereas before they could be called before and after. Also this attaches the
  321. attribute ``_cors_enabled`` to ``request`` - please take care that other
  322. code you're running does not remove it.
  323. 1.2.2 (2016-10-05)
  324. ------------------
  325. * Add ``CorsModel.__str__`` for human-readable text
  326. * Add a signal that allows you to add code for more intricate control over when
  327. CORS headers are added.
  328. 1.2.1 (2016-09-30)
  329. ------------------
  330. * Made settings dynamically respond to changes, and which allows you to import
  331. the defaults for headers and methods in order to extend them.
  332. 1.2.0 (2016-09-28)
  333. ------------------
  334. * Drop Python 2.6 support.
  335. * Drop Django 1.3-1.7 support, as they are no longer supported.
  336. * Confirmed Django 1.9 support (no changes outside of tests were necessary).
  337. * Added Django 1.10 support.
  338. * Package as a universal wheel.
  339. 1.1.0 (2014-12-15)
  340. ------------------
  341. * django-cors-header now supports Django 1.8 with its new application loading
  342. system! Thanks @jpadilla for making this possible and sorry for the delay in
  343. making a release.
  344. 1.0.0 (2014-12-13)
  345. ------------------
  346. django-cors-headers is all grown-up :) Since it's been used in production for
  347. many many deployments, I think it's time we mark this as a stable release.
  348. * Switching this middleware versioning over to semantic versioning
  349. * #46 add user-agent and accept-encoding default headers
  350. * #45 pep-8 this big boy up
  351. 0.13 (2014-08-14)
  352. -----------------
  353. * Add support for Python 3
  354. * Updated tests
  355. * Improved docuemntation
  356. * Small bugfixes
  357. 0.12 (2013-09-24)
  358. -----------------
  359. * Added an option to selectively enable CORS only for specific URLs
  360. 0.11 (2013-09-24)
  361. * Added the ability to specify a regex for whitelisting many origin hostnames
  362. at once
  363. 0.10 (2013-09-05)
  364. -----------------
  365. * Introduced port distinction for origin checking
  366. * Use ``urlparse`` for Python 3 support
  367. * Added testcases to project
  368. 0.06 (2013-02-18)
  369. -----------------
  370. * Add support for exposed response headers
  371. 0.05 (2013-01-26)
  372. -----------------
  373. * Fixed middleware to ensure correct response for CORS preflight requests
  374. 0.04 (2013-01-25)
  375. -----------------
  376. * Add ``Access-Control-Allow-Credentials`` control to simple requests
  377. 0.03 (2013-01-22)
  378. -----------------
  379. * Bugfix to repair mismatched default variable names
  380. 0.02 (2013-01-19)
  381. -----------------
  382. * Refactor/pull defaults into separate file
  383. 0.01 (2013-01-19)
  384. -----------------
  385. * Initial release