From b7ee6ca8c4ff40521e326b540aa80657187ba2b8 Mon Sep 17 00:00:00 2001 From: InfinityPacer Date: Sun, 19 Apr 2026 05:28:40 +0800 Subject: [PATCH] fix(plugin): sanitize local repo path telemetry --- app/helper/plugin.py | 33 +++++++++++++++++++++++++++++---- tests/test_plugin_helper.py | 23 +++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/test_plugin_helper.py diff --git a/app/helper/plugin.py b/app/helper/plugin.py index 7714abfc5..c59f471ac 100644 --- a/app/helper/plugin.py +++ b/app/helper/plugin.py @@ -123,6 +123,25 @@ class PluginHelper(metaclass=WeakSingleton): except Exception: return None + @staticmethod + def sanitize_repo_url_for_statistic(repo_url: Optional[str]) -> Optional[str]: + """ + 统计上报前脱敏 repo_url,避免泄露本地仓库绝对路径 + """ + if not repo_url: + return repo_url + if not PluginHelper.is_local_repo_url(repo_url): + return repo_url + + pid = PluginHelper.parse_local_repo_url(repo_url) + if not pid: + return LOCAL_REPO_PREFIX.rstrip("/") + + return PluginHelper.make_local_repo_url( + pid=pid, + package_version=PluginHelper.parse_local_repo_package_version(repo_url) + ) + @staticmethod def get_local_repo_paths() -> List[Path]: """ @@ -410,7 +429,7 @@ class PluginHelper(metaclass=WeakSingleton): timeout=5 ).post(install_reg_url, json={ "plugin_id": pid, - "repo_url": repo_url + "repo_url": self.sanitize_repo_url_for_statistic(repo_url) }) if res is not None and res.status_code == 200: return True @@ -427,7 +446,10 @@ class PluginHelper(metaclass=WeakSingleton): if items: for pid, repo_url in items: if pid: - payload_plugins.append({"plugin_id": pid, "repo_url": repo_url}) + payload_plugins.append({ + "plugin_id": pid, + "repo_url": self.sanitize_repo_url_for_statistic(repo_url) + }) else: plugins = self.systemconfig.get(SystemConfigKey.UserInstalledPlugins) if not plugins: @@ -1323,7 +1345,7 @@ class PluginHelper(metaclass=WeakSingleton): timeout=5 ).post(install_reg_url, json={ "plugin_id": pid, - "repo_url": repo_url + "repo_url": self.sanitize_repo_url_for_statistic(repo_url) }) if res is not None and res.status_code == 200: return True @@ -1340,7 +1362,10 @@ class PluginHelper(metaclass=WeakSingleton): if items: for pid, repo_url in items: if pid: - payload_plugins.append({"plugin_id": pid, "repo_url": repo_url}) + payload_plugins.append({ + "plugin_id": pid, + "repo_url": self.sanitize_repo_url_for_statistic(repo_url) + }) else: plugins = self.systemconfig.get(SystemConfigKey.UserInstalledPlugins) if not plugins: diff --git a/tests/test_plugin_helper.py b/tests/test_plugin_helper.py new file mode 100644 index 000000000..6a25be0cd --- /dev/null +++ b/tests/test_plugin_helper.py @@ -0,0 +1,23 @@ +from unittest import TestCase + + +class PluginHelperTest(TestCase): + + def test_sanitize_repo_url_for_statistic_keeps_remote_url(self): + try: + from app.helper.plugin import PluginHelper + except ModuleNotFoundError as exc: + self.skipTest(f"missing dependency: {exc}") + repo_url = "https://github.com/InfinityPacer/MoviePilot-Plugins" + self.assertEqual(repo_url, PluginHelper.sanitize_repo_url_for_statistic(repo_url)) + + def test_sanitize_repo_url_for_statistic_strips_local_path(self): + try: + from app.helper.plugin import PluginHelper + except ModuleNotFoundError as exc: + self.skipTest(f"missing dependency: {exc}") + repo_url = "local://TestPlugin?path=/Users/InfinityPacer/GitHub/MoviePilot/MoviePilot-Plugins&version=v2" + self.assertEqual( + "local://TestPlugin?version=v2", + PluginHelper.sanitize_repo_url_for_statistic(repo_url) + )