From 51d790300fbd85b7238795857da0f712e3b600b9 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Fri, 2 Oct 2020 09:25:50 +0200 Subject: [PATCH] Fix possible panic during plugin installation (#15689) --- app/plugin_install.go | 2 ++ app/plugin_install_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/app/plugin_install.go b/app/plugin_install.go index 1044ea3844..fc8cd97593 100644 --- a/app/plugin_install.go +++ b/app/plugin_install.go @@ -376,6 +376,8 @@ func (a *App) installExtractedPlugin(manifest *model.Manifest, fromPluginDir str updatedManifest, _, err := pluginsEnvironment.Activate(manifest.Id) if err != nil { return nil, model.NewAppError("installExtractedPlugin", "app.plugin.restart.app_error", nil, err.Error(), http.StatusInternalServerError) + } else if updatedManifest == nil { + return nil, model.NewAppError("installExtractedPlugin", "app.plugin.restart.app_error", nil, "failed to activate plugin: plugin already active", http.StatusInternalServerError) } manifest = updatedManifest } diff --git a/app/plugin_install_test.go b/app/plugin_install_test.go index 74989be443..74b7c57275 100644 --- a/app/plugin_install_test.go +++ b/app/plugin_install_test.go @@ -8,10 +8,14 @@ import ( "bytes" "compress/gzip" "io" + "os" + "path/filepath" "sort" "testing" "github.com/mattermost/mattermost-server/v5/model" + "github.com/mattermost/mattermost-server/v5/utils/fileutils" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -259,3 +263,35 @@ func TestInstallPluginLocally(t *testing.T) { }) }) } + +func TestInstallPluginAlreadyActive(t *testing.T) { + th := Setup(t) + defer th.TearDown() + + path, _ := fileutils.FindDir("tests") + reader, err := os.Open(filepath.Join(path, "testplugin.tar.gz")) + require.NoError(t, err) + + actualManifest, appError := th.App.InstallPlugin(reader, true) + require.NotNil(t, actualManifest) + require.Nil(t, appError) + appError = th.App.EnablePlugin(actualManifest.Id) + require.Nil(t, appError) + + pluginsEnvironment := th.App.GetPluginsEnvironment() + require.NotNil(t, pluginsEnvironment) + bundleInfos, err := pluginsEnvironment.Available() + require.Nil(t, err) + require.NotEmpty(t, bundleInfos) + for _, bundleInfo := range bundleInfos { + if bundleInfo.Manifest.Id == actualManifest.Id { + err := os.RemoveAll(bundleInfo.Path) + require.NoError(t, err) + } + } + + actualManifest, appError = th.App.InstallPlugin(reader, true) + require.NotNil(t, appError) + require.Nil(t, actualManifest) + require.Equal(t, "app.plugin.restart.app_error", appError.Id) +}