Skip to content

Commit

Permalink
Merge pull request #35150 from dimagi/em/formplayer-session-cookie-he…
Browse files Browse the repository at this point in the history
…aders

Set `secure` and `httponly` on `formplayer_session` cookie
  • Loading branch information
nospame authored Sep 30, 2024
2 parents 7fb17e3 + 1301079 commit 36c67e8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions corehq/apps/cloudcare/middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.utils.deprecation import MiddlewareMixin


FORMPLAYER_SESSION_COOKIE_NAME = 'formplayer_session'


Expand All @@ -27,4 +27,5 @@ def _set_formplayer_session_cookie(request, response):
couch_user = getattr(request, 'couch_user', None)
if couch_user:
if request.COOKIES.get(FORMPLAYER_SESSION_COOKIE_NAME) != couch_user.user_id:
response.set_cookie(FORMPLAYER_SESSION_COOKIE_NAME, couch_user.user_id)
response.set_cookie(FORMPLAYER_SESSION_COOKIE_NAME, couch_user.user_id,
httponly=settings.SESSION_COOKIE_HTTPONLY)
4 changes: 4 additions & 0 deletions corehq/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ def get_view_func(view_fn, view_kwargs):


class SecureCookiesMiddleware(MiddlewareMixin):
"""Sets `secure` flag for cookies on the response object.
Must be come before middleware that adds cookies, because of order and layering.
https://docs.djangoproject.com/en/4.2/topics/http/middleware/#middleware-order-and-layering
"""

def process_response(self, request, response):
if hasattr(response, 'cookies') and response.cookies:
Expand Down
3 changes: 1 addition & 2 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@

MIDDLEWARE = [
'corehq.middleware.NoCacheMiddleware',
'corehq.middleware.SecureCookiesMiddleware',
'corehq.middleware.SelectiveSessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
Expand All @@ -171,8 +172,6 @@
'no_exceptions.middleware.NoExceptionsMiddleware',
'corehq.apps.locations.middleware.LocationAccessMiddleware',
'corehq.apps.cloudcare.middleware.CloudcareMiddleware',
# middleware that adds cookies must come before SecureCookiesMiddleware
'corehq.middleware.SecureCookiesMiddleware',
'field_audit.middleware.FieldAuditMiddleware',
]

Expand Down

0 comments on commit 36c67e8

Please sign in to comment.