From db4dff81bf1405b572903235194453bc216d65ad Mon Sep 17 00:00:00 2001 From: the_aceix Date: Thu, 7 Dec 2023 16:55:39 +0000 Subject: [PATCH] fix(NET-760): check for private network range --- controllers/network.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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: ",