From 4fa6a1e4eee9101247794bf45af12010083b8ffb Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Fri, 6 Oct 2023 08:43:19 +0300 Subject: [PATCH] Merging to release-4-lts: [TT-7127] Fix goplugin invalid memory address or nil pointer dereference (#5589) (#5594) [TT-7127] Fix goplugin invalid memory address or nil pointer dereference (#5589) https://tyktech.atlassian.net/browse/TT-7127 Closes #4198 #4197 Signed-off-by: Chenyang Yan Co-authored-by: Chenyang Yan [TT-7127]: https://tyktech.atlassian.net/browse/TT-7127?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Tit Petric Co-authored-by: Tit Petric --- gateway/mw_go_plugin.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gateway/mw_go_plugin.go b/gateway/mw_go_plugin.go index 6a97a4be36a..16e45e83351 100644 --- a/gateway/mw_go_plugin.go +++ b/gateway/mw_go_plugin.go @@ -156,13 +156,16 @@ func (m *GoPluginMiddleware) goPluginFromRequest(r *http.Request) (*GoPluginMidd return perPathPerMethodGoPlugin.(*GoPluginMiddleware), found } func (m *GoPluginMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, conf interface{}) (err error, respCode int) { - // if a Go plugin is found for this path, override the base handler and logger: logger := m.logger handler := m.handler + successHandler := m.successHandler + if !m.APILevel { + // if a Go plugin is found for this path, override the base handler and logger if pluginMw, found := m.goPluginFromRequest(r); found { logger = pluginMw.logger handler = pluginMw.handler + successHandler = &SuccessHandler{BaseMiddleware: m.BaseMiddleware} } else { return nil, http.StatusOK // next middleware } @@ -226,7 +229,7 @@ func (m *GoPluginMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Reque m.logger.WithError(err).Error("Failed to process request with Go-plugin middleware func") default: // record 2XX to analytics - m.successHandler.RecordHit(r, Latency{Total: int64(ms)}, rw.statusCodeSent, rw.getHttpResponse(r)) + successHandler.RecordHit(r, Latency{Total: int64(ms)}, rw.statusCodeSent, rw.getHttpResponse(r)) // no need to continue passing this request down to reverse proxy respCode = mwStatusRespond