From fcaee22ee1080b3324bcf7a6ab4f081a051a0973 Mon Sep 17 00:00:00 2001 From: Travis Ralston <travpc@gmail.com> Date: Sun, 16 Jul 2023 23:23:32 -0600 Subject: [PATCH] Log a redacted URL during request panics --- api/router.go | 3 ++- util/http.go | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/api/router.go b/api/router.go index 8c683f4b..79430516 100644 --- a/api/router.go +++ b/api/router.go @@ -11,6 +11,7 @@ import ( "github.com/sirupsen/logrus" "github.com/turt2live/matrix-media-repo/api/_responses" "github.com/turt2live/matrix-media-repo/api/_routers" + "github.com/turt2live/matrix-media-repo/util" ) func buildPrimaryRouter() *httprouter.Router { @@ -54,7 +55,7 @@ func finishCorsFn(w http.ResponseWriter, r *http.Request) { } func panicFn(w http.ResponseWriter, r *http.Request, i interface{}) { - logrus.Errorf("Panic received on %s %s: %s", r.Method, r.URL.String(), i) + logrus.Errorf("Panic received on %s %s: %s", r.Method, util.GetLogSafeUrl(r), i) if e, ok := i.(error); ok { sentry.CaptureException(e) diff --git a/util/http.go b/util/http.go index 83aef1db..1bfd1a2b 100644 --- a/util/http.go +++ b/util/http.go @@ -2,6 +2,7 @@ package util import ( "net/http" + "net/url" "strings" "github.com/sirupsen/logrus" @@ -37,3 +38,9 @@ func GetLogSafeQueryString(r *http.Request) string { return qs.Encode() } + +func GetLogSafeUrl(r *http.Request) string { + copyUrl, _ := url.ParseRequestURI(r.URL.String()) + copyUrl.RawQuery = GetLogSafeQueryString(r) + return copyUrl.String() +} -- GitLab