Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tentative de Fix de l'erreur CSRF #585

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,13 @@
# Security
# ------------------------------------------------------------------------------

CSRF_COOKIE_HTTPONLY = True
SESSION_SERIALIZER = "lemarche.utils.session.JSONSerializer"
CSRF_USE_SESSIONS = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


CSRF_COOKIE_SECURE = True
SECURE_CONTENT_TYPE_NOSNIFF = True

SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True

SECURE_BROWSER_XSS_FILTER = True

Expand All @@ -364,7 +368,7 @@

SECURE_SSL_REDIRECT = env.bool("SECURE_SSL_REDIRECT", False)

SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
# SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"

SESSION_COOKIE_HTTPONLY = True

Expand Down
10 changes: 5 additions & 5 deletions config/settings/staging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
# for review apps
".cleverapps.io",
]
CSRF_TRUSTED_ORIGINS = [
"https://*.inclusion.beta.gouv.fr",
"https://bitoubi-django-staging.cleverapps.io",
"https://*.cleverapps.io",
]
# CSRF_TRUSTED_ORIGINS = [
# "https://*.inclusion.beta.gouv.fr",
# "https://bitoubi-django-staging.cleverapps.io",
# "https://*.cleverapps.io",
# ]

SECURE_SSL_REDIRECT = env.bool("SECURE_SSL_REDIRECT", True)

Expand Down
79 changes: 79 additions & 0 deletions lemarche/utils/sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import json
import uuid

from django.core.exceptions import PermissionDenied

from . import python


class SessionNamespace:
"""Class to facilitate the usage of namespaces inside the session."""

NOT_SET = python.Sentinel()

def __init__(self, session, namespace):
self._session = session
self.name = str(namespace)

def __repr__(self):
return f"<SessionNamespace({self._session[self.name]!r})>"

def __contains__(self, item):
return item in self._session[self.name]

def init(self, data):
self._session[self.name] = data
self._session.modified = True

def get(self, key, default=NOT_SET):
return self._session[self.name].get(key, default)

def set(self, key, value):
self._session[self.name][key] = value
self._session.modified = True

def update(self, data):
self._session[self.name].update(data)
self._session.modified = True

def exists(self):
return self.name in self._session

def delete(self):
if not self.exists():
return

del self._session[self.name]
self._session.modified = True

def save(self):
self._session.save()

def as_dict(self):
return dict(self._session[self.name])

@classmethod
def create_temporary(cls, session):
return cls(session, namespace=str(uuid.uuid4()))


class SessionNamespaceRequiredMixin:
required_session_namespaces = []

def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)

if not all(getattr(self, attr_name).exists() for attr_name in self.required_session_namespaces):
raise PermissionDenied("A session namespace doesn't exist.")


class JSONSerializer:
"""Class to be used in SESSION_SERIALIZER, so we can serialize data using our custom JSON encoder/decoder."""

def dumps(self, obj):
# Using latin-1 like django.contrib.sessions.serializers.JSONSerializer
return json.dumps(obj, cls=json.JSONEncoder).encode("latin-1")

def loads(self, data):
# Using latin-1 like django.contrib.sessions.serializers.JSONSerializer
return json.loads(data.decode("latin-1"), cls=json.JSONDecoder)