mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-10-14 08:13:18 +01:00
fix(webhook/downloadFrom): better default security and DX for allow / deny lists
This commit is contained in:
@@ -12,17 +12,6 @@ import (
|
||||
// ErrFiltered happens if a value is filtered by the [FilterDeadline] function.
|
||||
var ErrFiltered = errors.New("value filtered")
|
||||
|
||||
// RegexpToSlice wraps a single [regexp2.Regexp] into a slice suitable for
|
||||
// [FilterDeadline]. If the regexp pattern is empty, it returns nil (meaning no
|
||||
// filtering).
|
||||
func RegexpToSlice(r *regexp2.Regexp) []*regexp2.Regexp {
|
||||
if r == nil || r.String() == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return []*regexp2.Regexp{r}
|
||||
}
|
||||
|
||||
// FilterDeadline checks if the given value is allowed and not denied according
|
||||
// to regex patterns. The allowed list uses OR semantics (value must match at
|
||||
// least one pattern). The denied list uses OR semantics (value is denied if it
|
||||
|
||||
@@ -115,45 +115,3 @@ func TestFilterDeadline(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegexpToSlice(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
scenario string
|
||||
input *regexp2.Regexp
|
||||
expectNil bool
|
||||
expectLen int
|
||||
}{
|
||||
{
|
||||
scenario: "nil regexp",
|
||||
input: nil,
|
||||
expectNil: true,
|
||||
},
|
||||
{
|
||||
scenario: "empty regexp",
|
||||
input: regexp2.MustCompile("", 0),
|
||||
expectNil: true,
|
||||
},
|
||||
{
|
||||
scenario: "non-empty regexp",
|
||||
input: regexp2.MustCompile("^file:.*", 0),
|
||||
expectNil: false,
|
||||
expectLen: 1,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.scenario, func(t *testing.T) {
|
||||
result := RegexpToSlice(tc.input)
|
||||
|
||||
if tc.expectNil && result != nil {
|
||||
t.Fatalf("expected nil but got: %v", result)
|
||||
}
|
||||
|
||||
if !tc.expectNil && result == nil {
|
||||
t.Fatal("expected non-nil but got nil")
|
||||
}
|
||||
|
||||
if !tc.expectNil && len(result) != tc.expectLen {
|
||||
t.Fatalf("expected length %d but got %d", tc.expectLen, len(result))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user