From 53a0f8da276f8f8de23f3557d7ce69eff6e7b58a Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Thu, 28 Sep 2023 19:28:40 +0300 Subject: [PATCH] Add limiting ability to log fields (#24251) --- server/channels/app/platform/log.go | 3 ++- server/go.mod | 2 +- server/go.sum | 4 ++-- server/platform/services/imageproxy/local.go | 5 ++--- server/public/model/config.go | 5 +++++ 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/server/channels/app/platform/log.go b/server/channels/app/platform/log.go index 5b1b7d28d6..8021267d7b 100644 --- a/server/channels/app/platform/log.go +++ b/server/channels/app/platform/log.go @@ -13,6 +13,7 @@ import ( "os" "time" + "github.com/mattermost/logr/v2" "github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/shared/mlog" "github.com/mattermost/mattermost/server/v8/config" @@ -31,7 +32,7 @@ func (ps *PlatformService) initLogging() error { // create the app logger if needed if ps.logger == nil { var err error - ps.logger, err = mlog.NewLogger() + ps.logger, err = mlog.NewLogger(logr.MaxFieldLen(*ps.Config().LogSettings.MaxFieldSize)) if err != nil { return err } diff --git a/server/go.mod b/server/go.mod index af0e15b9de..5b23edda01 100644 --- a/server/go.mod +++ b/server/go.mod @@ -37,7 +37,7 @@ require ( github.com/lib/pq v1.10.9 github.com/mattermost/go-i18n v1.11.1-0.20211013152124-5c415071e404 github.com/mattermost/gziphandler v0.0.1 - github.com/mattermost/logr/v2 v2.0.16 + github.com/mattermost/logr/v2 v2.0.18 github.com/mattermost/mattermost/server/public v0.0.9 github.com/mattermost/morph v1.0.5-0.20230511171014-e76e25978d56 github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0 diff --git a/server/go.sum b/server/go.sum index d6b2621b84..908ad8ac08 100644 --- a/server/go.sum +++ b/server/go.sum @@ -443,8 +443,8 @@ github.com/mattermost/gziphandler v0.0.1 h1:uXHcXF5agnQ6bXabvpiwwwZOlCYoa7mKHH0l github.com/mattermost/gziphandler v0.0.1/go.mod h1:CvvZR7sXqhj81V2swXuQY7T04Ccc89u7W7pHNPKev8g= github.com/mattermost/ldap v0.0.0-20201202150706-ee0e6284187d h1:/RJ/UV7M5c7L2TQ0KNm4yZxxFvC1nvRz/gY/Daa35aI= github.com/mattermost/ldap v0.0.0-20201202150706-ee0e6284187d/go.mod h1:HLbgMEI5K131jpxGazJ97AxfPDt31osq36YS1oxFQPQ= -github.com/mattermost/logr/v2 v2.0.16 h1:jnePX4cPskC3WDFvUardh/xZfxNdsFXbEERJQ1kUEDE= -github.com/mattermost/logr/v2 v2.0.16/go.mod h1:1dm/YhTpozsqANXxo5Pi5zYLBsal2xY0pX+JZNbzYJY= +github.com/mattermost/logr/v2 v2.0.18 h1:qiznuwwKckZJoGtBYc4Y9FAY97/oQwV1Pq9oO5qP5nk= +github.com/mattermost/logr/v2 v2.0.18/go.mod h1:1dm/YhTpozsqANXxo5Pi5zYLBsal2xY0pX+JZNbzYJY= github.com/mattermost/mattermost/server/public v0.0.9 h1:Qsktgxx5dc8xVAUHP5MbSLi6Cf82iB/83r6S9bluHto= github.com/mattermost/mattermost/server/public v0.0.9/go.mod h1:sgXQrYzs+IJy51mB8E8OBljagk2u3YwQRoYlBH5goiw= github.com/mattermost/morph v1.0.5-0.20230511171014-e76e25978d56 h1:SjFYbWvmuf73d/KaYlnHosPpB3/k38ebr1WWw9X5XKc= diff --git a/server/platform/services/imageproxy/local.go b/server/platform/services/imageproxy/local.go index d1ac69c8c1..dba0d80f2d 100644 --- a/server/platform/services/imageproxy/local.go +++ b/server/platform/services/imageproxy/local.go @@ -152,13 +152,12 @@ func (backend *LocalBackend) ServeImage(w http.ResponseWriter, req *http.Request resp, err := backend.client.Do(actualReq) if err != nil { - msg := fmt.Sprintf("error fetching remote image: %v", err) - mlog.Warn(msg) + mlog.Warn("error fetching remote image", mlog.Err(err)) statusCode := http.StatusInternalServerError if e, ok := err.(net.Error); ok && e.Timeout() { statusCode = http.StatusGatewayTimeout } - http.Error(w, msg, statusCode) + http.Error(w, fmt.Sprintf("error fetching remote image: %v", err), statusCode) return } // close the original resp.Body, even if we wrap it in a NopCloser below diff --git a/server/public/model/config.go b/server/public/model/config.go index be823fabcf..0a799a356c 100644 --- a/server/public/model/config.go +++ b/server/public/model/config.go @@ -1276,6 +1276,7 @@ type LogSettings struct { EnableSentry *bool `access:"environment_logging,write_restrictable,cloud_restrictable"` // telemetry: none AdvancedLoggingJSON json.RawMessage `access:"environment_logging,write_restrictable,cloud_restrictable"` AdvancedLoggingConfig *string `access:"environment_logging,write_restrictable,cloud_restrictable"` // Deprecated: use `AdvancedLoggingJSON` + MaxFieldSize *int `access:"environment_logging,write_restrictable,cloud_restrictable"` } func NewLogSettings() *LogSettings { @@ -1340,6 +1341,10 @@ func (s *LogSettings) SetDefaults() { if s.AdvancedLoggingConfig == nil { s.AdvancedLoggingConfig = NewString("") } + + if s.MaxFieldSize == nil { + s.MaxFieldSize = NewInt(2048) + } } // GetAdvancedLoggingConfig returns the advanced logging config as a []byte.