forked from freedomofpress/securedrop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Never set session cookies for API requests
Implement a custom session interface that never sets session cookies on API requests Fixes freedomofpress#3876
- Loading branch information
Showing
2 changed files
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from flask import sessions, request | ||
|
||
|
||
class SessionThatIgnoresAPI(sessions.SecureCookieSessionInterface): | ||
def should_set_cookie(self, app, session): | ||
"""Used by session backends to determine if a ``Set-Cookie`` header | ||
should be set for this session cookie for this response. | ||
Extended in this class to skip setting the session cookie | ||
for all API requests | ||
""" | ||
|
||
if request.path.split('/')[1] == 'api': | ||
# Session cookies are not relevant to API requests | ||
# so always return False | ||
return False | ||
else: | ||
# All other cases revert to standard behaviour | ||
return super( | ||
sessions.SecureCookieSessionInterface, | ||
self).should_set_cookie(app, session) |