Skip to content

Commit 4a2cb74

Browse files
CastixGitHubamol-
authored andcommitted
fix issue #173 Session cookie expiration isn't locale-safe (#174)
* fix issue #173 Session cookie expiration isn't locale-safe
1 parent 0103dbf commit 4a2cb74

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ language: python
22
os:
33
- linux
44
dist: xenial
5+
addons:
6+
apt:
7+
packages:
8+
- language-pack-it
9+
- locales
510
services:
611
- mongodb
712
- memcached

beaker/session.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
from beaker.exceptions import BeakerException, InvalidCryptoBackendError
1010
from beaker.cookie import SimpleCookie
1111

12+
13+
months = (None, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
14+
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
15+
weekdays = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
16+
17+
1218
__all__ = ['SignedCookie', 'Session', 'InvalidSignature']
1319

1420

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

282288
self._set_cookie_expires(expires)
283289

290+
@staticmethod
291+
def serialize_cookie_date(v):
292+
v = v.timetuple()
293+
r = time.strftime("%%s, %d-%%s-%Y %H:%M:%S GMT", v)
294+
return r % (weekdays[v[6]], months[v[1]])
295+
296+
284297
def _set_cookie_expires(self, expires):
285298
if expires is None:
286299
expires = self.cookie_expires
@@ -300,7 +313,7 @@ def _set_cookie_expires(self, expires):
300313
self.cookie[self.key]['expires'] = ''
301314
return True
302315
self.cookie[self.key]['expires'] = \
303-
expires_date.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
316+
self.serialize_cookie_date(expires_date)
304317
return expires_date
305318

306319
def _update_cookie_out(self, set_cookie=True):

tests/test_cookie_expires.py

+11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ def test_cookie_exprires_2():
5656

5757
assert no_expires is False, no_expires
5858

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

6071
def test_set_cookie_expires():
6172
"""Exhibit Set-Cookie: values."""

0 commit comments

Comments
 (0)