Skip to content

Commit b84f4e2

Browse files
committed
pkg/util/uuid: Make uuid parsing errors as safe string
This patch helps to make the uuid parsing errors as safe so that when the roachpb.Key is logged, the error message is not redacted. The prior implementation of uuid errors followed the fmt.Errorf strings which are categorized as unsafe by default when logged using %v verb of string formatting. This patch convert those error objects to use redact.Safe implementation. Epic: CRDB-37533 Part of: CRDB-44885 Release note: None
1 parent 4803923 commit b84f4e2

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

pkg/util/uuid/codec.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ package uuid
1414
import (
1515
"bytes"
1616
"encoding/hex"
17-
"fmt"
17+
18+
"github.com/cockroachdb/errors"
19+
"github.com/cockroachdb/redact"
1820
)
1921

2022
// FromBytes returns a UUID generated from the raw byte slice input.
@@ -101,7 +103,7 @@ func (u *UUID) UnmarshalText(text []byte) error {
101103
stringifiedText := string(text)
102104

103105
if l < 32 || l > 48 {
104-
return fmt.Errorf("uuid: incorrect UUID length: %s", text)
106+
return errors.Newf("uuid: incorrect UUID length: %s", redact.SafeString(stringifiedText))
105107
} else if stringifiedText[0] == '{' && stringifiedText[l-1] == '}' {
106108
return u.decodeHyphenated(text[1 : l-1])
107109
} else if bytes.Equal(text[:9], urnPrefix) {
@@ -129,19 +131,19 @@ func (u *UUID) decodeHashLike(t []byte) error {
129131
func (u *UUID) decodeHyphenated(t []byte) error {
130132
l := len(t)
131133
if l < 32 || l > 40 {
132-
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
134+
return errors.Newf("uuid: incorrect UUID format: %s", redact.SafeString(string(t)))
133135
}
134136

135137
hashLike := make([]byte, 32)
136138
countSinceHyphen := 0
137139
i := 0
138140
for _, c := range t {
139141
if i >= len(hashLike) {
140-
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
142+
return errors.Newf("uuid: incorrect UUID format: %s", redact.SafeString(string(t)))
141143
}
142144
if c == '-' {
143145
if countSinceHyphen == 0 || countSinceHyphen%4 != 0 {
144-
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
146+
return errors.Newf("uuid: incorrect UUID format: %s", redact.SafeString(string(t)))
145147
}
146148
countSinceHyphen = 0
147149
continue
@@ -151,7 +153,7 @@ func (u *UUID) decodeHyphenated(t []byte) error {
151153
countSinceHyphen++
152154
}
153155
if i != len(hashLike) {
154-
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
156+
return errors.Newf("uuid: incorrect UUID format: %s", redact.SafeString(string(t)))
155157
}
156158
return u.decodeHashLike(hashLike)
157159
}
@@ -165,7 +167,10 @@ func (u UUID) MarshalBinary() ([]byte, error) {
165167
// It will return an error if the slice isn't 16 bytes long.
166168
func (u *UUID) UnmarshalBinary(data []byte) error {
167169
if len(data) != Size {
168-
return fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data))
170+
return errors.Newf(
171+
"uuid: UUID must be exactly 16 bytes long, got %d bytes",
172+
redact.SafeInt(len(data)),
173+
)
169174
}
170175
copy(u[:], data)
171176

0 commit comments

Comments
 (0)