go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.07k stars 1.29k forks source link

Not correct FQDN validation #1218

Open nao99 opened 5 months ago

nao99 commented 5 months ago

v10:

Hi everyone

I just noticed the FQDN validator doesn't work properly It accepts the "-" symbol at the ends of n domain level

I searched is this planned or any other github issue was already created, but I didn't find anything similar

Issue, Question or Enhancement:

Here you can see screenshots the regex is satisfies with a "domain-.com" example

telegram-cloud-photo-size-2-5321297685145310438-y telegram-cloud-photo-size-2-5321297685145310439-y

Code sample, to showcase or reproduce:

package main

import (
    "fmt"
    "github.com/go-playground/validator/v10"
)

func main() {
    validate := validator.New()

    err := validate.Var("google.com", "required,fqdn")
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    err = validate.Var("google-.com", "required,fqdn")
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println("Everything is ok, but it shouldn't cause the \"google-.com\" is not looking as a valid domain name")

    err = validate.Var("-google.com", "required,fqdn")
    if err != nil {
        fmt.Printf("For example, this is invalid as expected: %s", err.Error())
        return
    }
}

Possible solution:

I found a regex which can possibly resolve this issue (but I didn't test anything except the regex itself via regex101) https://stackoverflow.com/questions/10306690/what-is-a-regular-expression-which-will-match-a-valid-domain-name-without-a-subd

Screenshot 2024-02-01 at 12 06 10