From 07041e7768f2106ef5b218886d8d18a6f4f9c307 Mon Sep 17 00:00:00 2001 From: Shtorm <108103062+shtorm-7@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:16:58 +0300 Subject: [PATCH] fix: Validate VLESS encryption padding --- protocol/vless/encryption/common.go | 12 +++++++----- protocol/vless/outbound.go | 5 ++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/protocol/vless/encryption/common.go b/protocol/vless/encryption/common.go index 6fbb35df..3ffd4b41 100644 --- a/protocol/vless/encryption/common.go +++ b/protocol/vless/encryption/common.go @@ -248,7 +248,8 @@ func ParsePadding(padding string, paddingLens, paddingGaps *[][3]int) (err error if padding == "" { return } - maxLen := 0 + const maximumPaddingLength = 18 + 65535 + remainingPaddingLength := maximumPaddingLength for i, s := range strings.Split(padding, ".") { x := strings.Split(s, "-") if len(x) < 3 || x[0] == "" || x[1] == "" || x[2] == "" { @@ -268,15 +269,16 @@ func ParsePadding(padding string, paddingLens, paddingGaps *[][3]int) (err error return E.New("first padding length must not be smaller than 35") } if i%2 == 0 { + paddingLength := max(y[1], y[2]) + if paddingLength > remainingPaddingLength { + return E.New("total padding length must not be larger than ", maximumPaddingLength) + } + remainingPaddingLength -= paddingLength *paddingLens = append(*paddingLens, y) - maxLen += max(y[1], y[2]) } else { *paddingGaps = append(*paddingGaps, y) } } - if maxLen > 18+65535 { - return E.New("total padding length must not be larger than 65553") - } return } diff --git a/protocol/vless/outbound.go b/protocol/vless/outbound.go index 6a6ebca3..d80539b9 100644 --- a/protocol/vless/outbound.go +++ b/protocol/vless/outbound.go @@ -5,6 +5,7 @@ import ( stdtls "crypto/tls" "encoding/base64" "net" + "regexp" "strings" "sync" @@ -443,6 +444,8 @@ type clientEncryptionConfig struct { padding string } +var clientEncryptionPaddingPattern = regexp.MustCompile(`^[0-9]+-[0-9]+-[0-9]+$`) + func parseClientEncryption(raw string) (clientEncryptionConfig, error) { var cfg clientEncryptionConfig raw = strings.TrimSpace(raw) @@ -481,7 +484,7 @@ func parseClientEncryption(raw string) (clientEncryptionConfig, error) { if segment == "" { return cfg, E.New("invalid empty segment in encryption string") } - if paddingPhase && len(segment) < 20 { + if paddingPhase && len(segment) < 20 && clientEncryptionPaddingPattern.MatchString(segment) { paddingParts = append(paddingParts, segment) continue }