Skip to content

Commit

Permalink
Never set session cookies for API requests
Browse files Browse the repository at this point in the history
Implement a custom session interface that never sets session cookies
on API requests

Fixes freedomofpress#3876
  • Loading branch information
rjmackay committed Nov 25, 2018
1 parent 1b5ea7d commit bb835d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions securedrop/journalist_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from journalist_app.utils import get_source, logged_in
from models import Journalist
from store import Storage
from session_that_ignores_api import SessionThatIgnoresAPI

import typing
# https://www.python.org/dev/peps/pep-0484/#runtime-or-type-checking
Expand All @@ -40,6 +41,7 @@ def create_app(config):

app.config.from_object(config.JournalistInterfaceFlaskConfig)
app.sdconfig = config
app.session_interface = SessionThatIgnoresAPI()

csrf = CSRFProtect(app)
Environment(app)
Expand Down
21 changes: 21 additions & 0 deletions securedrop/session_that_ignores_api.py
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)

0 comments on commit bb835d0

Please sign in to comment.