Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix permission access to `appExtension.app` (#17825)
Lukasz Ostrowskia1e1e2554327
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 69d3d1768c..a778c87dde 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -53,3 +53,5 @@ All notable, unreleased changes to this project will be documented in this file.```To see details of why a GraphQL request is failing, you can use OpenTelemetry tracing, where each span for a failing request will be marked with an error flag and will include an error message.++- Fixed bug when not-authenticated staff user couldn't fetch `appExtension.app` without `MANAGE_APPS`. Now apps access is available by staff users and the app itself (for app and extension it owns)diff --git a/saleor/graphql/app/tests/queries/test_app_extension.py b/saleor/graphql/app/tests/queries/test_app_extension.pyindex c493dd3f7e..2c404dec66 100644--- a/saleor/graphql/app/tests/queries/test_app_extension.py+++ b/saleor/graphql/app/tests/queries/test_app_extension.py@@ -1,8 +1,8 @@import grapheneimport pytest-from .....app.models import AppExtension-from .....app.types import AppExtensionMount, AppExtensionTarget+from .....app.models import App, AppExtension+from .....app.types import AppExtensionMount, AppExtensionTarget, AppTypefrom .....core.jwt import jwt_decodefrom ....tests.utils import assert_no_permission, get_graphql_content@@ -334,15 +334,17 @@ def test_app_extension_with_app_query_by_staff_without_permissions()# then- assert_no_permission(response)+ response = get_graphql_content(response)++ assert response["data"]["appExtension"]["id"] == iddef test_app_extension_with_app_query_by_app_without_permissions(- external_app, app_api_client, permission_manage_products+ app, app_api_client, permission_manage_products):# givenapp_extension = AppExtension.objects.create(- app=external_app,+ app=app,label="Create product with App",url="https://www.example.com/app-product",mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,@@ -358,25 +360,38 @@ def test_app_extension_with_app_query_by_app_without_permissions()# then- assert_no_permission(response)+ response = get_graphql_content(response)+ # Can access OWN extension+ assert response["data"]["appExtension"]["id"] == id-def test_app_extension_with_app_query_by_app_with_permissions(- external_app,- app,- permission_manage_apps,- app_api_client,- permission_manage_products,++def test_app_extension_with_app_query_by_app_without_permissions_other_app(+ app_api_client, permission_manage_products):# given+ # another app - to be sure we don't mix it with app from the app_api_client+ another_app = App.objects.create(+ name="External App",+ is_active=True,+ type=AppType.THIRDPARTY,+ identifier="mirumee.app.sample",+ about_app="About app text.",+ data_privacy="Data privacy text.",+ data_privacy_url="http://www.example.com/privacy/",+ homepage_url="http://www.example.com/homepage/",+ support_url="http://www.example.com/support/contact/",+ configuration_url="http://www.example.com/app-configuration/",+ app_url="http://www.example.com/app/",+ )+app_extension = AppExtension.objects.create(- app=external_app,+ app=another_app,label="Create product with App",url="https://www.example.com/app-product",mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,)app_extension.permissions.add(permission_manage_products)- app.permissions.add(permission_manage_apps)id = graphene.Node.to_global_id("AppExtension", app_extension.id)variables = {"id": id}@@ -387,7 +402,8 @@ def test_app_extension_with_app_query_by_app_with_permissions()# then- get_graphql_content(response)+ # Can't access other apps+ assert_no_permission(response)def test_app_extension_with_app_query_by_owner_app(@@ -415,7 +431,7 @@ def test_app_extension_with_app_query_by_owner_app(def test_app_extension_with_app_query_by_staff_with_permissions(- external_app, app, permission_manage_apps, staff_api_client+ external_app, app, staff_api_client, permission_group_manage_apps):# givenapp_extension = AppExtension.objects.create(@@ -428,13 +444,36 @@ def test_app_extension_with_app_query_by_staff_with_permissions(id = graphene.Node.to_global_id("AppExtension", app_extension.id)variables = {"id": id}+ staff_api_client.user.groups.add(permission_group_manage_apps)+# when- response = staff_api_client.post_graphql(- QUERY_APP_EXTENSION_WITH_APP, variables, permissions=[permission_manage_apps]+ response = staff_api_client.post_graphql(QUERY_APP_EXTENSION_WITH_APP, variables)++ # then+ response = get_graphql_content(response)++ assert response["data"]["appExtension"]["id"] == id+++def test_app_extension_with_app_query_by_customer_without_permissions(+ external_app, app, api_client, permission_group_manage_apps+):+ # given+ app_extension = AppExtension.objects.create(+ app=external_app,+ label="Create product with App",+ url="https://www.example.com/app-product",+ mount=AppExtensionMount.PRODUCT_OVERVIEW_MORE_ACTIONS,)+ id = graphene.Node.to_global_id("AppExtension", app_extension.id)+ variables = {"id": id}++ # when+ response = api_client.post_graphql(QUERY_APP_EXTENSION_WITH_APP, variables)+# then- get_graphql_content(response)+ assert_no_permission(response)@pytest.mark.parametrize(diff --git a/saleor/graphql/app/types.py b/saleor/graphql/app/types.pyindex 1d4e787112..5ed6064bdf 100644--- a/saleor/graphql/app/types.py+++ b/saleor/graphql/app/types.py@@ -8,7 +8,7 @@ from ...app.types import AppExtensionHttpMethod, AppExtensionTargetfrom ...core.exceptions import PermissionDeniedfrom ...core.jwt import JWT_THIRDPARTY_ACCESS_TYPEfrom ...core.utils import build_absolute_uri-from ...permission.auth_filters import AuthorizationFilters+from ...permission.auth_filters import AuthorizationFilters, is_staff_userfrom ...permission.enums import AppPermissionfrom ...permission.utils import message_one_of_permissions_requiredfrom ...thumbnail import PIL_IDENTIFIER_TO_MIME_TYPE@@ -235,20 +235,27 @@ class AppExtension(AppManifestExtension, ModelObjectType[models.AppExtension]):@staticmethod@app_promise_callback- def resolve_app(root, info: ResolveInfo, app):- app_id = None- if app and app.id == root.app_id:- app_id = root.app_id- else:- requestor = get_user_or_app_from_context(info.context)- if requestor and requestor.has_perm(AppPermission.MANAGE_APPS):- app_id = root.app_id+ def resolve_app(root, info: ResolveInfo, app_requestor):+ # Resolve if app from context is the same as requested app+ if app_requestor and app_requestor.id == root.app_id:+ return AppByIdLoader(info.context).load(root.app_id)- if not app_id:- raise PermissionDenied(- permissions=[AppPermission.MANAGE_APPS, AuthorizationFilters.OWNER]- )- return AppByIdLoader(info.context).load(app_id)+ # Reject if app from context is different from requested app+ if app_requestor and app_requestor.id != root.app_id:+ raise PermissionDenied(permissions=[AuthorizationFilters.OWNER])++ # Resolve app if a user from the context is authenticated+ # At this point, it's always user, because we exit for app in checks above+ maybe_user = get_user_or_app_from_context(info.context)++ # Allow staff users to access app data, no MANAGE_APPS needed+ if maybe_user and is_staff_user(info.context):+ return AppByIdLoader(info.context).load(root.app_id)++ # If none of the conditions are met, reject with permission denied+ raise PermissionDenied(+ permissions=[AppPermission.MANAGE_APPS, AuthorizationFilters.OWNER]+ )@staticmethoddef resolve_permissions(root: models.AppExtension, _info: ResolveInfo):diff --git a/saleor/graphql/tests/fixtures.py b/saleor/graphql/tests/fixtures.pyindex 2041911647..f2baadb220 100644--- a/saleor/graphql/tests/fixtures.py+++ b/saleor/graphql/tests/fixtures.py@@ -86,6 +86,8 @@ class ApiClient(BaseApiClient):self,query,variables=None,+ # @deprecated - do not use it, because it makes implicit permissions check+ # Use explicit permissions assertionspermissions=None,check_no_permissions=True,**kwargs,