Skip to content

Commit cc82445

Browse files
committed
fix: exclude csrf check from test and dev
1 parent ae60871 commit cc82445

1 file changed

Lines changed: 14 additions & 20 deletions

File tree

core/schema.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ def parse_value(value):
195195
)
196196

197197

198+
def _check_csrf_token(request):
199+
user_agent = request.headers.get("User-Agent", "")
200+
if not (settings.MODE == 'dev' or settings.IS_TESTING or any(
201+
bypass in user_agent
202+
for bypass in getattr(settings, "USER_AGENT_CSRF_BYPASS", [])
203+
)):
204+
session_csrf = request.session['csrftoken']
205+
request_csrf = request.META['HTTP_X_CSRFTOKEN']
206+
if session_csrf != request_csrf:
207+
raise PermissionDenied("CSRF token missing or incorrect.")
208+
209+
198210
class OpenIMISJSONEncoder(DjangoJSONEncoder):
199211
def default(self, o):
200212
if isinstance(o, HttpRequest):
@@ -336,16 +348,7 @@ def async_mutate(cls, user, **data) -> List[Dict[str, Any]]:
336348
def mutate_and_get_payload(cls, root, info, **data):
337349
request = getattr(info, "context", None)
338350

339-
user_agent = request.headers.get("User-Agent", "")
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+
_check_csrf_token(request)
349352

350353
mutation_log = MutationLog.objects.create(
351354
json_content=json.dumps(data, cls=OpenIMISJSONEncoder),
@@ -646,16 +649,7 @@ def resolve_queryset(
646649
if not info.context.user.is_authenticated:
647650
raise PermissionDenied(_("unauthorized"))
648651

649-
user_agent = request.headers.get("User-Agent", "")
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.")
652+
_check_csrf_token(request)
659653

660654
qs = super(DjangoFilterConnectionField, cls).resolve_queryset(
661655
connection, iterable, info, args

0 commit comments

Comments
 (0)