ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(plugins): stamp source_type='registry' on registry installs
Juan Denis53220e578268
diff --git a/backend/app/services/plugin_service.py b/backend/app/services/plugin_service.pyindex c4d40f59..d97fc62e 100644--- a/backend/app/services/plugin_service.py+++ b/backend/app/services/plugin_service.py@@ -395,7 +395,7 @@ def _update_plugin_metadata(plugin, manifest):def install_from_url(url, user_id=None, expected_sha256=None, force=False,- hot_load=True):+ hot_load=True, source_type=None):"""Download and install a plugin from a URL.Args:@@ -407,6 +407,9 @@ def install_from_url(url, user_id=None, expected_sha256=None, force=False,hot_load: register the blueprint into the running app immediately.The boot-time repair pass (#48) passes False — load_all_pluginsregisters right after, and a double registration would error.+ source_type: override the stamped source_type (defaults to 'url').+ Registry installs pass 'registry' so the row records where the+ plugin really came from.Returns:InstalledPlugin instance@@ -427,12 +430,14 @@ def install_from_url(url, user_id=None, expected_sha256=None, force=False,buf.seek(0)return _install_from_buffer(- buf, source_url=url, source_type='url', user_id=user_id, force=force,+ buf, source_url=url, source_type=source_type or 'url',+ user_id=user_id, force=force,hot_load=hot_load,)-def install_from_path(path, user_id=None, force=False, hot_load=True):+def install_from_path(path, user_id=None, force=False, hot_load=True,+ source_type=None):"""Install a plugin from a local directory on the panel host.Useful during plugin development: point at the working tree, install,@@ -496,8 +501,8 @@ def install_from_path(path, user_id=None, force=False, hot_load=True):buf.seek(0)return _install_from_buffer(- buf, source_url=path, source_type='local', user_id=user_id,- force=force, hot_load=hot_load,+ buf, source_url=path, source_type=source_type or 'local',+ user_id=user_id, force=force, hot_load=hot_load,)@@ -937,7 +942,8 @@ def repair_missing_plugins():- builtin slug → reinstall from ``builtin-extensions/`` (in-repo,always present in the fresh tree)- - source_type 'url' → re-download from ``source_url`` (no checksum —+ - source_type 'url' or 'registry'+ → re-download from ``source_url`` (no checksum —the original install verified it; best effort)- anything else → mark `error` with a re-upload hint@@ -983,11 +989,13 @@ def repair_missing_plugins():if slug in builtin_by_slug:install_from_path(builtin_by_slug[slug]['path'],user_id=plugin.installed_by,- force=True, hot_load=False)+ force=True, hot_load=False,+ source_type='builtin')logger.info(f'Plugin {slug}: restored from builtin-extensions/')- elif plugin.source_type == 'url' and plugin.source_url:+ elif plugin.source_type in ('url', 'registry') and plugin.source_url:install_from_url(plugin.source_url, user_id=plugin.installed_by,- force=True, hot_load=False)+ force=True, hot_load=False,+ source_type=plugin.source_type)logger.info(f'Plugin {slug}: reinstalled from {plugin.source_url}')else:plugin.status = InstalledPlugin.STATUS_ERROR@@ -1391,7 +1399,8 @@ def install_builtin_extension(slug, user_id=None):for entry in list_builtin_extensions():if entry['slug'] == slug:- return install_from_path(entry['path'], user_id=user_id)+ return install_from_path(entry['path'], user_id=user_id,+ source_type='builtin')raise ValueError(f"No builtin extension with slug '{slug}'")@@ -1542,6 +1551,7 @@ def install_registry_extension(slug, user_id=None):raise ValueError(f"Registry entry '{slug}' has no source URL.")return install_from_url(source, user_id=user_id, expected_sha256=entry.get('sha256'),+ source_type='registry',)@@ -1590,6 +1600,13 @@ def update_plugin(plugin_id, user_id=None):source = (entry.get('source') or '').strip()if not source:raise ValueError('Registry entry has no source URL.')+ # The update itself is registry-driven, so plain url/registry rows get+ # stamped 'registry'; builtin/local/upload rows keep their identity.+ if plugin.source_type in (None, '', 'url', 'registry'):+ stamped = 'registry'+ else:+ stamped = plugin.source_typereturn install_from_url(source, user_id=user_id, expected_sha256=entry.get('sha256'), force=True,+ source_type=stamped,)diff --git a/backend/tests/test_plugins_pipeline.py b/backend/tests/test_plugins_pipeline.pyindex 87bb61b8..dd5ddd75 100644--- a/backend/tests/test_plugins_pipeline.py+++ b/backend/tests/test_plugins_pipeline.py@@ -112,6 +112,7 @@ def test_install_builtin_creates_active_plugin(app, plugin_dirs):plugin = plugin_service.install_builtin_extension('serverkit-demo')assert plugin.status == InstalledPlugin.STATUS_ACTIVE+ assert plugin.source_type == 'builtin'assert plugin.has_frontend is Trueassert plugin.has_backend is False# Contributions survive the round-trip through the manifest.@@ -121,6 +122,30 @@ def test_install_builtin_creates_active_plugin(app, plugin_dirs):assert (plugin_dirs['frontend'] / 'serverkit-demo' / 'index.jsx').exists()+def test_install_from_path_stamps_local(app, plugin_dirs):+ folder, _ = _write_builtin(plugin_dirs['builtin'], slug='local-demo')+ plugin = plugin_service.install_from_path(str(folder))+ assert plugin.status == InstalledPlugin.STATUS_ACTIVE+ assert plugin.source_type == 'local'+++def test_install_from_zip_stamps_upload(app, plugin_dirs):+ import io+ import zipfile+ manifest = {+ 'name': 'zip-demo', 'display_name': 'Zip Demo', 'version': '1.0.0',+ 'category': 'utility',+ 'contributions': {'nav': [{'id': 'zip-demo', 'label': 'Z', 'route': '/z'}]},+ }+ buf = io.BytesIO()+ with zipfile.ZipFile(buf, 'w') as zf:+ zf.writestr('plugin.json', json.dumps(manifest))+ zf.writestr('frontend/index.jsx', 'export function P(){return null;}\n')+ plugin = plugin_service.install_from_zip(buf.getvalue())+ assert plugin.status == InstalledPlugin.STATUS_ACTIVE+ assert plugin.source_type == 'upload'++def test_reinstall_refreshes_metadata(app, plugin_dirs):folder, manifest = _write_builtin(plugin_dirs['builtin'])plugin_service.install_builtin_extension('serverkit-demo')diff --git a/backend/tests/test_registry.py b/backend/tests/test_registry.pyindex 0b6d854e..1f59d46f 100644--- a/backend/tests/test_registry.py+++ b/backend/tests/test_registry.py@@ -118,6 +118,28 @@ def test_checksum_match_installs(app, plugin_dirs, monkeypatch):plugin = plugin_service.install_from_url('https://x/y.zip', expected_sha256=digest)assert plugin.status == InstalledPlugin.STATUS_ACTIVEassert plugin.version == '2.0.0'+ # A plain URL install keeps the historical stamp.+ assert plugin.source_type == 'url'+++def test_registry_install_stamps_registry_source_type(app, plugin_dirs, monkeypatch):+ """install_registry_extension records where the plugin really came from."""+ zip_bytes = _make_plugin_zip()+ digest = hashlib.sha256(zip_bytes).hexdigest()+ monkeypatch.setattr(plugin_service, '_download_zip', lambda url: io.BytesIO(zip_bytes))+ monkeypatch.setattr(registry_service, '_cache', {+ 'ts': 9e18, 'source': 'test',+ 'entries': [registry_service._normalize({+ 'slug': 'regext', 'display_name': 'Registry Ext', 'version': '2.0.0',+ 'source': 'https://x/regext.zip', 'sha256': digest,+ 'min_panel_version': '0.0.1',+ })],+ })++ plugin = plugin_service.install_registry_extension('regext')+ assert plugin.status == InstalledPlugin.STATUS_ACTIVE+ assert plugin.source_type == 'registry'+ assert plugin.source_url == 'https://x/regext.zip'# --------------------------------------------------------------------------- #@@ -130,6 +152,7 @@ def test_update_flow(app, plugin_dirs, monkeypatch):monkeypatch.setattr(plugin_service, '_download_zip', lambda url: io.BytesIO(v1))plugin = plugin_service.install_from_url('https://x/regext.zip')assert plugin.version == '1.0.0'+ assert plugin.source_type == 'url'# Registry advertises v2.0.0 for the same slug.monkeypatch.setattr(registry_service, '_cache', {@@ -151,6 +174,65 @@ def test_update_flow(app, plugin_dirs, monkeypatch):updated = plugin_service.update_plugin(plugin.id)assert updated.version == '2.0.0'assert InstalledPlugin.query.filter_by(slug='regext').count() == 1+ # The update itself came from the registry — the row now says so.+ assert updated.source_type == 'registry'+++def test_update_keeps_registry_source_type(app, plugin_dirs, monkeypatch):+ """A registry-installed plugin stays source_type='registry' after update."""+ v1 = _make_plugin_zip(slug='regext', version='1.0.0')+ digest_v1 = hashlib.sha256(v1).hexdigest()+ monkeypatch.setattr(plugin_service, '_download_zip', lambda url: io.BytesIO(v1))+ monkeypatch.setattr(registry_service, '_cache', {+ 'ts': 9e18, 'source': 'test',+ 'entries': [registry_service._normalize({+ 'slug': 'regext', 'display_name': 'Registry Ext', 'version': '1.0.0',+ 'source': 'https://x/regext.zip', 'sha256': digest_v1,+ 'min_panel_version': '0.0.1',+ })],+ })+ plugin = plugin_service.install_registry_extension('regext')+ assert plugin.source_type == 'registry'++ # Registry moves to v2; run the update.+ v2 = _make_plugin_zip(slug='regext', version='2.0.0')+ monkeypatch.setattr(plugin_service, '_download_zip', lambda url: io.BytesIO(v2))+ monkeypatch.setattr(registry_service, '_cache', {+ 'ts': 9e18, 'source': 'test',+ 'entries': [registry_service._normalize({+ 'slug': 'regext', 'display_name': 'Registry Ext', 'version': '2.0.0',+ 'source': 'https://x/regext.zip',+ 'sha256': hashlib.sha256(v2).hexdigest(),+ 'min_panel_version': '0.0.1',+ })],+ })+ updated = plugin_service.update_plugin(plugin.id)+ assert updated.version == '2.0.0'+ assert updated.source_type == 'registry'+++def test_update_preserves_builtin_source_type(app, plugin_dirs, monkeypatch):+ """A registry-driven update must not clobber a builtin/local/upload stamp."""+ v1 = _make_plugin_zip(slug='regext', version='1.0.0')+ monkeypatch.setattr(plugin_service, '_download_zip', lambda url: io.BytesIO(v1))+ plugin = plugin_service.install_from_url('https://x/regext.zip')+ plugin.source_type = 'builtin'+ db.session.commit()++ v2 = _make_plugin_zip(slug='regext', version='2.0.0')+ monkeypatch.setattr(plugin_service, '_download_zip', lambda url: io.BytesIO(v2))+ monkeypatch.setattr(registry_service, '_cache', {+ 'ts': 9e18, 'source': 'test',+ 'entries': [registry_service._normalize({+ 'slug': 'regext', 'display_name': 'Registry Ext', 'version': '2.0.0',+ 'source': 'https://x/regext.zip',+ 'sha256': hashlib.sha256(v2).hexdigest(),+ 'min_panel_version': '0.0.1',+ })],+ })+ updated = plugin_service.update_plugin(plugin.id)+ assert updated.version == '2.0.0'+ assert updated.source_type == 'builtin'# --------------------------------------------------------------------------- #