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

Artifact tag matcher: Curb complexity when parsing regexps from user input #3836

Merged
merged 2 commits into from
Jul 10, 2024
Merged
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
3 changes: 3 additions & 0 deletions internal/providers/artifact/versionsfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func BuildFilter(tags []string, tagRegex string) (*filter, error) {

// no tags specified, but a regex was, compile it
if tagRegex != "" {
if len(tagRegex) > 512 {
return nil, fmt.Errorf("tag regular expressions are limited to 512 characters")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): it could be useful to define this error statically so that upper layers can react accordingly, e.g. by returning a 400 or 409 in case this codepath is reachable from a handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, makes sense. I'll surface it in a follow-up, thanks!

}
re, err := regexp.Compile(tagRegex)
if err != nil {
return nil, fmt.Errorf("error compiling tag regex: %w", err)
Expand Down
75 changes: 75 additions & 0 deletions internal/providers/artifact/versionsfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,89 @@
package artifact

import (
"regexp"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

provifv1 "github.com/stacklok/minder/pkg/providers/v1"
)

func TestBuildFilter(t *testing.T) {
t.Parallel()
simpleRegex := `^simpleregex$`
compiledSimpleRegex := regexp.MustCompile(simpleRegex)
for _, tc := range []struct {
name string
tags []string
regex string
expected *filter
mustErr bool
}{
{
name: "tags",
tags: []string{"hello", "bye"},
mustErr: false,
expected: &filter{
tagMatcher: &tagListMatcher{tags: []string{"hello", "bye"}},
retentionPeriod: time.Time{},
},
},
{
name: "empty-tag",
tags: []string{"hello", ""},
mustErr: true,
},
{
name: "regex",
tags: []string{},
regex: simpleRegex,
mustErr: false,
expected: &filter{
tagMatcher: &tagRegexMatcher{
re: compiledSimpleRegex,
},
},
},
{
name: "invalidregexp",
tags: []string{},
regex: `$(invalid^`,
mustErr: true,
},
{
name: "valid-long-regexp",
tags: []string{},
regex: `^` + strings.Repeat("A", 1000) + `$`,
mustErr: true,
},
{
name: "no-tags",
tags: []string{},
mustErr: false,
expected: &filter{
tagMatcher: &tagAllMatcher{},
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
f, err := BuildFilter(tc.tags, tc.regex)
if tc.mustErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.NotNil(t, f.tagMatcher)
require.Equal(t, tc.expected.tagMatcher, f.tagMatcher)
})
}
}

func Test_filter_IsSkippable(t *testing.T) {
t.Parallel()

Expand Down