Skip to content

Commit ba1e095

Browse files
committed
feat(chore): add pattern validation logic
1 parent a954ccd commit ba1e095

5 files changed

Lines changed: 137 additions & 6 deletions

File tree

pkg/engine/engine.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func envelopeErr(req secrets.Request, err error) secrets.Envelope {
6666
type pluginRegistration struct {
6767
name string
6868
//version string
69-
pattern string
69+
pattern secrets.Pattern
7070
provider secrets.Resolver
7171
}
7272

@@ -76,10 +76,14 @@ func (e *Engine) Register(name, _, pattern string, provider secrets.Resolver) er
7676
}) {
7777
return fmt.Errorf("provider name %q already registered", name)
7878
}
79+
p, err := secrets.ParsePattern(pattern)
80+
if err != nil {
81+
return err
82+
}
7983

8084
e.plugins = append(e.plugins, pluginRegistration{
8185
name: name,
82-
pattern: pattern,
86+
pattern: p,
8387
provider: provider,
8488
})
8589

pkg/secrets/identifiers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ func isValidRune(c rune) bool {
7676
// - "*" matches a single component
7777
// - "**" matches zero or more components
7878
// - "/" is the separator
79-
func (id ID) Match(pattern string) bool {
79+
func (id ID) Match(pattern Pattern) bool {
8080
pathParts := split(string(id))
81-
patternParts := split(pattern)
81+
patternParts := split(string(pattern))
8282

8383
return match(patternParts, pathParts)
8484
}

pkg/secrets/identifiers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestParseID(t *testing.T) {
3737

3838
func TestMatch(t *testing.T) {
3939
tests := []struct {
40-
pattern string
40+
pattern Pattern
4141
matches []string
4242
noMatches []string
4343
}{
@@ -67,7 +67,7 @@ func TestMatch(t *testing.T) {
6767
},
6868
}
6969
for _, tc := range tests {
70-
t.Run(tc.pattern, func(t *testing.T) {
70+
t.Run(string(tc.pattern), func(t *testing.T) {
7171
for _, m := range tc.matches {
7272
id, err := ParseID(m)
7373
assert.NoError(t, err)

pkg/secrets/pattern.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package secrets
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var (
9+
ErrInvalidPattern = errors.New("invalid pattern")
10+
)
11+
12+
type Pattern string
13+
14+
func ParsePattern(pattern string) (Pattern, error) {
15+
p := Pattern(pattern)
16+
if err := p.Valid(); err != nil {
17+
return "", fmt.Errorf("parse pattern: %w", err)
18+
}
19+
return p, nil
20+
}
21+
22+
func (p Pattern) Valid() error {
23+
if !validPattern(string(p)) {
24+
return ErrInvalidPattern
25+
}
26+
return nil
27+
}
28+
29+
func (p Pattern) Match(id ID) bool {
30+
pathParts := split(string(id))
31+
patternParts := split(string(p))
32+
33+
return match(patternParts, pathParts)
34+
}
35+
36+
// validPattern checks if a pattern is valid without using regexp or unicode.
37+
// Rules:
38+
// - Components separated by '/'
39+
// - Each component is non-empty
40+
// - Only characters A-Z, a-z, 0-9, '.', '-', '_' or '*'
41+
// - No leading, trailing, or double slashes
42+
// - '*' can be used in two ways: '*' matches a single component, '**' matches zero or more components
43+
func validPattern(s string) bool {
44+
if len(s) == 0 {
45+
return false
46+
}
47+
48+
componentLen := 0
49+
wildcardLen := 0
50+
for _, r := range s {
51+
switch {
52+
case r == '/':
53+
if componentLen == 0 {
54+
// Empty component (leading, trailing, or double slash)
55+
return false
56+
}
57+
if wildcardLen > 2 {
58+
// No more than two wildcards per component
59+
return false
60+
}
61+
if wildcardLen > 0 && wildcardLen != componentLen {
62+
// Wildcard can't be mixed with other characters in the same component
63+
return false
64+
}
65+
componentLen = 0
66+
wildcardLen = 0
67+
case isValidPatternRune(r):
68+
componentLen++
69+
if r == '*' {
70+
wildcardLen++
71+
}
72+
default:
73+
return false
74+
}
75+
}
76+
77+
// Final component must not be empty
78+
return componentLen > 0
79+
}
80+
81+
func isValidPatternRune(c rune) bool {
82+
return (c >= 'A' && c <= 'Z') ||
83+
(c >= 'a' && c <= 'z') ||
84+
(c >= '0' && c <= '9') ||
85+
c == '.' || c == '-' || c == '_' || c == '*'
86+
}

pkg/secrets/pattern_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package secrets
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestParsePattern(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input string
13+
outErr bool
14+
}{
15+
{"valid pattern with single component", "foo", false},
16+
{"valid pattern with multiple components", "foo/bar/baz", false},
17+
{"valid pattern only with asterisk", "*", false},
18+
{"valid pattern only with double asterisk", "**", false},
19+
{"valid pattern starting with asterisk", "*/bar", false},
20+
{"valid pattern ending with asterisk", "foo/*/baz", false},
21+
{"valid pattern starting with double asterisk", "**/bar", false},
22+
{"valid pattern ending with double asterisk", "foo/**/baz", false},
23+
{"valid pattern with mix of components and wildcards", "foo/*/baz/**/*", false},
24+
{"invalid pattern with leading slash", "/foo/bar", true},
25+
{"invalid pattern with trailing slash", "foo/bar/", true},
26+
{"invalid pattern with empty component", "foo//bar", true},
27+
{"invalid empty pattern", "", true},
28+
{"invalid pattern only with slash", "/", true},
29+
{"invalid pattern with mix of asterisks and allowed characters", "foo/*a*/baz", true},
30+
}
31+
for _, tc := range tests {
32+
t.Run(tc.name, func(t *testing.T) {
33+
_, err := ParsePattern(tc.input)
34+
if tc.outErr {
35+
assert.Error(t, err)
36+
} else {
37+
assert.NoError(t, err)
38+
}
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)