Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Add header to cache CORS preflight requests #466

Merged
merged 4 commits into from
Sep 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cliquet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'cliquet.cache_pool_size': 10,
'cliquet.cache_url': '',
'cliquet.cors_origins': '*',
'cliquet.cors_max_age_seconds': 3600,
'cliquet.eos': None,
'cliquet.eos_message': None,
'cliquet.eos_url': None,
Expand Down Expand Up @@ -130,6 +131,9 @@ def includeme(config):
Service.cors_origins = tuple(aslist(cors_origins))
Service.default_cors_headers = ('Backoff', 'Retry-After', 'Alert',
'Content-Length')
cors_max_age = settings['cliquet.cors_max_age_seconds']
Service.cors_max_age = int(cors_max_age) if cors_max_age else None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I prefer to have the "if" at the beginning of the line to ease readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well this can be discussed in this particular case :) It is a safety check and it allows the main flow to remain concise... Matter of taste?


Service.error_handler = lambda self, e: errors.json_error_handler(e)

# Heartbeat registry.
Expand Down
24 changes: 24 additions & 0 deletions cliquet/tests/resource/test_views_cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,27 @@ def test_present_on_bad_id_400_errors(self):
def test_present_on_unknown_url(self):
self.assert_expose_headers('PUT_JSON', '/unknown', [
'Alert', 'Backoff', 'Retry-After', 'Content-Length'], status=404)


class CORSMaxAgeTest(BaseWebTest, unittest.TestCase):
def setUp(self):
super(CORSMaxAgeTest, self).setUp()
self.headers.update({
'Origin': 'lolnet.org',
'Access-Control-Request-Method': 'GET'
})

def test_cors_max_age_is_3600_seconds_by_default(self):
app = self.get_test_app()
resp = app.options('/', headers=self.headers)
self.assertEqual(int(resp.headers['Access-Control-Max-Age']), 3600)

def test_cors_max_age_can_be_specified_in_settings(self):
app = self.get_test_app({'cliquet.cors_max_age_seconds': '42'})
resp = app.options('/', headers=self.headers)
self.assertEqual(int(resp.headers['Access-Control-Max-Age']), 42)

def test_cors_max_age_is_disabled_if_unset(self):
app = self.get_test_app({'cliquet.cors_max_age_seconds': ''})
resp = app.options('/', headers=self.headers)
self.assertNotIn('Access-Control-Max-Age', resp.headers)
14 changes: 14 additions & 0 deletions cliquet_docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@ Basically, this will add both ``Cache-Control: max-age=3600`` and
``Expire: <server datetime + 1H>`` response headers to the ``GET`` responses.


CORS
----

By default, CORS headers are cached by clients during 1H (``Access-Control-Max-Age``).

The duration can be set from settings. If set to empty or to 0, the header
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure it's true if it's set to 0, from the code I read in cornice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can be confident, I wrote a test :)

is not sent to clients.

.. code-block:: ini

cliquet.cors_max_age_seconds = 7200



.. _configuration-authentication:

Authentication
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
colander==1.0
cornice==1.0.0
cornice==1.1.0
dealer==2.0.4
iso8601==0.1.10
PasteDeploy==1.5.2
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

REQUIREMENTS = [
'colander',
'cornice >= 1.0', # Disable request binding.
'cornice >= 1.1', # Fix cache CORS
'dealer', # For git.revision
'python-dateutil',
'pyramid_multiauth >= 0.5', # Pluggable authz
Expand Down