Skip to content

Commit

Permalink
check if port is between 0 and 65535 (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwennrich authored Jan 12, 2022
1 parent f7effa3 commit 38aa3f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/v1/clusterwidenetworkpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func validatePorts(ports []networking.NetworkPolicyPort) *multierror.Error {
errors = multierror.Append(errors, fmt.Errorf("only int ports are supported, but %v given", p.Port))
}

if p.Port != nil && (p.Port.IntValue() > 65535 || p.Port.IntValue() <= 0) {
errors = multierror.Append(errors, fmt.Errorf("only ports between 0 and 65535 are allowed, but %v given", p.Port))
}

if p.Protocol != nil {
proto := *p.Protocol
if proto != corev1.ProtocolUDP && proto != corev1.ProtocolTCP {
Expand Down
20 changes: 20 additions & 0 deletions api/v1/clusterwidenetworkpolicy_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestPolicySpec_Validate(t *testing.T) {
port1 := intstr.FromInt(8080)
port2 := intstr.FromInt(8081)
invalid := intstr.FromString("invalid")
invalidPort := intstr.FromInt(99999)
tests := []struct {
name string
Ingress []IngressRule
Expand Down Expand Up @@ -91,6 +92,25 @@ func TestPolicySpec_Validate(t *testing.T) {
},
wantErr: true,
},
{
name: "invalid port",
Ingress: []IngressRule{
{
From: []networking.IPBlock{
{
CIDR: "1.1.0.0/24",
},
},
Ports: []networking.NetworkPolicyPort{
{
Protocol: &tcp,
Port: &invalidPort,
},
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
Expand Down

0 comments on commit 38aa3f7

Please sign in to comment.