Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cookies #419

Merged
merged 2 commits into from
Oct 3, 2016
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
16 changes: 11 additions & 5 deletions liberapay/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,28 @@ def is_card_expired(exp_year, exp_month):
return exp_year < cur_year or exp_year == cur_year and exp_month < cur_month


def ensure_str(s):
if isinstance(s, str):
return s
return s.decode('ascii') if isinstance(s, bytes) else s.encode('ascii')


def set_cookie(cookies, key, value, expires=None, httponly=True, path='/'):
key = str(key)
cookies[key] = str(value)
key = ensure_str(key)
cookies[key] = ensure_str(value)
cookie = cookies[key]
if expires:
if isinstance(expires, timedelta):
expires += utcnow()
if isinstance(expires, datetime):
expires = to_rfc822(expires)
cookie[str('expires')] = str(expires)
cookie[str('expires')] = ensure_str(expires)
if httponly:
cookie[str('httponly')] = True
if path:
cookie[str('path')] = str(path)
cookie[str('path')] = ensure_str(path)
if website.canonical_domain:
cookie[str('domain')] = str(website.canonical_domain)
cookie[str('domain')] = ensure_str(website.canonical_domain)
if website.canonical_scheme == 'https':
cookie[str('secure')] = True

Expand Down
16 changes: 16 additions & 0 deletions tests/py/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ def setUp(self):
Harness.setUp(self)
self.client.website.canonical_scheme = 'https'
self.client.website.canonical_host = 'example.com'
self._canonical_domain = self.client.website.canonical_domain
self.client.website.canonical_domain = b'.example.com'

def tearDown(self):
Harness.tearDown(self)
website = self.client.website
website.canonical_scheme = website.env.canonical_scheme
website.canonical_host = website.env.canonical_host
website.canonical_domain = self._canonical_domain

def test_canonize_canonizes(self):
response = self.client.GxT("/",
Expand Down Expand Up @@ -105,6 +108,19 @@ def test_i18n_subdomain_is_redirected_to_https(self):
assert not r.headers.cookie
assert r.headers[b'Location'] == b'https://en.example.com/'

def test_csrf_cookie_properties(self):
r = self.client.GET(
'/',
HTTP_X_FORWARDED_PROTO=b'https', HTTP_HOST=b'en.example.com',
csrf_token=None, raise_immediately=False,
)
assert r.code == 200
cookie = r.headers.cookie[csrf.CSRF_TOKEN]
assert cookie[str('domain')] == str('.example.com')
assert cookie[str('expires')][-4:] == str(' GMT')
assert cookie[str('path')] == str('/')
assert cookie[str('secure')] is True


class Tests2(Harness):

Expand Down