Skip to content

Commit 30367b7

Browse files
committed
Refactor client.py session cookie code. Remove session db access.
The original code did a session_db.exists test followed by a session_db.getall. Refactor does a getall and if a KeyError is thrown, handles the error. Most likely the session key will be found so exception handling won't be triggered. Added test case to test the exception code path and minor rearrangement of setup code.
1 parent cbde715 commit 30367b7

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

roundup/cgi/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ def __init__(self, client):
174174
re.sub('[^a-zA-Z]', '', client.instance.config.TRACKER_NAME)
175175
cookies = LiberalCookie(client.env.get('HTTP_COOKIE', ''))
176176
if self.cookie_name in cookies:
177-
if not self.session_db.exists(cookies[self.cookie_name].value):
177+
try:
178+
self._sid = cookies[self.cookie_name].value
179+
self._data = self.session_db.getall(self._sid)
180+
except KeyError:
178181
self._sid = None
179182
# remove old cookie
180183
self.client.add_cookie(self.cookie_name, None)
181-
else:
182-
self._sid = cookies[self.cookie_name].value
183-
self._data = self.session_db.getall(self._sid)
184184

185185
def _gen_sid(self):
186186
""" generate a unique session key """

test/test_liveserver.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ def setup_class(cls):
7373
cls.db.config.MAILHOST = "localhost"
7474
cls.db.config.MAIL_HOST = "localhost"
7575
cls.db.config.MAIL_DEBUG = "../_test_tracker_mail.log"
76+
77+
# added to enable csrf forgeries/CORS to be tested
7678
cls.db.config.WEB_CSRF_ENFORCE_HEADER_ORIGIN = "required"
7779
cls.db.config.WEB_ALLOWED_API_ORIGINS = "https://client.com"
80+
cls.db.config['WEB_CSRF_ENFORCE_HEADER_X-REQUESTED-WITH'] = "required"
7881

7982
# disable web login rate limiting. The fast rate of tests
8083
# causes them to trip the rate limit and fail.
8184
cls.db.config.WEB_LOGIN_ATTEMPTS_MIN = 0
8285

83-
cls.db.config['WEB_CSRF_ENFORCE_HEADER_X-REQUESTED-WITH'] = "required"
84-
8586
# enable static precompressed files
8687
cls.db.config.WEB_USE_PRECOMPRESSED_FILES = 1
8788

@@ -903,6 +904,36 @@ def test_cache_control_js(self):
903904
self.assertEqual(f.status_code, 200)
904905
self.assertEqual(f.headers['Cache-Control'], 'public, max-age=1209600')
905906

907+
def test_missing_session_key(self):
908+
'''Test case where we have an outdated session cookie. Make
909+
sure cookie is removed.
910+
'''
911+
session = requests.Session()
912+
session.headers.update({'Origin': 'http://localhost:9001'})
913+
914+
# login using form to get cookie
915+
login = {"__login_name": 'admin', '__login_password': 'sekrit',
916+
"@action": "login"}
917+
f = session.post(self.url_base()+'/', data=login)
918+
919+
# verify cookie is present and we are logged in
920+
self.assertIn('<b>Hello, admin</b>', f.text)
921+
self.assertIn('roundup_session_Roundupissuetracker',
922+
session.cookies)
923+
924+
f = session.get(self.url_base()+'/')
925+
self.assertIn('<b>Hello, admin</b>', f.text)
926+
927+
for cookie in session.cookies:
928+
if cookie.name == 'roundup_session_Roundupissuetracker':
929+
cookie.value = 'bad_cookie_no_chocolate'
930+
break
931+
932+
f = session.get(self.url_base()+'/')
933+
934+
self.assertNotIn('<b>Hello, admin</b>', f.text)
935+
self.assertNotIn('roundup_session_Roundupissuetracker', session.cookies)
936+
906937
def test_login_fail_then_succeed(self):
907938
# Set up session to manage cookies <insert blue monster here>
908939
session = requests.Session()

0 commit comments

Comments
 (0)