diff --git a/controllers/network.go b/controllers/network.go index 3c8cbf7a3a..ff3a39d08b 100644 --- a/controllers/network.go +++ b/controllers/network.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "net" "net/http" "strings" @@ -246,6 +247,40 @@ func createNetwork(w http.ResponseWriter, r *http.Request) { return } + // validate address ranges: must be private + if network.AddressRange != "" { + ip, _, err := net.ParseCIDR(network.AddressRange) + if err != nil { + logger.Log(0, r.Header.Get("user"), "failed to create network: ", + err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + if !ip.IsPrivate() { + err := errors.New("address range must be private") + logger.Log(0, r.Header.Get("user"), "failed to create network: ", + err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + } + if network.AddressRange6 != "" { + ip, _, err := net.ParseCIDR(network.AddressRange6) + if err != nil { + logger.Log(0, r.Header.Get("user"), "failed to create network: ", + err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + if !ip.IsPrivate() { + err := errors.New("address range must be private") + logger.Log(0, r.Header.Get("user"), "failed to create network: ", + err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + } + network, err = logic.CreateNetwork(network) if err != nil { logger.Log(0, r.Header.Get("user"), "failed to create network: ",