Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix the permission check in webhook resolvers (#13378)
Patryk Zawadzki5e7c57dad9e6
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 05308c6279..70ec038434 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -101,6 +101,7 @@ All notable, unreleased changes to this project will be documented in this file.- Add missing descriptions to Product module. - #13259 by @FremahA- Add missing description for Invoice module - #13265 by @DevilsAutumn- Add missing descriptions to Discount module - #13261 by @devilsautumn+- Fix error handling in the permission check for `Query.webhook` - #13378 by @patrys# 3.14.0diff --git a/saleor/graphql/webhook/resolvers.py b/saleor/graphql/webhook/resolvers.pyindex 73602f10df..2a4de9d615 100644--- a/saleor/graphql/webhook/resolvers.py+++ b/saleor/graphql/webhook/resolvers.py@@ -8,17 +8,18 @@ from ...permission.enums import AppPermissionfrom ...webhook import models, payloadsfrom ...webhook.deprecated_event_types import WebhookEventTypefrom ...webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType+from ..core import ResolveInfofrom ..core.tracing import traced_resolverfrom ..core.utils import from_global_id_or_errorfrom .types import Webhook, WebhookEvent-def resolve_webhook(info, id, app):+def resolve_webhook(info: ResolveInfo, id, app):_, id = from_global_id_or_error(id, Webhook)if app:return app.webhooks.filter(id=id).first()user = info.context.user- if user.has_perm(AppPermission.MANAGE_APPS):+ if user and user.has_perm(AppPermission.MANAGE_APPS):return models.Webhook.objects.filter(pk=id).first()raise PermissionDenied(permissions=[AppPermission.MANAGE_APPS])@@ -31,7 +32,7 @@ def resolve_webhook_events():@traced_resolver-def resolve_sample_payload(info, event_name, app):+def resolve_sample_payload(info: ResolveInfo, event_name, app):user = info.context.userrequired_permission = WebhookEventAsyncType.PERMISSIONS.get(event_name, WebhookEventSyncType.PERMISSIONS.get(event_name)@@ -46,7 +47,7 @@ def resolve_sample_payload(info, event_name, app):raise PermissionDenied(permissions=[required_permission])-def resolve_shipping_methods_for_checkout(info, checkout, manager):+def resolve_shipping_methods_for_checkout(info: ResolveInfo, checkout, manager):lines, _ = fetch_checkout_lines(checkout)shipping_channel_listings = checkout.channel.shipping_method_listings.all()checkout_info = fetch_checkout_info(diff --git a/saleor/graphql/webhook/tests/queries/test_webhook.py b/saleor/graphql/webhook/tests/queries/test_webhook.pyindex 73d6ebd898..be646de0d2 100644--- a/saleor/graphql/webhook/tests/queries/test_webhook.py+++ b/saleor/graphql/webhook/tests/queries/test_webhook.py@@ -105,6 +105,21 @@ def test_query_webhook_by_app_without_permission(app_api_client):assert webhook_response is None+def test_query_webhook_by_anonymnous_client(api_client):+ second_app = App.objects.create(name="Sample app account", is_active=True)+ webhook = Webhook.objects.create(+ app=second_app, target_url="http://www.example.com/test"+ )+ webhook.events.create(event_type="order_created")++ query = QUERY_WEBHOOK+ webhook_id = graphene.Node.to_global_id("Webhook", webhook.pk)+ variables = {"id": webhook_id}+ response = api_client.post_graphql(query, variables=variables)+ content = get_graphql_content(response, ignore_errors=True)+ assert content["data"]["webhook"] is None++def test_query_webhook_sync_and_async_events(staff_api_client, webhook, permission_manage_apps):