Преглед изворни кода

common/uuid: fix panic when parsing 32-len invalid UUID string. (#5468)

* common/uuid: fix panic when parsing 32-len invalid UUID string.

* fix: removed typo
ari-ahm пре 4 дана
родитељ
комит
6738ecf68e
3 измењених фајлова са 11 додато и 2 уклоњено
  1. 5 1
      common/uuid/uuid.go
  2. 5 0
      common/uuid/uuid_test.go
  3. 1 1
      proxy/vless/encoding/encoding.go

+ 5 - 1
common/uuid/uuid.go

@@ -85,10 +85,14 @@ func ParseString(str string) (UUID, error) {
 	b := uuid.Bytes()
 
 	for _, byteGroup := range byteGroups {
-		if text[0] == '-' {
+		if len(text) > 0 && text[0] == '-' {
 			text = text[1:]
 		}
 
+		if len(text) < byteGroup {
+			return uuid, errors.New("invalid UUID: ", str)
+		}
+
 		if _, err := hex.Decode(b[:byteGroup/2], text[:byteGroup]); err != nil {
 			return uuid, err
 		}

+ 5 - 0
common/uuid/uuid_test.go

@@ -44,6 +44,11 @@ func TestParseString(t *testing.T) {
 	if err == nil {
 		t.Fatal("Expect error but nil")
 	}
+
+	_, err = ParseString("2418d087-648d-4990-86e8-19dca1d0")
+	if err == nil {
+		t.Fatal("Expect error but nil")
+	}
 }
 
 func TestNewUUID(t *testing.T) {

+ 1 - 1
proxy/vless/encoding/encoding.go

@@ -93,7 +93,7 @@ func DecodeRequestHeader(isfb bool, first *buf.Buffer, reader io.Reader, validat
 
 		if request.User = validator.Get(id); request.User == nil {
 			u := uuid.UUID(id)
-			return nil, nil, nil, isfb, errors.New("invalid request user id: %s" + u.String())
+			return nil, nil, nil, isfb, errors.New("invalid request user id: " + u.String())
 		}
 
 		if isfb {