Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix CORS support for clients with credentials=include

Patryk Zawadzkibdf355db8e3f
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index 438c8b0216..c938394360 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -1,3 +1,4 @@
+import fnmatch
import json
import logging
import traceback
@@ -91,12 +92,18 @@ class GraphQLView(View):
else:
return HttpResponseNotAllowed(["GET", "OPTIONS", "POST"])
# Add access control headers
- response["Access-Control-Allow-Origin"] = settings.ALLOWED_GRAPHQL_ORIGINS
- response["Access-Control-Allow-Methods"] = "POST, OPTIONS"
- response[
- "Access-Control-Allow-Headers"
- ] = "Origin, Content-Type, Accept, Authorization"
- response["Access-Control-Allow-Credentials"] = "true"
+ if "HTTP_ORIGIN" in request.META:
+ for origin in settings.ALLOWED_GRAPHQL_ORIGINS:
+ if fnmatch.fnmatchcase(request.META["HTTP_ORIGIN"], origin):
+ response["Access-Control-Allow-Origin"] = request.META[
+ "HTTP_ORIGIN"
+ ]
+ response["Access-Control-Allow-Methods"] = "POST, OPTIONS"
+ response[
+ "Access-Control-Allow-Headers"
+ ] = "Origin, Content-Type, Accept, Authorization"
+ response["Access-Control-Allow-Credentials"] = "true"
+ break
return response
def render_playground(self, request):
diff --git a/saleor/settings.py b/saleor/settings.py
index 08e73a4817..a524ba7e3a 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -390,7 +390,7 @@ TEST_RUNNER = "saleor.tests.runner.PytestTestRunner"
PLAYGROUND_ENABLED = get_bool_from_env("PLAYGROUND_ENABLED", True)
ALLOWED_HOSTS = get_list(os.environ.get("ALLOWED_HOSTS", "localhost,127.0.0.1"))
-ALLOWED_GRAPHQL_ORIGINS = os.environ.get("ALLOWED_GRAPHQL_ORIGINS", "*")
+ALLOWED_GRAPHQL_ORIGINS = get_list(os.environ.get("ALLOWED_GRAPHQL_ORIGINS", "*"))
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")