Skip to content

Commit

Permalink
fix(uuid): UUID regexes to support all-or-none '-' separator
Browse files Browse the repository at this point in the history
This PR changes the UUID validation regexps to support either
UUIDs with the expected number of separators or no separator at all.

* contributes go-swagger/go-swagger#2878

Signed-off-by: Frederic BIDON <[email protected]>
  • Loading branch information
fredbi committed Dec 27, 2023
1 parent 03a91f9 commit 12fa10e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
9 changes: 5 additions & 4 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ const (
// - symbol unicode points are permitted (e.g. emoji) (not for top-level domain)
HostnamePattern = `^([a-zA-Z0-9\p{S}\p{L}]((-?[a-zA-Z0-9\p{S}\p{L}]{0,62})?)|([a-zA-Z0-9\p{S}\p{L}](([a-zA-Z0-9-\p{S}\p{L}]{0,61}[a-zA-Z0-9\p{S}\p{L}])?)(\.)){1,}([a-zA-Z\p{L}]){2,63})$`
// UUIDPattern Regex for UUID that allows uppercase
UUIDPattern = `(?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$`
UUIDPattern = `(?i)(^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$)|(^[0-9a-f]{32}$)`
// UUID3Pattern Regex for UUID3 that allows uppercase
UUID3Pattern = `(?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?3[0-9a-f]{3}-?[0-9a-f]{4}-?[0-9a-f]{12}$`
UUID3Pattern = `(?i)(^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$)|(^[0-9a-f]{12}3[0-9a-f]{3}?[0-9a-f]{16}$)`
// UUID4Pattern Regex for UUID4 that allows uppercase
UUID4Pattern = `(?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?4[0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$`
UUID4Pattern = `(?i)(^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$)|(^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$)`

// UUID5Pattern Regex for UUID5 that allows uppercase
UUID5Pattern = `(?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?5[0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$`
UUID5Pattern = `(?i)(^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$)|(^[0-9a-f]{12}5[0-9a-f]{3}[89ab][0-9a-f]{15}$)`
// json null type
jsonNull = "null"
)
Expand Down
76 changes: 72 additions & 4 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,26 @@ func TestFormatMAC(t *testing.T) {
func TestFormatUUID3(t *testing.T) {
first3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("somewhere.com"))
other3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("somewhereelse.com"))
other4 := uuid.Must(uuid.NewRandom())
other5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhereelse.com"))
uuid3 := UUID3(first3.String())
str := other3.String()
testStringFormat(t, &uuid3, "uuid3", str, []string{}, []string{"not-a-uuid"})
testStringFormat(t, &uuid3, "uuid3", str,
[]string{
other3.String(),
strings.ReplaceAll(other3.String(), "-", ""),
},
[]string{
"not-a-uuid",
other4.String(),
other5.String(),
strings.ReplaceAll(other4.String(), "-", ""),
strings.ReplaceAll(other5.String(), "-", ""),
strings.Replace(other3.String(), "-", "", 2),
strings.Replace(other4.String(), "-", "", 2),
strings.Replace(other5.String(), "-", "", 2),
},
)

// special case for zero UUID
var uuidZero UUID3
Expand All @@ -188,10 +205,27 @@ func TestFormatUUID3(t *testing.T) {

func TestFormatUUID4(t *testing.T) {
first4 := uuid.Must(uuid.NewRandom())
other3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("somewhere.com"))
other4 := uuid.Must(uuid.NewRandom())
other5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhereelse.com"))
uuid4 := UUID4(first4.String())
str := other4.String()
testStringFormat(t, &uuid4, "uuid4", str, []string{}, []string{"not-a-uuid"})
testStringFormat(t, &uuid4, "uuid4", str,
[]string{
other4.String(),
strings.ReplaceAll(other4.String(), "-", ""),
},
[]string{
"not-a-uuid",
other3.String(),
other5.String(),
strings.ReplaceAll(other3.String(), "-", ""),
strings.ReplaceAll(other5.String(), "-", ""),
strings.Replace(other3.String(), "-", "", 2),
strings.Replace(other4.String(), "-", "", 2),
strings.Replace(other5.String(), "-", "", 2),
},
)

// special case for zero UUID
var uuidZero UUID4
Expand All @@ -202,10 +236,27 @@ func TestFormatUUID4(t *testing.T) {

func TestFormatUUID5(t *testing.T) {
first5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhere.com"))
other3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("somewhere.com"))
other4 := uuid.Must(uuid.NewRandom())
other5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhereelse.com"))
uuid5 := UUID5(first5.String())
str := other5.String()
testStringFormat(t, &uuid5, "uuid5", str, []string{}, []string{"not-a-uuid"})
testStringFormat(t, &uuid5, "uuid5", str,
[]string{
other5.String(),
strings.ReplaceAll(other5.String(), "-", ""),
},
[]string{
"not-a-uuid",
other3.String(),
other4.String(),
strings.ReplaceAll(other3.String(), "-", ""),
strings.ReplaceAll(other4.String(), "-", ""),
strings.Replace(other3.String(), "-", "", 2),
strings.Replace(other4.String(), "-", "", 2),
strings.Replace(other5.String(), "-", "", 2),
},
)

// special case for zero UUID
var uuidZero UUID5
Expand All @@ -216,10 +267,27 @@ func TestFormatUUID5(t *testing.T) {

func TestFormatUUID(t *testing.T) {
first5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhere.com"))
other3 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhereelse.com"))
other4 := uuid.Must(uuid.NewRandom())
other5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhereelse.com"))
uuid := UUID(first5.String())
str := other5.String()
testStringFormat(t, &uuid, "uuid", str, []string{}, []string{"not-a-uuid"})
testStringFormat(t, &uuid, "uuid", str,
[]string{
other3.String(),
other4.String(),
other5.String(),
strings.ReplaceAll(other3.String(), "-", ""),
strings.ReplaceAll(other4.String(), "-", ""),
strings.ReplaceAll(other5.String(), "-", ""),
},
[]string{
"not-a-uuid",
strings.Replace(other3.String(), "-", "", 2),
strings.Replace(other4.String(), "-", "", 2),
strings.Replace(other5.String(), "-", "", 2),
},
)

// special case for zero UUID
var uuidZero UUID
Expand Down

0 comments on commit 12fa10e

Please sign in to comment.