From 2637a326fb86594da42d20e0f8af09a4a47c4dde Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Wed, 14 Jul 2021 15:07:31 +0200 Subject: [PATCH] [MM-36563] Only read body from inter-plugin request if there is one (#17798) --- app/plugin_api_test.go | 55 ++++++++++++++++++++++++++++++------------ plugin/client_rpc.go | 23 ++++++++++-------- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/app/plugin_api_test.go b/app/plugin_api_test.go index 4a81a6c83a..a189958718 100644 --- a/app/plugin_api_test.go +++ b/app/plugin_api_test.go @@ -1414,23 +1414,27 @@ func TestInterpluginPluginHTTP(t *testing.T) { } func (p *MyPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/api/v2/test" { - return - } + switch r.URL.Path { + case "/api/v2/test": + if r.URL.Query().Get("abc") != "xyz" { + return + } - if r.URL.Query().Get("abc") != "xyz" { - return - } + if r.Header.Get("Mattermost-Plugin-ID") != "testplugininterclient" { + return + } - if r.Header.Get("Mattermost-Plugin-ID") != "testplugininterclient" { - return + buf := bytes.Buffer{} + buf.ReadFrom(r.Body) + resp := "we got:" + buf.String() + w.WriteHeader(598) + w.Write([]byte(resp)) + if r.URL.Path != "/api/v2/test" { + return + } + case "/nobody": + w.WriteHeader(599) } - - buf := bytes.Buffer{} - buf.ReadFrom(r.Body) - resp := "we got:" + buf.String() - w.WriteHeader(598) - w.Write([]byte(resp)) } func main() { @@ -1474,7 +1478,26 @@ func TestInterpluginPluginHTTP(t *testing.T) { if resp.StatusCode != 598 { return nil, "wrong status " + string(respbody) } - return nil, string(respbody) + + if string(respbody) != "we got:This is the request" { + return nil, "wrong response " + string(respbody) + } + + req, err = http.NewRequest("GET", "/testplugininterserver/nobody", nil) + if err != nil { + return nil, err.Error() + } + + resp = p.API.PluginHTTP(req) + if resp == nil { + return nil, "Nil resp" + } + + if resp.StatusCode != 599 { + return nil, "wrong status " + string(respbody) + } + + return nil, "ok" } func main() { @@ -1498,7 +1521,7 @@ func TestInterpluginPluginHTTP(t *testing.T) { hooks, err := th.App.GetPluginsEnvironment().HooksForPlugin("testplugininterclient") require.NoError(t, err) _, ret := hooks.MessageWillBePosted(nil, nil) - assert.Equal(t, "we got:This is the request", ret) + assert.Equal(t, "ok", ret) } func TestApiMetrics(t *testing.T) { diff --git a/plugin/client_rpc.go b/plugin/client_rpc.go index 58c0841a8c..798a6c79f3 100644 --- a/plugin/client_rpc.go +++ b/plugin/client_rpc.go @@ -475,17 +475,20 @@ func (g *apiRPCClient) PluginHTTP(request *http.Request) *http.Response { RequestURI: request.RequestURI, } - requestBody, err := ioutil.ReadAll(request.Body) - if err != nil { - log.Printf("RPC call to PluginHTTP API failed: %s", err.Error()) - return nil - } - request.Body.Close() - request.Body = nil - _args := &Z_PluginHTTPArgs{ - Request: forwardedRequest, - RequestBody: requestBody, + Request: forwardedRequest, + } + + if request.Body != nil { + requestBody, err := ioutil.ReadAll(request.Body) + if err != nil { + log.Printf("RPC call to PluginHTTP API failed: %s", err.Error()) + return nil + } + request.Body.Close() + request.Body = nil + + _args.RequestBody = requestBody } _returns := &Z_PluginHTTPReturns{}