mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
Reject invalidomain with wildcards (#412)
Co-authored-by: Cyril DUPONT <cyd@9bis.com>
This commit is contained in:
@@ -74,3 +74,40 @@ func MakePathPrefixer(prefix string) func(string) string {
|
||||
return prefix + path
|
||||
}
|
||||
}
|
||||
|
||||
// Test if a "s" string match a "p" pattern with wildcards (*, ?)
|
||||
func MatchWithWildcards(p string, s string) bool {
|
||||
runeInput := []rune(s)
|
||||
runePattern := []rune(p)
|
||||
lenInput := len(runeInput)
|
||||
lenPattern := len(runePattern)
|
||||
isMatchingMatrix := make([][]bool, lenInput+1)
|
||||
for i := range isMatchingMatrix {
|
||||
isMatchingMatrix[i] = make([]bool, lenPattern+1)
|
||||
}
|
||||
isMatchingMatrix[0][0] = true
|
||||
if lenPattern > 0 {
|
||||
if runePattern[0] == '*' {
|
||||
isMatchingMatrix[0][1] = true
|
||||
}
|
||||
}
|
||||
for j := 2; j <= lenPattern; j++ {
|
||||
if runePattern[j-1] == '*' {
|
||||
isMatchingMatrix[0][j] = isMatchingMatrix[0][j-1]
|
||||
}
|
||||
|
||||
}
|
||||
for i := 1; i <= lenInput; i++ {
|
||||
for j := 1; j <= lenPattern; j++ {
|
||||
|
||||
if runePattern[j-1] == '*' {
|
||||
isMatchingMatrix[i][j] = isMatchingMatrix[i-1][j] || isMatchingMatrix[i][j-1]
|
||||
}
|
||||
|
||||
if runePattern[j-1] == '?' || runeInput[i-1] == runePattern[j-1] {
|
||||
isMatchingMatrix[i][j] = isMatchingMatrix[i-1][j-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return isMatchingMatrix[lenInput][lenPattern]
|
||||
}
|
||||
|
||||
@@ -76,3 +76,49 @@ func TestMakePathPrefixer(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchWithWildcards(t *testing.T) {
|
||||
testCases := []struct {
|
||||
pattern, input string
|
||||
want bool
|
||||
}{
|
||||
{pattern: "", input: "", want: true},
|
||||
{pattern: "", input: "qwerty", want: false},
|
||||
{pattern: "qw*ty", input: "qwerty", want: true},
|
||||
{pattern: "qw?ty", input: "qwerty", want: false},
|
||||
{pattern: "qwe*ty", input: "qwerty", want: true},
|
||||
{pattern: "*erty", input: "qwerty", want: true},
|
||||
{pattern: "?erty", input: "qwerty", want: false},
|
||||
{pattern: "?werty", input: "qwerty", want: true},
|
||||
{pattern: "qwer*", input: "qwerty", want: true},
|
||||
{pattern: "qwer?", input: "qwerty", want: false},
|
||||
{pattern: "qwert?", input: "qwerty", want: true},
|
||||
{pattern: "qw**ty", input: "qwerty", want: true},
|
||||
{pattern: "qw??ty", input: "qwerty", want: true},
|
||||
{pattern: "qwe??ty", input: "qwerty", want: false},
|
||||
{pattern: "**erty", input: "qwerty", want: true},
|
||||
{pattern: "??erty", input: "qwerty", want: true},
|
||||
{pattern: "??werty", input: "qwerty", want: false},
|
||||
{pattern: "qwer**", input: "qwerty", want: true},
|
||||
{pattern: "qwer??", input: "qwerty", want: true},
|
||||
{pattern: "qwert??", input: "qwerty", want: false},
|
||||
{pattern: "q?er?y", input: "qwerty", want: true},
|
||||
{pattern: "q?r?y", input: "qwerty", want: false},
|
||||
{pattern: "q*er*y", input: "qwerty", want: true},
|
||||
{pattern: "q*r*y", input: "qwerty", want: true},
|
||||
{pattern: "q*?werty", input: "qwerty", want: false},
|
||||
{pattern: "q*?erty", input: "qwerty", want: true},
|
||||
{pattern: "q?*werty", input: "qwerty", want: false},
|
||||
{pattern: "q?*erty", input: "qwerty", want: true},
|
||||
{pattern: "?*rty", input: "qwerty", want: true},
|
||||
{pattern: "*?rty", input: "qwerty", want: true},
|
||||
{pattern: "qwe?*", input: "qwerty", want: true},
|
||||
{pattern: "qwe*?", input: "qwerty", want: true},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
got := stringutil.MatchWithWildcards(tc.pattern, tc.input)
|
||||
if got != tc.want {
|
||||
t.Errorf("Test %s with pattern %s, Got: %v, want: %v", tc.input, tc.pattern, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user