[management] Extend nameserver match domain validation (#3864)

* Enhance match domain validation logic and add test cases

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>

* remove the leading dot and root dot support ns regex

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>

* Remove support for wildcard ns match domain

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>

---------

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
Bethuel Mmbaga 2025-05-23 00:16:19 +03:00 committed by GitHub
parent 24f932b2ce
commit 2a89d6e47a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 11 deletions

View File

@ -18,7 +18,9 @@ import (
"github.com/netbirdio/netbird/management/server/types" "github.com/netbirdio/netbird/management/server/types"
) )
const domainPattern = `^(?i)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,}$` const domainPattern = `^(?i)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*[*.a-z]{1,}$`
var invalidDomainName = errors.New("invalid domain name")
// GetNameServerGroup gets a nameserver group object from account and nameserver group IDs // GetNameServerGroup gets a nameserver group object from account and nameserver group IDs
func (am *DefaultAccountManager) GetNameServerGroup(ctx context.Context, accountID, userID, nsGroupID string) (*nbdns.NameServerGroup, error) { func (am *DefaultAccountManager) GetNameServerGroup(ctx context.Context, accountID, userID, nsGroupID string) (*nbdns.NameServerGroup, error) {
@ -319,13 +321,9 @@ func validateDomain(domain string) error {
return errors.New("domain should consists of only letters, numbers, and hyphens with no leading, trailing hyphens, or spaces") return errors.New("domain should consists of only letters, numbers, and hyphens with no leading, trailing hyphens, or spaces")
} }
labels, valid := dns.IsDomainName(domain) _, valid := dns.IsDomainName(domain)
if !valid { if !valid {
return errors.New("invalid domain name") return invalidDomainName
}
if labels < 2 {
return errors.New("domain should consists of a minimum of two labels")
} }
return nil return nil

View File

@ -899,13 +899,33 @@ func TestValidateDomain(t *testing.T) {
errFunc: require.NoError, errFunc: require.NoError,
}, },
{ {
name: "Invalid domain name with double hyphen", name: "Valid domain name with only one label",
domain: "test--example.com", domain: "example",
errFunc: require.NoError,
},
{
name: "Valid domain name with trailing dot",
domain: "example.",
errFunc: require.NoError,
},
{
name: "Invalid wildcard domain name",
domain: "*.example",
errFunc: require.Error, errFunc: require.Error,
}, },
{ {
name: "Invalid domain name with only one label", name: "Invalid domain name with leading dot",
domain: "com", domain: ".com",
errFunc: require.Error,
},
{
name: "Invalid domain name with dot only",
domain: ".",
errFunc: require.Error,
},
{
name: "Invalid domain name with double hyphen",
domain: "test--example.com",
errFunc: require.Error, errFunc: require.Error,
}, },
{ {