From 0432688a853effe551a483faf2c28b34883afcf8 Mon Sep 17 00:00:00 2001 From: thejesh23 <35212698+thejesh23@users.noreply.github.com> Date: Mon, 23 Nov 2026 02:12:10 -0800 Subject: [PATCH] pgtype: reject 36-character UUIDs without hyphens in the right places parseUUID dropped bytes 8, 13, 18 and 23 of a 36-character string without checking that they are hyphens, so a 36-hex-digit string or one with other separators parsed as a different UUID. PostgreSQL's uuid_in rejects both. --- pgtype/uuid.go | 3 +++ pgtype/uuid_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/pgtype/uuid.go b/pgtype/uuid.go index 725defec0..c215329af 100644 --- a/pgtype/uuid.go +++ b/pgtype/uuid.go @@ -35,6 +35,9 @@ func (b UUID) UUIDValue() (UUID, error) { func parseUUID(src string) (dst [16]byte, err error) { switch len(src) { case 36: + if src[8] != '-' || src[13] != '-' || src[18] != '-' || src[23] != '-' { + return dst, fmt.Errorf("cannot parse UUID %v", src) + } src = src[0:8] + src[9:13] + src[14:18] + src[19:23] + src[24:] case 32: // dashes already stripped, assume valid diff --git a/pgtype/uuid_test.go b/pgtype/uuid_test.go index e608585e3..946a48aba 100644 --- a/pgtype/uuid_test.go +++ b/pgtype/uuid_test.go @@ -174,3 +174,14 @@ func TestUUID_UnmarshalJSON(t *testing.T) { }) } } + +func TestUUIDRejectsMisplacedHyphens(t *testing.T) { + for _, s := range []string{ + "a0eebc99X9c0bX4ef8Xbb6dX6bb9bd380a11", + "a0eebc999c0b4ef8bb6d6bb9bd380a11abcd", + } { + var u pgtype.UUID + require.Errorf(t, u.Scan(s), "Scan(%q)", s) + require.Errorf(t, u.UnmarshalJSON([]byte(`"`+s+`"`)), "UnmarshalJSON(%q)", s) + } +}