Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update regex engine to support possessive match and lookbehind syntaxes #1147

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions rule/engine_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package rule

import (
"errors"
"hash/crc64"
"strings"

"github.com/pkg/errors"

"github.com/dlclark/regexp2"

Expand All @@ -23,7 +25,17 @@ func (re *regexpMatchingEngine) compile(pattern string) error {
re.table = crc64.MakeTable(polynomial)
}
if checksum := crc64.Checksum([]byte(pattern), re.table); checksum != re.checksum {
compiled, err := compiler.CompileRegex(pattern, '<', '>')
startDelim := byte('<')
endDelim := byte('>')
if strings.Contains(pattern, "(?>") || strings.Contains(pattern, "(?<") {
if strings.ContainsRune(pattern, '{') && strings.ContainsRune(pattern, '}') {
startDelim = byte('{')
endDelim = byte('}')
} else {
return errors.Errorf("attempted to use regex 'possessive match' or regex 'lookbehind' without changing delimiters from '<...>' to '{...}' in: %s", pattern)
}
}
compiled, err := compiler.CompileRegex(pattern, startDelim, endDelim)
if err != nil {
return err
}
Expand Down
45 changes: 45 additions & 0 deletions rule/engine_regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ func TestFindStringSubmatch(t *testing.T) {
want: nil,
wantErr: true,
},
{
name: "bad lookbehind (wrong delimiters)",
args: args{
pattern: `urn:foo:<(?<=foo:)foobar>`,
matchAgainst: "urn:foo:foobar",
},
want: nil,
wantErr: true,
},
{
name: "bad possessive (wrong delimiters)",
args: args{
pattern: `urn:foo:<(?>=foo:)foobar>`,
matchAgainst: "urn:foo:foobar",
},
want: nil,
wantErr: true,
},
{
name: "one group",
args: args{
Expand Down Expand Up @@ -56,6 +74,33 @@ func TestFindStringSubmatch(t *testing.T) {
want: []string{"bar"},
wantErr: false,
},
{
name: "positive lookbehind (?<=foo)bar",
args: args{
pattern: `urn:foo:{(?<=foo:)foobar}`,
matchAgainst: "urn:foo:foobar",
},
want: []string{"foobar"},
wantErr: false,
},
{
name: "negative lookbehind (?<!boo)foobar",
args: args{
pattern: `urn:foo:{(?<!boo:)foobar}`,
matchAgainst: "urn:foo:foobar",
},
want: []string{"foobar"},
wantErr: false,
},
{
name: "negative lookbehind (?<!boo)foobar, not match",
args: args{
pattern: `urn:foo:{(?<!boo:)foobar}`,
matchAgainst: "urn:boo:foobar",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading