Skip to content

Commit

Permalink
fix issue #173 Session cookie expiration isn't locale-safe (#174)
Browse files Browse the repository at this point in the history
* fix issue #173 Session cookie expiration isn't locale-safe
  • Loading branch information
CastixGitHub authored and amol- committed May 23, 2019
1 parent 0103dbf commit 4a2cb74
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ language: python
os:
- linux
dist: xenial
addons:
apt:
packages:
- language-pack-it
- locales
services:
- mongodb
- memcached
Expand Down
15 changes: 14 additions & 1 deletion beaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from beaker.exceptions import BeakerException, InvalidCryptoBackendError
from beaker.cookie import SimpleCookie


months = (None, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
weekdays = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")


__all__ = ['SignedCookie', 'Session', 'InvalidSignature']


Expand Down Expand Up @@ -281,6 +287,13 @@ def _set_cookie_values(self, expires=None):

self._set_cookie_expires(expires)

@staticmethod
def serialize_cookie_date(v):
v = v.timetuple()
r = time.strftime("%%s, %d-%%s-%Y %H:%M:%S GMT", v)
return r % (weekdays[v[6]], months[v[1]])


def _set_cookie_expires(self, expires):
if expires is None:
expires = self.cookie_expires
Expand All @@ -300,7 +313,7 @@ def _set_cookie_expires(self, expires):
self.cookie[self.key]['expires'] = ''
return True
self.cookie[self.key]['expires'] = \
expires_date.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
self.serialize_cookie_date(expires_date)
return expires_date

def _update_cookie_out(self, set_cookie=True):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cookie_expires.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ def test_cookie_exprires_2():

assert no_expires is False, no_expires

def test_cookie_expires_different_locale():
from locale import setlocale, LC_TIME
expires_date = datetime.datetime(2019, 5, 22)
setlocale(LC_TIME, 'it_IT.UTF-8')
# if you get locale.Error: unsupported locale setting. you have to enable that locale in your OS.
assert expires_date.strftime("%a, %d-%b-%Y %H:%M:%S GMT").startswith('mer,')
session = Session({}, cookie_expires=True, validate_key='validate_key')
assert session._set_cookie_expires(expires_date)
expires = cookie_expiration(session)
assert expires == 'Wed, 22-May-2019 00:00:00 GMT', expires
setlocale(LC_TIME, '') # restore default locale for further tests

def test_set_cookie_expires():
"""Exhibit Set-Cookie: values."""
Expand Down

0 comments on commit 4a2cb74

Please sign in to comment.