google / uuid

Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

uuid.Parse allows invalid UUID's

dhuckins opened this issue · comments

it seems that there are some cases where uuid.Parse is not throwing an error for invalid uuids
examples of invalid UUID's that do not give errors

  • extra dashes: -6ba7b810-9dad-11d1-80b4-00c04fd430c8-
  • extra characters: 06ba7b810-9dad-11d1-80b4-00c04fd430c81
  • quotes: "6ba7b810-9dad-11d1-80b4-00c04fd430c8"

see here for a reproducible example

This is a known "feature" and has been discussed before. Fixing this could very well break existing code. I actually don't see any issue with the first and third examples. The current code also can parse [6ba7b810-9dad-11d1-80b4-00c04fd430c8], <6ba7b810-9dad-11d1-80b4-00c04fd430c8>, (6ba7b810-9dad-11d1-80b4-00c04fd430c8), '6ba7b810-9dad-11d1-80b4-00c04fd430c8' all of which I have no particular problem with. I agree that the seocnd example does not return an error is unfortunate. In any event, the time for this change has long since passed.

If validating, rather than just parsing, strings as UUIDs I could imagine a function like:

func Validate(s string) (uuid, error) {
    if len(s) == 36 + 2 { 
      // do some checks
    }
    return Parse(s)
}

This is a duplicate of issue #60