mirror of
https://github.com/juanfont/headscale.git
synced 2026-09-22 00:04:53 +09:00
util: check RNG error before slicing url-safe random string
A crypto/rand failure sliced empty output and panicked.
This commit is contained in:
committed by
Kristoffer Dalby
parent
ba54349176
commit
84c99023e5
@@ -32,9 +32,18 @@ func GenerateRandomBytes(n int) ([]byte, error) {
|
||||
func GenerateRandomStringURLSafe(n int) (string, error) {
|
||||
b, err := GenerateRandomBytes(n)
|
||||
|
||||
uenc := base64.RawURLEncoding.EncodeToString(b)
|
||||
return encodeRandomURLSafe(b, n, err)
|
||||
}
|
||||
|
||||
return uenc[:n], err
|
||||
// encodeRandomURLSafe URL-safe base64-encodes b and truncates to n. It checks
|
||||
// err first: on an RNG failure b is nil, so slicing the empty encoding would
|
||||
// panic instead of returning the ("", err) the caller is promised.
|
||||
func encodeRandomURLSafe(b []byte, n int, err error) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return base64.RawURLEncoding.EncodeToString(b)[:n], nil
|
||||
}
|
||||
|
||||
// GenerateRandomStringDNSSafe returns a DNS-safe
|
||||
|
||||
Reference in New Issue
Block a user