Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): validate installation scope for cached marketplace packages (#39935)

Xiyuan Chen1b7a132afa8f
diff --git a/api/core/plugin/plugin_service.py b/api/core/plugin/plugin_service.py
index a0147adadfd..f70326aced8 100644
--- a/api/core/plugin/plugin_service.py
+++ b/api/core/plugin/plugin_service.py
@@ -919,10 +919,8 @@ class PluginService:
try:
manager.fetch_plugin_manifest(tenant_id, new_plugin_unique_identifier)
- # already downloaded, skip, and record install event
- marketplace.record_install_plugin_event(new_plugin_unique_identifier)
except Exception:
- # plugin not installed, download and upload pkg
+ # plugin not downloaded yet, download and upload pkg
pkg = download_plugin_pkg(new_plugin_unique_identifier)
response = manager.upload_pkg(
tenant_id,
@@ -932,6 +930,11 @@ class PluginService:
# check if the plugin is available to install
PluginService._check_plugin_installation_scope(response.verification)
+ else:
+ # already downloaded, the cached pkg still has to satisfy the installation scope
+ decode_response = manager.decode_plugin_from_identifier(tenant_id, new_plugin_unique_identifier)
+ PluginService._check_plugin_installation_scope(decode_response.verification)
+ marketplace.record_install_plugin_event(new_plugin_unique_identifier)
result = manager.upgrade_plugin(
tenant_id,
@@ -1122,14 +1125,8 @@ class PluginService:
for plugin_unique_identifier in plugin_unique_identifiers:
try:
manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
- plugin_decode_response = manager.decode_plugin_from_identifier(tenant_id, plugin_unique_identifier)
- # check if the plugin is available to install
- PluginService._check_plugin_installation_scope(plugin_decode_response.verification)
- # already downloaded, skip
- actual_plugin_unique_identifiers.append(plugin_unique_identifier)
- metas.append({"plugin_unique_identifier": plugin_unique_identifier})
except Exception:
- # plugin not installed, download and upload pkg
+ # plugin not downloaded yet, download and upload pkg
pkg = download_plugin_pkg(plugin_unique_identifier)
response = manager.upload_pkg(
tenant_id,
@@ -1141,6 +1138,12 @@ class PluginService:
# use response plugin_unique_identifier
actual_plugin_unique_identifiers.append(response.unique_identifier)
metas.append({"plugin_unique_identifier": response.unique_identifier})
+ else:
+ # already downloaded, the cached pkg still has to satisfy the installation scope
+ plugin_decode_response = manager.decode_plugin_from_identifier(tenant_id, plugin_unique_identifier)
+ PluginService._check_plugin_installation_scope(plugin_decode_response.verification)
+ actual_plugin_unique_identifiers.append(plugin_unique_identifier)
+ metas.append({"plugin_unique_identifier": plugin_unique_identifier})
result = manager.install_from_identifiers(
tenant_id,
diff --git a/api/tests/unit_tests/services/plugin/test_plugin_service.py b/api/tests/unit_tests/services/plugin/test_plugin_service.py
index 4c063d30f4b..ccacc5ca52d 100644
--- a/api/tests/unit_tests/services/plugin/test_plugin_service.py
+++ b/api/tests/unit_tests/services/plugin/test_plugin_service.py
@@ -16,6 +16,7 @@ from core.provider_manager import ProviderConfigurationCacheSource
from graphon.model_runtime.entities.common_entities import I18nObject
from graphon.model_runtime.entities.provider_entities import ConfigurateMethod, ProviderEntity
from models.provider import Provider, ProviderCredential, ProviderType, TenantPreferredModelProvider
+from services.feature_service import PluginInstallationPermissionModel, PluginInstallationScope
MODULE = "core.plugin.plugin_service"
TENANT_ID = "11111111-1111-1111-1111-111111111111"
@@ -1018,11 +1019,15 @@ class TestPluginModelProviderCacheInvalidation:
patch(f"{MODULE}.PluginService.invalidate_plugin_model_providers_cache") as invalidate_cache,
):
mock_config.MARKETPLACE_ENABLED = True
- feature_service.get_system_features.return_value = SimpleNamespace(
- plugin_installation_permission=SimpleNamespace(restrict_to_marketplace_only=False)
+ feature_service.get_plugin_installation_permission.return_value = PluginInstallationPermissionModel(
+ restrict_to_marketplace_only=False,
+ plugin_installation_scope=PluginInstallationScope.ALL,
)
installer = installer_cls.return_value
installer.fetch_plugin_manifest.return_value = MagicMock()
+ decode_response = MagicMock()
+ decode_response.verification = None
+ installer.decode_plugin_from_identifier.return_value = decode_response
installer.upgrade_plugin.return_value = "task-id"
from core.plugin.plugin_service import PluginService
diff --git a/api/tests/unit_tests/services/plugin/test_plugin_service_installation.py b/api/tests/unit_tests/services/plugin/test_plugin_service_installation.py
index b75d469e5d5..3402f558cec 100644
--- a/api/tests/unit_tests/services/plugin/test_plugin_service_installation.py
+++ b/api/tests/unit_tests/services/plugin/test_plugin_service_installation.py
@@ -294,6 +294,71 @@ class TestUpgradePluginWithMarketplace:
mock_download.assert_called_once_with("new-uid")
installer.upload_pkg.assert_called_once()
+ @pytest.mark.parametrize(
+ "scope",
+ [PluginInstallationScope.OFFICIAL_ONLY, PluginInstallationScope.OFFICIAL_AND_SPECIFIC_PARTNERS],
+ )
+ @patch("core.plugin.plugin_service.download_plugin_pkg")
+ @patch("core.plugin.plugin_service.marketplace")
+ @patch("core.plugin.plugin_service.FeatureService")
+ @patch("core.plugin.plugin_service.PluginInstaller")
+ @patch("core.plugin.plugin_service.dify_config")
+ def test_rejects_cached_pkg_outside_scope(
+ self, mock_config, mock_installer_cls, mock_fs, mock_marketplace, mock_download, scope
+ ):
+ mock_config.MARKETPLACE_ENABLED = True
+ mock_fs.get_plugin_installation_permission.return_value = _make_permission(scope=scope)
+ installer = mock_installer_cls.return_value
+ installer.fetch_plugin_manifest.return_value = MagicMock()
+ decode_resp = MagicMock()
+ decode_resp.verification.authorized_category = PluginVerification.AuthorizedCategory.Community
+ installer.decode_plugin_from_identifier.return_value = decode_resp
+
+ with pytest.raises(PluginInstallationForbiddenError):
+ PluginService.upgrade_plugin_with_marketplace("t1", "old-uid", "new-uid")
+
+ installer.upgrade_plugin.assert_not_called()
+ mock_marketplace.record_install_plugin_event.assert_not_called()
+ # the rejection must not fall through to the download branch and cache the pkg again
+ mock_download.assert_not_called()
+ installer.upload_pkg.assert_not_called()
+
+ @patch("core.plugin.plugin_service.FeatureService")
+ @patch("core.plugin.plugin_service.PluginInstaller")
+ @patch("core.plugin.plugin_service.dify_config")
+ def test_rejects_before_touching_daemon_when_scope_is_none(self, mock_config, mock_installer_cls, mock_fs):
+ mock_config.MARKETPLACE_ENABLED = True
+ mock_fs.get_plugin_installation_permission.return_value = _make_permission(scope=PluginInstallationScope.NONE)
+ installer = mock_installer_cls.return_value
+
+ with pytest.raises(PluginInstallationForbiddenError):
+ PluginService.upgrade_plugin_with_marketplace("t1", "old-uid", "new-uid")
+
+ installer.fetch_plugin_manifest.assert_not_called()
+ installer.upgrade_plugin.assert_not_called()
+
+ @patch("core.plugin.plugin_service.marketplace")
+ @patch("core.plugin.plugin_service.FeatureService")
+ @patch("core.plugin.plugin_service.PluginInstaller")
+ @patch("core.plugin.plugin_service.dify_config")
+ def test_allows_cached_official_pkg_under_official_only(
+ self, mock_config, mock_installer_cls, mock_fs, mock_marketplace
+ ):
+ mock_config.MARKETPLACE_ENABLED = True
+ mock_fs.get_plugin_installation_permission.return_value = _make_permission(
+ scope=PluginInstallationScope.OFFICIAL_ONLY
+ )
+ installer = mock_installer_cls.return_value
+ installer.fetch_plugin_manifest.return_value = MagicMock()
+ decode_resp = MagicMock()
+ decode_resp.verification.authorized_category = PluginVerification.AuthorizedCategory.Langgenius
+ installer.decode_plugin_from_identifier.return_value = decode_resp
+
+ PluginService.upgrade_plugin_with_marketplace("t1", "old-uid", "new-uid")
+
+ mock_marketplace.record_install_plugin_event.assert_called_once_with("new-uid")
+ installer.upgrade_plugin.assert_called_once()
+
class TestUpgradePluginWithGithub:
@patch("core.plugin.plugin_service.FeatureService")
@@ -374,6 +439,29 @@ class TestInstallFromMarketplacePkg:
call_args = installer.install_from_identifiers.call_args[0]
assert call_args[1] == ["uid-1"]
+ @patch("core.plugin.plugin_service.download_plugin_pkg")
+ @patch("core.plugin.plugin_service.FeatureService")
+ @patch("core.plugin.plugin_service.PluginInstaller")
+ @patch("core.plugin.plugin_service.dify_config")
+ def test_rejects_cached_pkg_outside_scope(self, mock_config, mock_installer_cls, mock_fs, mock_download):
+ mock_config.MARKETPLACE_ENABLED = True
+ mock_fs.get_plugin_installation_permission.return_value = _make_permission(
+ scope=PluginInstallationScope.OFFICIAL_ONLY
+ )
+ installer = mock_installer_cls.return_value
+ installer.fetch_plugin_manifest.return_value = MagicMock()
+ decode_resp = MagicMock()
+ decode_resp.verification.authorized_category = PluginVerification.AuthorizedCategory.Community
+ installer.decode_plugin_from_identifier.return_value = decode_resp
+
+ with pytest.raises(PluginInstallationForbiddenError):
+ PluginService.install_from_marketplace_pkg("t1", ["uid-1"])
+
+ installer.install_from_identifiers.assert_not_called()
+ # the rejection must not fall through to the download branch and cache the pkg again
+ mock_download.assert_not_called()
+ installer.upload_pkg.assert_not_called()
+
class TestUninstall:
@patch("core.plugin.plugin_service.PluginInstaller")