From 6d1eb315001f3ad64530517cd84e0edcf0456b81 Mon Sep 17 00:00:00 2001 From: Kevin Chowski Date: Tue, 19 May 2020 10:16:26 -0600 Subject: [PATCH] When parsing a 38-char UUID, require '{' and '}' characters It was not intentional to simply ignore these characters, and examples/docs/tests all indicate that curly braces were the intended characters. I've added some test cases to ensure things work. Fixes https://github.com/google/uuid/issues/60 --- uuid.go | 6 ++++++ uuid_test.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/uuid.go b/uuid.go index 524404c..0f07174 100644 --- a/uuid.go +++ b/uuid.go @@ -55,6 +55,9 @@ func Parse(s string) (UUID, error) { // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} case 36 + 2: + if s[0] != '{' || s[37] != '}' { + return uuid, fmt.Errorf("invalid prefix/suffix: got %q and %q, want '{' and '}'", s[0], s[37]) + } s = s[1:] // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx @@ -101,6 +104,9 @@ func ParseBytes(b []byte) (UUID, error) { } b = b[9:] case 36 + 2: // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} + if b[0] != '{' || b[37] != '}' { + return uuid, fmt.Errorf("invalid prefix/suffix: got %q and %q, want '{' and '}'", b[0], b[37]) + } b = b[1:] case 32: // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx var ok bool diff --git a/uuid_test.go b/uuid_test.go index 4e04e7c..3b7614c 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -67,6 +67,9 @@ var tests = []test{ {"g47ac10b-58cc-4372-a567-0e02b2c3d479", 0, Invalid, false}, {"{f47ac10b-58cc-0372-8567-0e02b2c3d479}", 0, RFC4122, true}, + {"[f47ac10b-58cc-0372-8567-0e02b2c3d479]", 0, Invalid, false}, + {"{f47ac10b-58cc-0372-8567-0e02b2c3d479]", 0, Invalid, false}, + {"[f47ac10b-58cc-0372-8567-0e02b2c3d479}", 0, Invalid, false}, {"{f47ac10b-58cc-0372-8567-0e02b2c3d479", 0, Invalid, false}, {"f47ac10b-58cc-0372-8567-0e02b2c3d479}", 0, Invalid, false},