Skip to content

Commit 313b990

Browse files
committed
fix: session cookie
1 parent 3e71255 commit 313b990

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

core/schema.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,15 @@ def mutate_and_get_payload(cls, root, info, **data):
338338
request = getattr(info, "context", None)
339339

340340
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.")
341+
342+
if not any(
343+
bypass in user_agent
344+
for bypass in getattr(settings, "USER_AGENT_CSRF_BYPASS", [])
345+
):
346+
session_csrf = request.session['csrftoken']
347+
request_csrf = request.META['HTTP_X_CSRFTOKEN']
348+
if session_csrf != request_csrf:
349+
raise PermissionDenied("CSRF token missing or incorrect.")
351350

352351
mutation_log = MutationLog.objects.create(
353352
json_content=json.dumps(data, cls=OpenIMISJSONEncoder),
@@ -649,16 +648,15 @@ def resolve_queryset(
649648
raise PermissionDenied(_("unauthorized"))
650649

651650
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.")
651+
652+
if not any(
653+
bypass in user_agent
654+
for bypass in getattr(settings, "USER_AGENT_CSRF_BYPASS", [])
655+
):
656+
session_csrf = request.session['csrftoken']
657+
request_csrf = request.META['HTTP_X_CSRFTOKEN']
658+
if session_csrf != request_csrf:
659+
raise PermissionDenied("CSRF token missing or incorrect.")
662660

663661
qs = super(DjangoFilterConnectionField, cls).resolve_queryset(
664662
connection, iterable, info, args
@@ -2153,7 +2151,9 @@ def mutate(cls, root, info):
21532151
csrf_token = get_token(info.context)
21542152
if not csrf_token:
21552153
raise GraphQLError("CSRF token could not be generated")
2156-
2154+
if info.context and hasattr(info.context, 'session'):
2155+
info.context.session['csrftoken'] = csrf_token
2156+
info.context.session.save()
21572157
return GetCsrfTokenMutation(csrf_token=csrf_token)
21582158

21592159

0 commit comments

Comments
 (0)