From 603891e79dd9becb333d0f3b8e6e12cb8af52a06 Mon Sep 17 00:00:00 2001 From: Matteo Pace Date: Tue, 10 Dec 2024 07:25:52 +0100 Subject: [PATCH] Extend ID Regex to support escaped quotes (#410) * extends regex for json logs * adds regex description --- waflog/read.go | 10 +++++++++- waflog/read_test.go | 7 ++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/waflog/read.go b/waflog/read.go index 2ce255d..dc07569 100644 --- a/waflog/read.go +++ b/waflog/read.go @@ -28,7 +28,15 @@ func (ll *FTWLogLines) TriggeredRules() []uint { ll.triggeredRulesInitialized = true lines := ll.getMarkedLines() - regex := regexp.MustCompile(`\[id "(\d+)"\]|"id":\s*"?(\d+)"?`) + + // This regex provides flexibility in parsing how the rule ID is logged. + // `\[id \\?"(\d+)\\?"\]` supports: + // - [id "999999"] + // - [id \"999999\"] (escaped quotes) + // `"id":\s*"?(\d+)"?` supports: + // - ["id":"999999"] + // - {"id":4} + regex := regexp.MustCompile(`\[id \\?"(\d+)\\?"\]|"id":\s*"?(\d+)"?`) for _, line := range lines { log.Trace().Msgf("ftw/waflog: Looking for any rule in '%s'", line) match := regex.FindAllSubmatch(line, -1) diff --git a/waflog/read_test.go b/waflog/read_test.go index 4da767c..4c86e6f 100644 --- a/waflog/read_test.go +++ b/waflog/read_test.go @@ -371,11 +371,11 @@ func (s *readTestSuite) TestFindAllIdsInLogs() { markerLine := "X-cRs-TeSt: " + stageID logLines := fmt.Sprint("\n", markerLine, `[id "1"] something else [id "2"]`, - `"id": 3, something else {"id":4}`+"\n", + `"id": 3, something else {"id":4},`, + `something else [id \"5\"]`+"\n", "\n", markerLine) filename, err := utils.CreateTempFileWithContent(logLines, "test-errorlog-") s.Require().NoError(err) - cfg.LogFile = filename log, err := os.Open(filename) s.Require().NoError(err) @@ -388,9 +388,10 @@ func (s *readTestSuite) TestFindAllIdsInLogs() { ll.WithEndMarker([]byte(markerLine)) foundRuleIds := ll.TriggeredRules() - s.Len(foundRuleIds, 4) + s.Len(foundRuleIds, 5) s.Contains(foundRuleIds, uint(1)) s.Contains(foundRuleIds, uint(2)) s.Contains(foundRuleIds, uint(3)) s.Contains(foundRuleIds, uint(4)) + s.Contains(foundRuleIds, uint(5)) }