Skip to content

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

Allow to access to app's resources when app is an owner (#9425)

Maciej Korycinskib6e950e8ad91
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 538126dd4b..ce9485c7f0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix failing `checkoutCustomerAttach` mutation - #9401 by @IKarbowiak
- Add new mutation `orderCreateFromCheckout` - #9343 by @korycins
- Add `language_code` field to webhook payload for `Order`, `Checkout` and `Customer` - #9433 by @rafalp
+- Fix access to own resources by App - #9425 by @korycins
# 3.1.7
diff --git a/saleor/graphql/account/utils.py b/saleor/graphql/account/utils.py
index cbcb997663..56746129e8 100644
--- a/saleor/graphql/account/utils.py
+++ b/saleor/graphql/account/utils.py
@@ -464,7 +464,7 @@ def look_for_permission_in_users_with_manage_staff(
def requestor_has_access(
- requestor: Union["User", "App"], owner: Optional["User"], *perms
+ requestor: Union["User", "App"], owner: Optional[Union["User", "App"]], *perms
) -> bool:
"""Check if requestor can access data.
diff --git a/saleor/graphql/app/mutations.py b/saleor/graphql/app/mutations.py
index 1e0744e577..ce343a95ac 100644
--- a/saleor/graphql/app/mutations.py
+++ b/saleor/graphql/app/mutations.py
@@ -16,6 +16,7 @@ from ..core import types as grapqhl_types
from ..core.enums import PermissionEnum
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.types import AppError, NonNullList
+from ..decorators import staff_member_required
from ..utils import get_user_or_app_from_context, requestor_is_superuser
from .types import App, AppInstallation, AppToken, Manifest
from .utils import ensure_can_manage_permissions
@@ -137,7 +138,11 @@ class AppCreate(ModelMutation):
)
class Meta:
- description = "Creates a new app."
+ auto_permission_message = False
+ description = (
+ "Creates a new app. Requires the following "
+ "permissions: AUTHENTICATED_STAFF_USER and MANAGE_APPS."
+ )
model = models.App
object_type = App
permissions = (AppPermission.MANAGE_APPS,)
@@ -155,6 +160,11 @@ class AppCreate(ModelMutation):
ensure_can_manage_permissions(requestor, permissions)
return cleaned_input
+ @classmethod
+ @staff_member_required
+ def perform_mutation(cls, root, info, **data):
+ return super().perform_mutation(root, info, **data)
+
@classmethod
def save(cls, info, instance, cleaned_input):
instance.save()
@@ -349,7 +359,11 @@ class AppInstall(ModelMutation):
)
class Meta:
- description = "Install new app by using app manifest."
+ auto_permission_message = False
+ description = (
+ "Install new app by using app manifest. Requires the following "
+ "permissions: AUTHENTICATED_STAFF_USER and MANAGE_APPS."
+ )
model = models.AppInstallation
object_type = AppInstallation
permissions = (AppPermission.MANAGE_APPS,)
@@ -371,6 +385,11 @@ class AppInstall(ModelMutation):
ensure_can_manage_permissions(requestor, permissions)
return cleaned_input
+ @classmethod
+ @staff_member_required
+ def perform_mutation(cls, root, info, **data):
+ return super().perform_mutation(root, info, **data)
+
@classmethod
def post_save_action(cls, info, instance, cleaned_input):
activate_after_installation = cleaned_input["activate_after_installation"]
diff --git a/saleor/graphql/app/tests/mutations/test_app_create.py b/saleor/graphql/app/tests/mutations/test_app_create.py
index 81c1a14511..1f3cdc37c8 100644
--- a/saleor/graphql/app/tests/mutations/test_app_create.py
+++ b/saleor/graphql/app/tests/mutations/test_app_create.py
@@ -60,12 +60,13 @@ def test_app_create_mutation(
assert default_token == app.tokens.get().auth_token
-def test_app_create_mutation_for_app(
+def test_app_is_not_allowed_to_call_create_mutation_for_app(
permission_manage_apps,
permission_manage_products,
app_api_client,
staff_user,
):
+ # given
query = APP_CREATE_MUTATION
requestor = app_api_client.app
requestor.permissions.add(permission_manage_apps, permission_manage_products)
@@ -74,15 +75,12 @@ def test_app_create_mutation_for_app(
"name": "New integration",
"permissions": [PermissionEnum.MANAGE_PRODUCTS.name],
}
+
+ # when
response = app_api_client.post_graphql(query, variables=variables)
- content = get_graphql_content(response)
- app_data = content["data"]["appCreate"]["app"]
- default_token = content["data"]["appCreate"]["authToken"]
- app = App.objects.exclude(pk=requestor.pk).get()
- assert app_data["isActive"] == app.is_active
- assert app_data["name"] == app.name
- assert list(app.permissions.all()) == [permission_manage_products]
- assert default_token == app.tokens.get().auth_token
+
+ # then
+ assert_no_permission(response)
def test_app_create_mutation_out_of_scope_permissions(
@@ -140,33 +138,6 @@ def test_app_create_mutation_superuser_can_create_app_with_any_perms(
assert default_token == app.tokens.get().auth_token
-def test_app_create_mutation_for_app_out_of_scope_permissions(
- permission_manage_apps,
- permission_manage_products,
- app_api_client,
- staff_user,
-):
- query = APP_CREATE_MUTATION
-
- variables = {
- "name": "New integration",
- "permissions": [PermissionEnum.MANAGE_PRODUCTS.name],
- }
- response = app_api_client.post_graphql(
- query, variables=variables, permissions=(permission_manage_apps,)
- )
- content = get_graphql_content(response)
- data = content["data"]["appCreate"]
-
- errors = data["errors"]
- assert not data["app"]
- assert len(errors) == 1
- error = errors[0]
- assert error["field"] == "permissions"
- assert error["code"] == AppErrorCode.OUT_OF_SCOPE_PERMISSION.name
- assert error["permissions"] == [PermissionEnum.MANAGE_PRODUCTS.name]
-
-
def test_app_create_mutation_no_permissions(
permission_manage_apps,
permission_manage_products,
diff --git a/saleor/graphql/app/tests/mutations/test_app_install.py b/saleor/graphql/app/tests/mutations/test_app_install.py
index 6747096613..5e0dfd9534 100644
--- a/saleor/graphql/app/tests/mutations/test_app_install.py
+++ b/saleor/graphql/app/tests/mutations/test_app_install.py
@@ -5,7 +5,7 @@ import graphene
from .....app.models import AppInstallation
from .....core import JobStatus
from ....core.enums import AppErrorCode, PermissionEnum
-from ....tests.utils import get_graphql_content
+from ....tests.utils import assert_no_permission, get_graphql_content
INSTALL_APP_MUTATION = """
mutation AppInstall(
@@ -62,13 +62,10 @@ def test_install_app_mutation(
mocked_task.assert_called_with(app_installation.pk, True)
-def test_install_app_mutation_by_app(
+def test_app_is_not_allowed_to_install_app(
permission_manage_apps, permission_manage_orders, app_api_client, monkeypatch
):
- mocked_task = Mock()
- monkeypatch.setattr(
- "saleor.graphql.app.mutations.install_app_task.delay", mocked_task
- )
+ # given
query = INSTALL_APP_MUTATION
app_api_client.app.permissions.set(
[permission_manage_apps, permission_manage_orders]
@@ -78,18 +75,15 @@ def test_install_app_mutation_by_app(
"manifest_url": "http://localhost:3000/manifest",
"permissions": [PermissionEnum.MANAGE_ORDERS.name],
}
+
+ # when
response = app_api_client.post_graphql(
query,
variables=variables,
)
- content = get_graphql_content(response)
- app_installation = AppInstallation.objects.get()
- app_installation_data = content["data"]["appInstall"]["appInstallation"]
- _, app_id = graphene.Node.from_global_id(app_installation_data["id"])
- assert int(app_id) == app_installation.id
- assert app_installation_data["status"] == JobStatus.PENDING.upper()
- assert app_installation_data["manifestUrl"] == app_installation.manifest_url
- mocked_task.assert_called_with(app_installation.pk, True)
+
+ # then
+ assert_no_permission(response)
def test_app_install_mutation_out_of_scope_permissions(
@@ -116,30 +110,3 @@ def test_app_install_mutation_out_of_scope_permissions(
assert error["field"] == "permissions"
assert error["code"] == AppErrorCode.OUT_OF_SCOPE_PERMISSION.name
assert error["permissions"] == [PermissionEnum.MANAGE_ORDERS.name]
-
-
-def test_install_app_mutation_by_app_out_of_scope_permissions(
- permission_manage_apps, app_api_client
-):
- query = INSTALL_APP_MUTATION
- app_api_client.app.permissions.set([permission_manage_apps])
- variables = {
- "app_name": "New external integration",
- "manifest_url": "http://localhost:3000/manifest",
- "permissions": [PermissionEnum.MANAGE_ORDERS.name],
- }
- response = app_api_client.post_graphql(
- query,
- variables=variables,
- )
-
- content = get_graphql_content(response)
- data = content["data"]["appInstall"]
-
- errors = data["errors"]
- assert not data["appInstallation"]
- assert len(errors) == 1
- error = errors[0]
- assert error["field"] == "permissions"
- assert error["code"] == AppErrorCode.OUT_OF_SCOPE_PERMISSION.name
- assert error["permissions"] == [PermissionEnum.MANAGE_ORDERS.name]
diff --git a/saleor/graphql/app/tests/queries/test_app.py b/saleor/graphql/app/tests/queries/test_app.py
index 5b28d050c8..c6c9a68fcd 100644
--- a/saleor/graphql/app/tests/queries/test_app.py
+++ b/saleor/graphql/app/tests/queries/test_app.py
@@ -40,6 +40,14 @@ QUERY_APP = """
code
}
}
+ metadata{
+ key
+ value
+ }
+ privateMetadata{
+ key
+ value
+ }
}
}
"""
@@ -154,14 +162,20 @@ def test_app_without_id_as_staff(
def test_own_app_without_id(
app_api_client,
app,
- permission_manage_orders,
order_with_lines,
webhook,
- permission_manage_apps,
):
+ # given
+ app.store_value_in_metadata({"public": "metadata"})
+ app.store_value_in_private_metadata({"private": "metadata"})
+ app.save()
+
+ # when
response = app_api_client.post_graphql(
- QUERY_APP, permissions=[permission_manage_apps]
+ QUERY_APP,
)
+
+ # then
content = get_graphql_content(response)
tokens = app.tokens.all()
@@ -181,6 +195,10 @@ def test_own_app_without_id(
assert app_data["supportUrl"] == app.support_url
assert app_data["configurationUrl"] == app.configuration_url
assert app_data["appUrl"] == app.app_url
+ assert app_data["metadata"][0]["key"] == "public"
+ assert app_data["metadata"][0]["value"] == "metadata"
+ assert app_data["privateMetadata"][0]["key"] == "private"
+ assert app_data["privateMetadata"][0]["value"] == "metadata"
def test_app_query_without_permission(
diff --git a/saleor/graphql/app/types.py b/saleor/graphql/app/types.py
index e4409a5a7c..f4568eceed 100644
--- a/saleor/graphql/app/types.py
+++ b/saleor/graphql/app/types.py
@@ -6,11 +6,11 @@ from ...app import models
from ...app.types import AppExtensionTarget
from ...core.exceptions import PermissionDenied
from ...core.permissions import AppPermission, AuthorizationFilters
+from ..account.utils import requestor_has_access
from ..core.connection import CountableConnection
from ..core.descriptions import ADDED_IN_31, PREVIEW_FEATURE
from ..core.federation import federated_entity, resolve_federation_references
from ..core.types import Job, ModelObjectType, NonNullList, Permission
-from ..decorators import permission_required
from ..meta.types import ObjectWithMetadata
from ..utils import format_permissions_for_display, get_user_or_app_from_context
from ..webhook.types import Webhook
@@ -23,6 +23,14 @@ from .resolvers import (
)
+def has_required_permission(app: models.App, context):
+ requester = get_user_or_app_from_context(context)
+ if not requestor_has_access(requester, app, AppPermission.MANAGE_APPS):
+ raise PermissionDenied(
+ permissions=[AppPermission.MANAGE_APPS, AuthorizationFilters.OWNER]
+ )
+
+
class AppManifestExtension(graphene.ObjectType):
permissions = NonNullList(
Permission,
@@ -217,18 +225,18 @@ class App(ModelObjectType):
return format_permissions_for_display(permissions)
@staticmethod
- @permission_required(AppPermission.MANAGE_APPS)
- def resolve_tokens(root: models.App, _info, **_kwargs):
- return root.tokens.all() # type: ignore
+ def resolve_tokens(root: models.App, info, **_kwargs):
+ has_required_permission(root, info.context)
+ return root.tokens.all()
@staticmethod
- @permission_required(AppPermission.MANAGE_APPS)
- def resolve_webhooks(root: models.App, _info):
+ def resolve_webhooks(root: models.App, info):
+ has_required_permission(root, info.context)
return root.webhooks.all()
@staticmethod
- @permission_required(AppPermission.MANAGE_APPS)
def resolve_access_token(root: models.App, info):
+ has_required_permission(root, info.context)
return resolve_access_token_for_app(info, root)
@staticmethod
diff --git a/saleor/graphql/meta/permissions.py b/saleor/graphql/meta/permissions.py
index 9afc380805..e4f3aff69f 100644
--- a/saleor/graphql/meta/permissions.py
+++ b/saleor/graphql/meta/permissions.py
@@ -82,7 +82,10 @@ def menu_permissions(_info, _object_pk: Any) -> List[BasePermissionEnum]:
return [MenuPermissions.MANAGE_MENUS]
-def app_permissions(_info, _object_pk: int) -> List[BasePermissionEnum]:
+def app_permissions(info, object_pk: str) -> List[BasePermissionEnum]:
+ app = info.context.app
+ if app and app.pk == int(object_pk):
+ return []
return [AppPermission.MANAGE_APPS]
diff --git a/saleor/graphql/meta/resolvers.py b/saleor/graphql/meta/resolvers.py
index 80cb903420..03185a6c5b 100644
--- a/saleor/graphql/meta/resolvers.py
+++ b/saleor/graphql/meta/resolvers.py
@@ -92,9 +92,9 @@ def resolve_private_metadata(root: ModelWithMetadata, info):
if not get_required_permission:
raise PermissionDenied()
- required_permissions = get_required_permission(info, item_id)
+ required_permissions = get_required_permission(info, item_id) # type: ignore
- if not required_permissions:
+ if not isinstance(required_permissions, list):
raise PermissionDenied()
requester = get_user_or_app_from_context(info.context)
diff --git a/saleor/graphql/meta/tests/test_meta_mutations.py b/saleor/graphql/meta/tests/test_meta_mutations.py
index 0d96064ce9..b12536e0e6 100644
--- a/saleor/graphql/meta/tests/test_meta_mutations.py
+++ b/saleor/graphql/meta/tests/test_meta_mutations.py
@@ -665,6 +665,48 @@ def test_add_public_metadata_for_app(staff_api_client, permission_manage_apps, a
)
+def test_add_public_metadata_for_app_by_different_app(
+ app_api_client, permission_manage_apps, app, payment_app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", payment_app.pk)
+ app_api_client.app = app
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ app_api_client,
+ permission_manage_apps,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], payment_app, app_id
+ )
+
+
+def test_add_public_metadata_for_app_that_is_owner(
+ app_api_client, permission_manage_apps, app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", app.pk)
+ app_api_client.app = app
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ app_api_client,
+ None,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], app, app_id
+ )
+
+
def test_add_public_metadata_for_page(staff_api_client, permission_manage_pages, page):
# given
page_id = graphene.Node.to_global_id("Page", page.pk)
@@ -2291,6 +2333,52 @@ def test_add_private_metadata_for_app(staff_api_client, permission_manage_apps,
)
+def test_add_private_metadata_for_app_by_different_app(
+ app_api_client, permission_manage_apps, app, payment_app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", payment_app.pk)
+ app_api_client.app = app
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ app_api_client,
+ permission_manage_apps,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ payment_app,
+ app_id,
+ )
+
+
+def test_add_private_metadata_by_app_that_is_owner(
+ app_api_client, permission_manage_apps, app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", app.pk)
+ app_api_client.app = app
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ app_api_client,
+ None,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ app,
+ app_id,
+ )
+
+
def test_add_private_metadata_for_page(staff_api_client, permission_manage_pages, page):
# given
page_id = graphene.Node.to_global_id("Page", page.pk)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index e41b636170..5695bf76ac 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -10067,7 +10067,7 @@ type Mutation {
): AttributeReorderValues
"""
- Creates a new app. Requires one of the following permissions: MANAGE_APPS.
+ Creates a new app. Requires the following permissions: AUTHENTICATED_STAFF_USER and MANAGE_APPS.
"""
appCreate(
"""Fields required to create a new app."""
@@ -10116,7 +10116,7 @@ type Mutation {
): AppTokenVerify
"""
- Install new app by using app manifest. Requires one of the following permissions: MANAGE_APPS.
+ Install new app by using app manifest. Requires the following permissions: AUTHENTICATED_STAFF_USER and MANAGE_APPS.
"""
appInstall(
"""Fields required to install a new app."""
@@ -15465,7 +15465,7 @@ type AttributeReorderValues {
}
"""
-Creates a new app. Requires one of the following permissions: MANAGE_APPS.
+Creates a new app. Requires the following permissions: AUTHENTICATED_STAFF_USER and MANAGE_APPS.
"""
type AppCreate {
"""The newly created authentication token."""
@@ -15571,7 +15571,7 @@ type AppTokenVerify {
}
"""
-Install new app by using app manifest. Requires one of the following permissions: MANAGE_APPS.
+Install new app by using app manifest. Requires the following permissions: AUTHENTICATED_STAFF_USER and MANAGE_APPS.
"""
type AppInstall {
appErrors: [AppError!]! @deprecated(reason: "This field will be removed in Saleor 4.0. Use `errors` field instead.")