From aec5236db18e006eed327acf5b715be2e26c7733 Mon Sep 17 00:00:00 2001 From: Muhamad Azamy Date: Mon, 21 Dec 2020 17:23:46 +0100 Subject: [PATCH] validate gateway registration also validate domain names --- go.mod | 1 + go.sum | 2 ++ pkg/directory/gateway_handlers.go | 4 ++++ pkg/directory/types/gateway.go | 7 +++++++ 4 files changed, 14 insertions(+) diff --git a/go.mod b/go.mod index 6d56e207..050430b4 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/threefoldtech/tfexplorer go 1.14 require ( + github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect github.com/cenkalti/backoff v2.2.1+incompatible github.com/dave/jennifer v1.3.0 github.com/emicklei/dot v0.10.1 diff --git a/go.sum b/go.sum index 793a2709..a2d72bb3 100644 --- a/go.sum +++ b/go.sum @@ -42,6 +42,8 @@ github.com/asaskevich/govalidator v0.0.0-20180319081651-7d2e70ef918f/go.mod h1:l github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= +github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg= +github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= github.com/aws/aws-sdk-go v1.25.25/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= diff --git a/pkg/directory/gateway_handlers.go b/pkg/directory/gateway_handlers.go index 26ca0525..35fc624f 100644 --- a/pkg/directory/gateway_handlers.go +++ b/pkg/directory/gateway_handlers.go @@ -26,6 +26,10 @@ func (s *GatewayAPI) registerGateway(r *http.Request) (interface{}, mw.Response) return nil, mw.BadRequest(err) } + if err := gw.Validate(); err != nil { + return nil, mw.BadRequest(err) + } + db := mw.Database(r) if _, err := s.Add(r.Context(), db, gw); err != nil { return nil, mw.Error(err) diff --git a/pkg/directory/types/gateway.go b/pkg/directory/types/gateway.go index b8fb3532..9c6bb16e 100644 --- a/pkg/directory/types/gateway.go +++ b/pkg/directory/types/gateway.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "github.com/asaskevich/govalidator" "github.com/jbenet/go-base58" "github.com/pkg/errors" "github.com/threefoldtech/tfexplorer/models" @@ -47,6 +48,12 @@ func (n *Gateway) Validate() error { return fmt.Errorf("nodeID and public key does not match") } + for _, domain := range n.ManagedDomains { + if !govalidator.IsDNSName(domain) { + return fmt.Errorf("domain name '%s' is invalid", domain) + } + } + // Unfortunately, jsx schema does not support nil types // so this is the only way to check if values are not set empty := generated.Location{}