added superuser ips whitelist
This commit is contained in:
@@ -31,7 +31,7 @@ func UploadedFileSize(maxBytes int64) validation.RuleFunc {
|
||||
"validation_file_size_limit",
|
||||
"Failed to upload {{.file}} - the maximum allowed file size is {{.maxSize}} bytes.",
|
||||
).SetParams(map[string]any{
|
||||
"file": v.OriginalName,
|
||||
"file": cutStr(v.OriginalName, 300),
|
||||
"maxSize": maxBytes,
|
||||
})
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func UploadedFileMimeType(validTypes []string) validation.RuleFunc {
|
||||
|
||||
baseErr := validation.NewError(
|
||||
"validation_invalid_mime_type",
|
||||
fmt.Sprintf("Failed to upload %q due to unsupported file type.", v.OriginalName),
|
||||
fmt.Sprintf("Failed to upload %q due to unsupported file type.", cutStr(v.OriginalName, 300)),
|
||||
)
|
||||
|
||||
if len(validTypes) == 0 {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package validators
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"regexp"
|
||||
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
@@ -27,3 +28,30 @@ func IsRegex(value any) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IPOrSubnet checks whether the validated value is an individual
|
||||
// IPv4/IPv6 or CIDR subnet.
|
||||
func IPOrSubnet(value any) error {
|
||||
v, ok := value.(string)
|
||||
if !ok {
|
||||
return ErrUnsupportedValueType
|
||||
}
|
||||
|
||||
if v == "" {
|
||||
return nil // nothing to check
|
||||
}
|
||||
|
||||
// subnet
|
||||
_, err := netip.ParsePrefix(v)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// individual IP
|
||||
_, err = netip.ParseAddr(v)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return validation.NewError("validation_invlaid_ip_or_subnet", "invalid IP or CIDR subnet")
|
||||
}
|
||||
|
||||
@@ -31,3 +31,32 @@ func TestIsRegex(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPOrSubnet(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
scenarios := []struct {
|
||||
val string
|
||||
expectError bool
|
||||
}{
|
||||
{"", false},
|
||||
{`invalid`, true},
|
||||
{`127.0`, true}, // incomplete
|
||||
{`127.0.0.1`, false},
|
||||
{`::1`, false},
|
||||
{`0000:0000:0000:0000:0000:0000:0000:0001`, false},
|
||||
{`127.0.0.1/24`, false},
|
||||
{`::/128`, false},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%#v", i, s.val), func(t *testing.T) {
|
||||
err := validators.IPOrSubnet(s.val)
|
||||
|
||||
hasErr := err != nil
|
||||
if hasErr != s.expectError {
|
||||
t.Fatalf("Expected hasErr to be %v, got %v (%v)", s.expectError, hasErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,3 +38,10 @@ func JoinValidationErrors(errA, errB error) error {
|
||||
|
||||
return errors.Join(errA, errB)
|
||||
}
|
||||
|
||||
func cutStr(str string, max int) string {
|
||||
if len(str) > max {
|
||||
return str[:max] + "..."
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user