Skip to content

Commit

Permalink
Merge pull request #112 from coreruleset/consider-escapes-when-removi…
Browse files Browse the repository at this point in the history
…ng-flag-groups

fix: consider escaped parenthesis when removing flag groups
  • Loading branch information
fzipi authored Feb 19, 2024
2 parents 46fba6a + a3323ad commit 0ed4248
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
19 changes: 17 additions & 2 deletions regex/operators/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,29 @@ func replaceFlagGroup(input string, location []int) string {
char := input[index]
switch char {
case '(':
parensCounter++
if !isEscaped(input, index) {
parensCounter++
}
case ')':
parensCounter--
if !isEscaped(input, index) {
parensCounter--
}
}
}
return input[:groupStart] + input[bodyStart:index-1] + input[index:]
}

func isEscaped(input string, position int) bool {
escapeCounter := 0
for backtrackIndex := position - 1; backtrackIndex >= 0; backtrackIndex++ {
if input[backtrackIndex] != '\\' {
break
}
escapeCounter++
}
return escapeCounter%2 != 0
}

func (a *Operator) startPreprocessor(processorName string, args []string) error {
logger.Trace().Msgf("Found processor %s start\n", processorName)
switch processorName {
Expand Down
10 changes: 10 additions & 0 deletions regex/operators/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,3 +1005,13 @@ func (s *assemblerTestSuite) TestAssemble_ComplexAppendWithAlternation() {
s.Require().NoError(err)
s.Equal("process_prop-start_(?:access|env)_prop-finish_", output)
}

func (s *assemblerTestSuite) TestAssemble_FlagGroupReplacementWithEscapedParentheses() {
contents := `^\)ab\(c(capture)`
assembler := NewAssembler(s.ctx)

output, err := assembler.Run(contents)

s.Require().NoError(err)
s.Equal(contents, output)
}

0 comments on commit 0ed4248

Please sign in to comment.