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

feat: add validator for IPv4 bind address / host:port #1282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ var (
"ipv4": isIPv4,
"ipv6": isIPv6,
"ip": isIP,
"ipv4_port": isIPv4Port,
"cidrv4": isCIDRv4,
"cidrv6": isCIDRv6,
"cidr": isCIDR,
Expand Down Expand Up @@ -2704,6 +2705,29 @@ func isHostnamePort(fl FieldLevel) bool {
return true
}

// isIPv4Port validates a <ipv4>:<port> combination for fields typically used for socket address.
func isIPv4Port(fl FieldLevel) bool {
val := fl.Field().String()
ip, port, err := net.SplitHostPort(val)
if err != nil {
return false
}
// Port must be a iny <= 65535.
if portNum, err := strconv.ParseInt(
port, 10, 32,
); err != nil || portNum > 65535 || portNum < 1 {
return false
}

// If IP address is specified, it should match a valid IPv4 address
if ip != "" {
// we need to support older Golang versions, so we can not use netip.ParseAddr
parsedIp := net.ParseIP(ip)
return parsedIp != nil && parsedIp.To4() != nil
}
return true
Comment on lines +2711 to +2728
Copy link
Contributor

Choose a reason for hiding this comment

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

More readable and concise implementation:

Suggested change
ip, port, err := net.SplitHostPort(val)
if err != nil {
return false
}
// Port must be a iny <= 65535.
if portNum, err := strconv.ParseInt(
port, 10, 32,
); err != nil || portNum > 65535 || portNum < 1 {
return false
}
// If IP address is specified, it should match a valid IPv4 address
if ip != "" {
// we need to support older Golang versions, so we can not use netip.ParseAddr
parsedIp := net.ParseIP(ip)
return parsedIp != nil && parsedIp.To4() != nil
}
return true
host, port, err := net.SplitHostPort(val)
if err != nil {
return false
}
// Validate port range (1-65535).
if portNum, err := strconv.ParseUInt(port, 10, 32,); err != nil || portNum < 1 || portNum > 65535 {
return false
}
// If no host is specified, return true (valid)
if host == "" {
return true
}
// Parse and validate IPv4 address
ip := net.ParseIP(host)
return ip != nil && ip.To4() != nil

}

// isLowercase is the validation function for validating if the current field's value is a lowercase string.
func isLowercase(fl FieldLevel) bool {
field := fl.Field()
Expand Down
32 changes: 32 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12377,6 +12377,38 @@ func Test_hostnameport_validator(t *testing.T) {
}
}

func Test_ipv4_port_validator(t *testing.T) {
type IPv4Port struct {
BindAddr string `validate:"ipv4_port"`
}

type testInput struct {
data string
expected bool
}
testData := []testInput{
{"192.168.1.1:1234", true},
{":1234", true},
{"localhost:1234", false},
{"aaa.bbb.ccc.ddd:234", false},
{":alpha", false},
{"1.2.3.4", false},
{"2001:db8::1:0.0.0.0:234", false},
{"2001:db8::1:0.0.0.0", false},
{"2001:db8::1:0.0.0.0:", false},
{"2001:db8::1:0.0.0.0:123456", false},
{"2001:db8::1:0.0.0.0:123456:", false},
}
for _, td := range testData {
h := IPv4Port{BindAddr: td.data}
v := New()
err := v.Struct(h)
if td.expected != (err == nil) {
t.Fatalf("Test failed for data: %v Error: %v", td.data, err)
}
}
}

func TestLowercaseValidation(t *testing.T) {
tests := []struct {
param string
Expand Down
Loading