[MM-61458] Fix errcheck issues in server/channels/app/plugin_requests.go (#29121)

This commit is contained in:
molejnik88 2024-11-07 09:09:11 +01:00 committed by GitHub
parent 6b304b9c4a
commit e40d7b442a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 13 deletions

View file

@ -133,7 +133,6 @@ issues:
channels/app/plugin_api_tests/manual.test_serve_metrics_plugin/main.go|\
channels/app/plugin_api_tests/test_update_user_auth_plugin/main.go|\
channels/app/plugin_install.go|\
channels/app/plugin_requests.go|\
channels/app/plugin_signature.go|\
channels/app/plugin_signature_test.go|\
channels/app/plugin_test.go|\

View file

@ -25,11 +25,13 @@ func (ch *Channels) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
pluginsEnvironment := ch.GetPluginsEnvironment()
if pluginsEnvironment == nil {
err := model.NewAppError("ServePluginRequest", "app.plugin.disabled.app_error", nil, "Enable plugins to serve plugin requests", http.StatusNotImplemented)
mlog.Error(err.Error())
w.WriteHeader(err.StatusCode)
appErr := model.NewAppError("ServePluginRequest", "app.plugin.disabled.app_error", nil, "Enable plugins to serve plugin requests", http.StatusNotImplemented)
mlog.Error(appErr.Error())
w.WriteHeader(appErr.StatusCode)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(err.ToJSON()))
if _, err := w.Write([]byte(appErr.ToJSON())); err != nil {
mlog.Warn("Error while writing response", mlog.Err(err))
}
return
}
@ -49,11 +51,13 @@ func (ch *Channels) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
func (a *App) ServeInterPluginRequest(w http.ResponseWriter, r *http.Request, sourcePluginId, destinationPluginId string) {
pluginsEnvironment := a.ch.GetPluginsEnvironment()
if pluginsEnvironment == nil {
err := model.NewAppError("ServeInterPluginRequest", "app.plugin.disabled.app_error", nil, "Plugin environment not found.", http.StatusNotImplemented)
a.Log().Error(err.Error())
w.WriteHeader(err.StatusCode)
appErr := model.NewAppError("ServeInterPluginRequest", "app.plugin.disabled.app_error", nil, "Plugin environment not found.", http.StatusNotImplemented)
a.Log().Error(appErr.Error())
w.WriteHeader(appErr.StatusCode)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(err.ToJSON()))
if _, err := w.Write([]byte(appErr.ToJSON())); err != nil {
mlog.Warn("Error while writing response", mlog.Err(err))
}
return
}
@ -147,17 +151,19 @@ func (ch *Channels) servePluginRequest(w http.ResponseWriter, r *http.Request, h
r.Header.Del("Mattermost-User-Id")
if token != "" {
session, err := New(ServerConnector(ch)).GetSession(token)
session, appErr := New(ServerConnector(ch)).GetSession(token)
csrfCheckPassed := false
if session != nil && err == nil && cookieAuth && r.Method != "GET" {
if session != nil && appErr == nil && cookieAuth && r.Method != "GET" {
sentToken := ""
if r.Header.Get(model.HeaderCsrfToken) == "" {
bodyBytes, _ := io.ReadAll(r.Body)
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
r.ParseForm()
if err := r.ParseForm(); err != nil {
mlog.Warn("Failed to parse form data for plugin request", mlog.Err(err))
}
sentToken = r.FormValue("csrf")
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
} else {
@ -199,7 +205,7 @@ func (ch *Channels) servePluginRequest(w http.ResponseWriter, r *http.Request, h
csrfCheckPassed = true
}
if (session != nil && session.Id != "") && err == nil && csrfCheckPassed {
if (session != nil && session.Id != "") && appErr == nil && csrfCheckPassed {
r.Header.Set("Mattermost-User-Id", session.UserId)
context.SessionId = session.Id