From d7c20a36f36a5f498d8e50a46ca14545389b7f51 Mon Sep 17 00:00:00 2001 From: Michael Lavers Date: Mon, 1 Nov 2021 15:11:21 -0700 Subject: [PATCH] Add Regex Length Checks To Avoid Panics (#93) --- lambda/logserver/logserver.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lambda/logserver/logserver.go b/lambda/logserver/logserver.go index bea7b6d..cc2ae92 100644 --- a/lambda/logserver/logserver.go +++ b/lambda/logserver/logserver.go @@ -99,7 +99,7 @@ func formatReport(metrics map[string]interface{}) string { return ret } -var reportStringRegExp, _ = regexp.Compile("RequestId: ([a-zA-Z0-9-]+)(.*)") +var reportStringRegExp, _ = regexp.Compile("RequestId: ([a-fA-F0-9-]+)(.*)") func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) { defer util.Close(req.Body) @@ -126,7 +126,10 @@ func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) { ls.lastRequestId = event.Record.(map[string]interface{})["requestId"].(string) case string: recordString := event.Record.(string) - ls.lastRequestId = reportStringRegExp.FindStringSubmatch(recordString)[1] + results := reportStringRegExp.FindStringSubmatch(recordString) + if len(results) > 1 { + ls.lastRequestId = results[1] + } } ls.lastRequestIdLock.Unlock() case "platform.report": @@ -141,8 +144,14 @@ func (ls *LogServer) handler(res http.ResponseWriter, req *http.Request) { case string: recordString := event.Record.(string) results := reportStringRegExp.FindStringSubmatch(recordString) - requestId = results[1] - metricString = results[2] + if len(results) > 1 { + requestId = results[1] + if len(results) > 2 { + metricString = results[2] + } + } else { + util.Debugf("Unknown platform log: %s", recordString) + } } reportStr := fmt.Sprintf(