summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Hallam <jesse.hallam@gmail.com>2018-07-26 08:33:56 -0400
committerJoram Wilander <jwawilander@gmail.com>2018-07-26 08:33:56 -0400
commitf8f80d80df1e27e2ed4dcc91518bf504ab7f3e34 (patch)
tree1db540f2605936e290596445dc08267d8f4aec9e
parentb3c2ecd9b9209413e7272b2fcd7bd3d04f2f85f4 (diff)
downloadchat-f8f80d80df1e27e2ed4dcc91518bf504ab7f3e34.tar.gz
chat-f8f80d80df1e27e2ed4dcc91518bf504ab7f3e34.tar.bz2
chat-f8f80d80df1e27e2ed4dcc91518bf504ab7f3e34.zip
Fix plugin.ServeHTTP subpath (#9161)
* test ServicePluginRequest with subpath * handle subpath when routing to plugin.ServeHTTP
-rw-r--r--app/plugin_requests.go8
-rw-r--r--app/plugin_test.go48
2 files changed, 54 insertions, 2 deletions
diff --git a/app/plugin_requests.go b/app/plugin_requests.go
index 1c5a73e8c..2f9ac1476 100644
--- a/app/plugin_requests.go
+++ b/app/plugin_requests.go
@@ -5,12 +5,14 @@ package app
import (
"net/http"
+ "path"
"strings"
"github.com/gorilla/mux"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
+ "github.com/mattermost/mattermost-server/utils"
)
func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
@@ -26,7 +28,7 @@ func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
hooks, err := a.Plugins.HooksForPlugin(params["plugin_id"])
if err != nil {
- a.Log.Error("Access to route for non-existant plugin", mlog.String("missing_plugin_id", params["plugin_id"]), mlog.Err(err))
+ a.Log.Error("Access to route for non-existent plugin", mlog.String("missing_plugin_id", params["plugin_id"]), mlog.Err(err))
http.NotFound(w, r)
return
}
@@ -67,10 +69,12 @@ func (a *App) servePluginRequest(w http.ResponseWriter, r *http.Request, handler
params := mux.Vars(r)
+ subpath, _ := utils.GetSubpathFromConfig(a.Config())
+
newQuery := r.URL.Query()
newQuery.Del("access_token")
r.URL.RawQuery = newQuery.Encode()
- r.URL.Path = strings.TrimPrefix(r.URL.Path, "/plugins/"+params["plugin_id"])
+ r.URL.Path = strings.TrimPrefix(r.URL.Path, path.Join(subpath, "plugins", params["plugin_id"]))
handler(&plugin.Context{}, w, r)
}
diff --git a/app/plugin_test.go b/app/plugin_test.go
index 02b492d28..c051324a7 100644
--- a/app/plugin_test.go
+++ b/app/plugin_test.go
@@ -4,6 +4,8 @@
package app
import (
+ "bytes"
+ "io/ioutil"
"net/http"
"net/http/httptest"
"testing"
@@ -52,6 +54,52 @@ func TestServePluginRequest(t *testing.T) {
assert.Equal(t, http.StatusNotImplemented, w.Result().StatusCode)
}
+func TestPrivateServePluginRequest(t *testing.T) {
+ th := Setup().InitBasic()
+ defer th.TearDown()
+
+ testCases := []struct {
+ Description string
+ ConfigFunc func(cfg *model.Config)
+ URL string
+ ExpectedURL string
+ }{
+ {
+ "no subpath",
+ func(cfg *model.Config) {},
+ "/plugins/id/endpoint",
+ "/endpoint",
+ },
+ {
+ "subpath",
+ func(cfg *model.Config) { *cfg.ServiceSettings.SiteURL += "/subpath" },
+ "/subpath/plugins/id/endpoint",
+ "/endpoint",
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.Description, func(t *testing.T) {
+ th.App.UpdateConfig(testCase.ConfigFunc)
+ expectedBody := []byte("body")
+ request := httptest.NewRequest(http.MethodGet, testCase.URL, bytes.NewReader(expectedBody))
+ recorder := httptest.NewRecorder()
+
+ handler := func(context *plugin.Context, w http.ResponseWriter, r *http.Request) {
+ assert.Equal(t, testCase.ExpectedURL, r.URL.Path)
+
+ body, _ := ioutil.ReadAll(r.Body)
+ assert.Equal(t, expectedBody, body)
+ }
+
+ request = mux.SetURLVars(request, map[string]string{"plugin_id": "id"})
+
+ th.App.servePluginRequest(recorder, request, handler)
+ })
+ }
+
+}
+
func TestHandlePluginRequest(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()