Skip to content

Commit

Permalink
fix: fix backtracking in IsEscaped()
Browse files Browse the repository at this point in the history
  • Loading branch information
theseion committed Jul 15, 2024
1 parent acf3379 commit d388fa1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package utils

func IsEscaped(input string, position int) bool {
escapeCounter := 0
for backtrackIndex := position - 1; backtrackIndex >= 0; backtrackIndex++ {
for backtrackIndex := position - 1; backtrackIndex >= 0; backtrackIndex-- {
if input[backtrackIndex] != '\\' {
break
}
Expand Down
42 changes: 42 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 OWASP ModSecurity Core Rule Set Project
// SPDX-License-Identifier: Apache-2.0

package utils

import (
"testing"

"github.com/stretchr/testify/suite"
)

type utilsTestSuite struct {
suite.Suite
}

func TestRunUtilsTestSuite(t *testing.T) {
suite.Run(t, new(utilsTestSuite))
}

func (s *utilsTestSuite) TestIsEscaped() {
s.True(IsEscaped(`abc\(de`, 4))
s.True(IsEscaped(`\(abc`, 1))
s.True(IsEscaped(`abc\(`, 4))
}

func (s *utilsTestSuite) TestIsEscaped_Backslashes() {
s.True(IsEscaped(`abc\\de`, 4))
s.True(IsEscaped(`\\abc`, 1))
s.True(IsEscaped(`abc\\`, 4))
}

func (s *utilsTestSuite) TestIsEscaped_Not() {
s.False(IsEscaped(`abc\\(de`, 5))
s.False(IsEscaped(`\\(abc`, 2))
s.False(IsEscaped(`abc\\(`, 5))
}

func (s *utilsTestSuite) TestIsEscaped_Not_Backslashes() {
s.False(IsEscaped(`abc\\\de`, 5))
s.False(IsEscaped(`\\\abc`, 2))
s.False(IsEscaped(`abc\\\`, 5))
}

0 comments on commit d388fa1

Please sign in to comment.