瀏覽代碼

remove fallback if rand.Reader fails

Failing to read from rand.Reader essentially can't happen, and if it
does is not possible to fallback securely, so just panic

Signed-off-by: Nicola Murino <[email protected]>
Nicola Murino 11 月之前
父節點
當前提交
f363d037a7
共有 1 個文件被更改,包括 4 次插入11 次删除
  1. 4 11
      internal/util/util.go

+ 4 - 11
internal/util/util.go

@@ -48,7 +48,6 @@ import (
 
 	"github.com/google/uuid"
 	"github.com/lithammer/shortuuid/v3"
-	"github.com/rs/xid"
 	"golang.org/x/crypto/ssh"
 
 	"github.com/drakkan/sftpgo/v2/internal/logger"
@@ -576,23 +575,17 @@ func createDirPathIfMissing(file string, perm os.FileMode) error {
 func GenerateRandomBytes(length int) []byte {
 	b := make([]byte, length)
 	_, err := io.ReadFull(rand.Reader, b)
-	if err == nil {
-		return b
-	}
-
-	b = xid.New().Bytes()
-	for len(b) < length {
-		b = append(b, xid.New().Bytes()...)
+	if err != nil {
+		PanicOnError(fmt.Errorf("failed to read random data (see https://go.dev/issue/66821): %w", err))
 	}
-
-	return b[:length]
+	return b
 }
 
 // GenerateUniqueID retuens an unique ID
 func GenerateUniqueID() string {
 	u, err := uuid.NewRandom()
 	if err != nil {
-		return xid.New().String()
+		PanicOnError(fmt.Errorf("failed to read random data (see https://go.dev/issue/66821): %w", err))
 	}
 	return shortuuid.DefaultEncoder.Encode(u)
 }