|
44 | 44 | from django.db.models import Q, Count |
45 | 45 | from django.db.models.expressions import RawSQL |
46 | 46 | from django.http import HttpRequest |
47 | | -from django.middleware.csrf import CsrfViewMiddleware, get_token |
| 47 | +from django.middleware.csrf import get_token |
48 | 48 | from django.utils import translation |
49 | 49 | from django.utils.timezone import now |
50 | 50 | from graphene.utils.str_converters import to_snake_case, to_camel_case |
|
72 | 72 | ) |
73 | 73 | from core.utils import ( |
74 | 74 | ExtendedConnection, |
75 | | - is_this_session_superuser, |
76 | 75 | collect_all_gql_permissions, |
77 | 76 | ) |
78 | 77 | from core.models import ( |
@@ -338,16 +337,15 @@ def mutate_and_get_payload(cls, root, info, **data): |
338 | 337 | request = getattr(info, "context", None) |
339 | 338 |
|
340 | 339 | user_agent = request.headers.get("User-Agent", "") |
341 | | - current_session_key = request.session.session_key |
342 | | - if not is_this_session_superuser(current_session_key): |
343 | | - if not any( |
344 | | - bypass in user_agent |
345 | | - for bypass in getattr(settings, "USER_AGENT_CSRF_BYPASS", []) |
346 | | - ): |
347 | | - csrf_middleware = CsrfViewMiddleware(lambda req: None) |
348 | | - reason = csrf_middleware.process_view(request, None, (), {}) |
349 | | - if reason: |
350 | | - raise PermissionDenied("CSRF token missing or incorrect.") |
| 340 | + |
| 341 | + if not any( |
| 342 | + bypass in user_agent |
| 343 | + for bypass in getattr(settings, "USER_AGENT_CSRF_BYPASS", []) |
| 344 | + ): |
| 345 | + session_csrf = request.session['csrftoken'] |
| 346 | + request_csrf = request.META['HTTP_X_CSRFTOKEN'] |
| 347 | + if session_csrf != request_csrf: |
| 348 | + raise PermissionDenied("CSRF token missing or incorrect.") |
351 | 349 |
|
352 | 350 | mutation_log = MutationLog.objects.create( |
353 | 351 | json_content=json.dumps(data, cls=OpenIMISJSONEncoder), |
@@ -649,16 +647,15 @@ def resolve_queryset( |
649 | 647 | raise PermissionDenied(_("unauthorized")) |
650 | 648 |
|
651 | 649 | user_agent = request.headers.get("User-Agent", "") |
652 | | - current_session_key = request.session.session_key |
653 | | - if not is_this_session_superuser(current_session_key): |
654 | | - if not any( |
655 | | - bypass in user_agent |
656 | | - for bypass in getattr(settings, "USER_AGENT_CSRF_BYPASS", []) |
657 | | - ): |
658 | | - csrf_middleware = CsrfViewMiddleware(lambda req: None) |
659 | | - reason = csrf_middleware.process_view(request, None, (), {}) |
660 | | - if reason: |
661 | | - raise PermissionDenied("CSRF token missing or incorrect.") |
| 650 | + |
| 651 | + if not any( |
| 652 | + bypass in user_agent |
| 653 | + for bypass in getattr(settings, "USER_AGENT_CSRF_BYPASS", []) |
| 654 | + ): |
| 655 | + session_csrf = request.session['csrftoken'] |
| 656 | + request_csrf = request.META['HTTP_X_CSRFTOKEN'] |
| 657 | + if session_csrf != request_csrf: |
| 658 | + raise PermissionDenied("CSRF token missing or incorrect.") |
662 | 659 |
|
663 | 660 | qs = super(DjangoFilterConnectionField, cls).resolve_queryset( |
664 | 661 | connection, iterable, info, args |
@@ -2153,7 +2150,9 @@ def mutate(cls, root, info): |
2153 | 2150 | csrf_token = get_token(info.context) |
2154 | 2151 | if not csrf_token: |
2155 | 2152 | raise GraphQLError("CSRF token could not be generated") |
2156 | | - |
| 2153 | + if info.context and hasattr(info.context, 'session'): |
| 2154 | + info.context.session['csrftoken'] = csrf_token |
| 2155 | + info.context.session.save() |
2157 | 2156 | return GetCsrfTokenMutation(csrf_token=csrf_token) |
2158 | 2157 |
|
2159 | 2158 |
|
|
0 commit comments