Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
Handle plugin register manifiest mismatches to show error
Gabe Kangasc9af8af2c443
diff --git a/pluginhost/registry.go b/pluginhost/registry.goindex c20d3e1dd0..038e96cb05 100644--- a/pluginhost/registry.go+++ b/pluginhost/registry.go@@ -158,7 +158,7 @@ func (p *Host) handleRegistryInstall(w http.ResponseWriter, r *http.Request) {entry, err := p.manager.Install(r.Context(), pkgBytes)if err != nil {- writeJSONResponse(w, http.StatusBadRequest, map[string]string{jsonErrorKey: err.Error()})+ writeJSONResponse(w, http.StatusBadRequest, map[string]string{jsonErrorKey: "plugin cannot be installed: " + err.Error()})return}log.Infof("plugin %q [%s] v%s installed from registry by admin", entry.DisplayName, entry.Slug, entry.Version)diff --git a/pluginhost/registry_test.go b/pluginhost/registry_test.goindex ed44a4f442..89289870de 100644--- a/pluginhost/registry_test.go+++ b/pluginhost/registry_test.go@@ -283,6 +283,43 @@ func TestHandleRegistryInstall_Success(t *testing.T) {}}+func TestHandleRegistryInstall_RejectsUnloadablePackage(t *testing.T) {+ wasmPath := findExampleWasm(t)+ wasmBytes, _ := os.ReadFile(wasmPath)+ pkg := buildPackageBytes(t, []byte(`{+ "api": "1",+ "name": "hello-world",+ "version": "9.9.9",+ "description": "mismatched manifest/runtime for registry install",+ "permissions": []+ }`), wasmBytes)+ sum := sha256.Sum256(pkg)+ sha := hex.EncodeToString(sum[:])++ upstream := newRegistryStub(t, "hello-world", "9.9.9", pkg, sha)+ withRegistryEnv(t, upstream.URL)+ host := newTestHost(t)++ rec := httptest.NewRecorder()+ req := httptest.NewRequest(http.MethodPost, "/api/admin/plugin-registry/install",+ strings.NewReader(`{"slug":"hello-world","version":"9.9.9"}`))+ host.handleRegistryInstall(rec, req)++ if rec.Code != http.StatusBadRequest {+ t.Fatalf("status = %d, want 400 (body=%s)", rec.Code, rec.Body.String())+ }+ body := rec.Body.String()+ if !strings.Contains(body, "plugin cannot be installed") {+ t.Errorf("error should say plugin cannot be installed, got %s", body)+ }+ if !strings.Contains(body, "manifest/runtime mismatch") {+ t.Errorf("error should surface the load problem, got %s", body)+ }+ if len(host.manager.List()) != 0 {+ t.Fatalf("unloadable package should not be installed")+ }+}+// --- /api/admin/plugin-registry/<unknown> ---func TestHandleRegistryRoute_DispatchesActions(t *testing.T) {diff --git a/services/plugins/manager.go b/services/plugins/manager.goindex 102b3effb1..3e69d558f8 100644--- a/services/plugins/manager.go+++ b/services/plugins/manager.go@@ -492,10 +492,12 @@ func (m *Manager) Enable(ctx context.Context, name string) error {return err}-// validateUploadedPackage checks an uploaded .ocpkg's bytes and returns-// its manifest. Pulled out of Install so the validation steps and the-// (lock + write + scan) plumbing don't combine into one long function.-func validateUploadedPackage(packageBytes []byte) (*Manifest, error) {+// validateUploadedPackage checks an uploaded .ocpkg's bytes, verifies the+// wasm can complete the same register()/manifest-agreement path used by a+// real load, and returns its manifest. Pulled out of Install so the+// validation steps and the (lock + write + scan) plumbing don't combine into+// one long function.+func validateUploadedPackage(ctx context.Context, env *HostEnv, packageBytes []byte) (*Manifest, error) {if len(packageBytes) == 0 {return nil, fmt.Errorf("upload is empty")}@@ -510,13 +512,25 @@ func validateUploadedPackage(packageBytes []byte) (*Manifest, error) {if err != nil {return nil, fmt.Errorf("missing manifest: %w", err)}- if _, err := readZipFile(zr, pkgWasmFilename); err != nil {+ wasmBytes, err := readZipFile(zr, pkgWasmFilename)+ if err != nil {return nil, fmt.Errorf("missing compiled plugin: %w", err)}manifest, err := ParseManifest(manifestBytes)if err != nil {return nil, fmt.Errorf("invalid manifest: %w", err)}+ // Preflight the package through the real load path before we write it into+ // the plugins directory. Without this, an .ocpkg whose manifest parses but+ // whose wasm fails register() or disagrees with the manifest appears to+ // "install" successfully from the catalog, then only surfaces as a+ // discovered-but-broken plugin later. The admin clicked Install, so the+ // operation should fail up front if the package cannot be loaded.+ loaded, err := loadFromBytes(ctx, env, manifestBytes, wasmBytes, manifest.Slug)+ if err != nil {+ return nil, err+ }+ loaded.Close(ctx)return manifest, nil}@@ -595,7 +609,7 @@ const MaxUploadBytes = 50 * 1024 * 1024// named .ocpkg ends up replacing the right file. Returns the discovered// entry for the installed plugin.func (m *Manager) Install(ctx context.Context, packageBytes []byte) (*DiscoveredEntry, error) {- manifest, err := validateUploadedPackage(packageBytes)+ manifest, err := validateUploadedPackage(ctx, m.env, packageBytes)if err != nil {return nil, err}diff --git a/services/plugins/manager_test.go b/services/plugins/manager_test.goindex 8f2b401f67..812017bf54 100644--- a/services/plugins/manager_test.go+++ b/services/plugins/manager_test.go@@ -518,6 +518,31 @@ func TestManager_Install_RejectsMissingManifest(t *testing.T) {}}+func TestManager_Install_RejectsUnloadablePackage(t *testing.T) {+ wasmPath := findExampleWasm(t)+ wasmBytes, _ := os.ReadFile(wasmPath)++ mgr := NewManager(t.TempDir(), &HostEnv{})+ pkg := buildPackageBytes(t, []byte(`{+ "api": "1",+ "name": "hello-world",+ "version": "9.9.9",+ "description": "mismatched manifest/runtime for install preflight",+ "permissions": []+ }`), wasmBytes, nil)+ _, err := mgr.Install(context.Background(), pkg)+ if err == nil {+ t.Fatal("expected error for unloadable package")+ }+ if !strings.Contains(err.Error(), "manifest/runtime mismatch") {+ t.Errorf("error should mention manifest/runtime mismatch: got %v", err)+ }+ entries := mgr.List()+ if len(entries) != 0 {+ t.Fatalf("unloadable package should not be discovered, got %d entries", len(entries))+ }+}+func TestManager_Uninstall_DeletesFileAndClearsState(t *testing.T) {wasmPath := findExampleWasm(t)wasmBytes, _ := os.ReadFile(wasmPath)diff --git a/web/pages/admin/plugins.tsx b/web/pages/admin/plugins.tsxindex ae85f50026..9bd50e8391 100644--- a/web/pages/admin/plugins.tsx+++ b/web/pages/admin/plugins.tsx@@ -268,7 +268,9 @@ const Plugins = () => {setPendingEnable(entry);}} catch (e) {- setError(e instanceof Error ? e.message : String(e));+ const msg = e instanceof Error ? e.message : String(e);+ setError(msg);+ message.error(msg);}},[loadPlugins, loadRegistry, t],diff --git a/web/tests/plugins-page.test.tsx b/web/tests/plugins-page.test.tsxindex 817027a366..a83da968f5 100644--- a/web/tests/plugins-page.test.tsx+++ b/web/tests/plugins-page.test.tsx@@ -1,5 +1,6 @@import React from 'react';import { fireEvent, render, screen, waitFor } from '@testing-library/react';+import { message } from 'antd';import Plugins from '../pages/admin/plugins';import {fetchData,@@ -24,6 +25,18 @@ jest.mock('next-export-i18n', () => ({}),}));+jest.mock('antd', () => {+ const actual = jest.requireActual('antd');+ return {+ ...actual,+ message: {+ ...actual.message,+ error: jest.fn(),+ success: jest.fn(),+ },+ };+});+jest.mock('../components/layouts/AdminLayout', () => ({AdminLayout: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,}));@@ -69,6 +82,7 @@ const mockedFetchData = fetchData as jest.MockedFunction<typeof fetchData>;describe('Plugins admin page', () => {beforeEach(() => {mockedFetchData.mockReset();+ jest.clearAllMocks();});test('surfaces registry install errors to the admin page', async () => {@@ -94,5 +108,9 @@ describe('Plugins admin page', () => {screen.getByText('invalid manifest: manifest.scripts requires the "http.serve" permission'),).toBeInTheDocument();});++ expect(message.error).toHaveBeenCalledWith(+ 'invalid manifest: manifest.scripts requires the "http.serve" permission',+ );});});