Skip to content

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

Reject GraphQL subscription operations sent over HTTP (#19719)

Lukasz Ostrowski89c649e39b40
diff --git a/saleor/graphql/core/tests/test_view.py b/saleor/graphql/core/tests/test_view.py
index e61cad4dab..2b35922600 100644
--- a/saleor/graphql/core/tests/test_view.py
+++ b/saleor/graphql/core/tests/test_view.py
@@ -757,3 +757,32 @@ def test_real_public_mutation_blocked_when_disabled(
# then
assert response.status_code == 401
assert response.json() == EXPECTED_STOREFRONT_TRAFFIC_ERROR
+
+
+def test_subscription_operation_is_rejected(api_client, caplog):
+ # given
+ query = """
+ subscription {
+ event {
+ ... on ProductCreated {
+ product { id }
+ }
+ }
+ }
+ """
+
+ # when
+ with caplog.at_level(logging.ERROR, logger="saleor.graphql.errors.unhandled"):
+ response = api_client.post_graphql(query, check_no_permissions=False)
+
+ # then
+ assert response.status_code == 400
+ content = get_graphql_content_from_response(response)
+ errors = content["errors"]
+ assert len(errors) == 1
+ assert errors[0]["message"] == (
+ "Subscriptions are supported only as webhooks. See "
+ "https://docs.saleor.io/developer/extending/webhooks/overview"
+ )
+ assert errors[0]["extensions"]["exception"]["code"] == "GraphQLError"
+ assert caplog.records == []
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index 57f21b8c2e..04fb790435 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -451,6 +451,30 @@ class GraphQLView(View):
saleor_attributes.SALEOR_SOURCE_SERVICE_NAME, source_service_name
)
+ if operation_type == "subscription":
+ # The schema exposes a `Subscription` type only so apps can define
+ # webhook subscription documents. The synchronous executor used here
+ # cannot run them and raises a bare `Exception`, which surfaces as an
+ # unhandled error. Reject with a proper GraphQL error instead.
+ subscription_error = GraphQLError(
+ "Subscriptions are supported only as webhooks. See "
+ "https://docs.saleor.io/developer/extending/webhooks/overview"
+ )
+ span.set_status(
+ status=StatusCode.ERROR, description=str(subscription_error)
+ )
+ error_type = subscription_error.__class__.__name__
+ record_graphql_query_count(
+ operation_type=operation_type, error_type=error_type
+ )
+ record_graphql_query_cost(
+ QUERY_COST_FAILED_OPERATION,
+ operation_type=operation_type,
+ error_type=error_type,
+ )
+ query_duration_attrs[error_attributes.ERROR_TYPE] = error_type
+ return ExecutionResult(errors=[subscription_error], invalid=True)
+
query_cost, cost_errors = validate_query(
schema=schema,
document_ast=document.document_ast,