Skip to content

Commit

Permalink
Extend ID Regex to support escaped quotes (#410)
Browse files Browse the repository at this point in the history
* extends regex for json logs
* adds regex description
  • Loading branch information
M4tteoP authored Dec 10, 2024
1 parent 6c1b1cb commit 603891e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 9 additions & 1 deletion waflog/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions waflog/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
}

0 comments on commit 603891e

Please sign in to comment.