Skip to content

Commit

Permalink
Validate CIDR on firewall_rule_create.go
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Apr 18, 2024
1 parent fe9ecdf commit a06b789
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cmd/firewall/firewall_rule_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package firewall

import (
"fmt"
"net"
"strings"

"github.com/civo/civogo"
Expand Down Expand Up @@ -40,6 +41,12 @@ var firewallRuleCreateCmd = &cobra.Command{
os.Exit(1)
}

// Validate CIDR input
if err := validateCIDRs(cidr); err != nil {
utility.Error(err.Error())
os.Exit(1)
}

newRuleConfig := &civogo.FirewallRuleConfig{
FirewallID: firewall.ID,
Protocol: protocol,
Expand Down Expand Up @@ -98,3 +105,16 @@ var firewallRuleCreateCmd = &cobra.Command{
}
},
}

// validateCIDRs checks if each CIDR in a comma-separated list is valid
func validateCIDRs(cidrs string) error {
for _, cidr := range strings.Split(cidrs, ",") {
if cidr = strings.TrimSpace(cidr); cidr == "" {
continue
}
if _, _, err := net.ParseCIDR(cidr); err != nil {
return fmt.Errorf("invalid CIDR address '%s': %s", cidr, err)
}
}
return nil
}

0 comments on commit a06b789

Please sign in to comment.