From 64fc810359d0ba749ae966158438e8e2e7b0f58a Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Fri, 12 Nov 2021 00:01:37 +0800 Subject: [PATCH] fix: panic while insert record when request uri too long (#325) Signed-off-by: Lex Lim --- object/record.go | 6 +++++- util/path.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/object/record.go b/object/record.go index 2c54897d..803a5aca 100644 --- a/object/record.go +++ b/object/record.go @@ -52,6 +52,10 @@ type Record struct { func NewRecord(ctx *context.Context) *Record { ip := strings.Replace(util.GetIPFromRequest(ctx.Request), ": ", "", -1) action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1) + requestUri := util.FilterQuery(ctx.Request.RequestURI, []string{ "accessToken" }) + if len(requestUri) > 1000 { + requestUri = requestUri[0:1000] + } record := Record{ Name: util.GenerateId(), @@ -59,7 +63,7 @@ func NewRecord(ctx *context.Context) *Record { ClientIp: ip, User: "", Method: ctx.Request.Method, - RequestUri: ctx.Request.RequestURI, + RequestUri: requestUri, Action: action, IsTriggered: false, } diff --git a/util/path.go b/util/path.go index 25556777..550b3daa 100644 --- a/util/path.go +++ b/util/path.go @@ -42,3 +42,31 @@ func GetUrlHost(urlString string) string { u, _ := url.Parse(urlString) return fmt.Sprintf("%s://%s", u.Scheme, u.Host) } + +func FilterQuery(urlString string, blackList []string) string { + urlData, err := url.Parse(urlString) + if err != nil { + return urlString + } + + queries := urlData.Query() + retQuery := make(url.Values) + inBlackList := false + for key, value := range queries { + inBlackList = false + for _, blackListItem := range blackList { + if blackListItem == key { + inBlackList = true + break + } + } + if !inBlackList { + retQuery[key] = value + } + } + if len(retQuery) > 0 { + return urlData.Path + "?" + strings.ReplaceAll(retQuery.Encode(), "%2F", "/") + } else { + return urlData.Path + } +} \ No newline at end of file