Skip to content

Commit

Permalink
fix: panic while insert record when request uri too long (#325)
Browse files Browse the repository at this point in the history
Signed-off-by: Lex Lim <hyperzlink@outlook.com>
  • Loading branch information
Lex Lim committed Nov 11, 2021
1 parent abba56a commit 64fc810
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion object/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ 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(),
CreatedTime: util.GetCurrentTime(),
ClientIp: ip,
User: "",
Method: ctx.Request.Method,
RequestUri: ctx.Request.RequestURI,
RequestUri: requestUri,
Action: action,
IsTriggered: false,
}
Expand Down
28 changes: 28 additions & 0 deletions util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 64fc810

Please sign in to comment.