Skip to content

Commit 2201b0b

Browse files
committed
Should fix bbangert#60 also adds tests to replicate the issue and avoid regressions
1 parent 18f4f1f commit 2201b0b

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

beaker/session.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,13 @@ def __init__(self, request, key='beaker.session.id', timeout=None,
535535
self._path = self.get('_path', '/')
536536
except:
537537
pass
538-
if self.timeout is not None and time.time() - \
539-
self['_accessed_time'] > self.timeout:
540-
self.clear()
538+
539+
if self.timeout is not None:
540+
now = time.time()
541+
last_accessed_time = self.get('_accessed_time', now)
542+
if now - last_accessed_time > self.timeout:
543+
self.clear()
544+
541545
self.accessed_dict = self.copy()
542546
self._create_cookie()
543547

tests/test_cookie_only.py

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import datetime
1+
import datetime, time
22
import re
33
import os
44

@@ -141,6 +141,65 @@ def invalidate_session_app(environ, start_response):
141141
res = app.get('/')
142142
assert 'expires=' not in res.headers.getall('Set-Cookie')[0]
143143

144+
145+
def test_changing_encrypt_key_with_timeout():
146+
COMMON_ENCRYPT_KEY = '666a19cf7f61c64c'
147+
DIFFERENT_ENCRYPT_KEY = 'hello-world'
148+
149+
options = {'session.encrypt_key': COMMON_ENCRYPT_KEY,
150+
'session.timeout': 300,
151+
'session.validate_key': 'hoobermas',
152+
'session.type': 'cookie'}
153+
app = TestApp(SessionMiddleware(simple_app, **options))
154+
res = app.get('/')
155+
assert 'The current value is: 1' in res, res
156+
157+
# Get the session cookie, so we can reuse it.
158+
cookies = res.headers['Set-Cookie']
159+
160+
# Check that we get the same session with the same cookie
161+
options = {'session.encrypt_key': COMMON_ENCRYPT_KEY,
162+
'session.timeout': 300,
163+
'session.validate_key': 'hoobermas',
164+
'session.type': 'cookie'}
165+
app = TestApp(SessionMiddleware(simple_app, **options))
166+
res = app.get('/', headers={'Cookie': cookies})
167+
assert 'The current value is: 2' in res, res
168+
169+
# Now that we are sure that it reuses the same session,
170+
# change the encrypt_key so that it is unable to understand the cookie.
171+
options = {'session.encrypt_key': DIFFERENT_ENCRYPT_KEY,
172+
'session.timeout': 300,
173+
'session.validate_key': 'hoobermas',
174+
'session.type': 'cookie'}
175+
app = TestApp(SessionMiddleware(simple_app, **options))
176+
res = app.get('/', headers={'Cookie': cookies})
177+
178+
# Let's check it created a new session as the old one is invalid
179+
# in the past it just crashed.
180+
assert 'The current value is: 1' in res, res
181+
182+
183+
def test_cookie_properly_expires():
184+
COMMON_ENCRYPT_KEY = '666a19cf7f61c64c'
185+
186+
options = {'session.encrypt_key': COMMON_ENCRYPT_KEY,
187+
'session.timeout': 1,
188+
'session.validate_key': 'hoobermas',
189+
'session.type': 'cookie'}
190+
app = TestApp(SessionMiddleware(simple_app, **options))
191+
res = app.get('/')
192+
assert 'The current value is: 1' in res, res
193+
194+
res = app.get('/')
195+
assert 'The current value is: 2' in res, res
196+
197+
# Wait session to expire and check it starts with a clean one
198+
time.sleep(1)
199+
res = app.get('/')
200+
assert 'The current value is: 1' in res, res
201+
202+
144203
if __name__ == '__main__':
145204
from paste import httpserver
146205
wsgi_app = SessionMiddleware(simple_app, {})

0 commit comments

Comments
 (0)