fix: panic under some stupid input config

This commit is contained in:
wwqgtxx 2025-04-09 17:53:36 +08:00
parent 4b15568a29
commit 487d7fa81f
2 changed files with 12 additions and 5 deletions

View File

@ -20,16 +20,19 @@ func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
config := new(tlsC.RealityConfig) config := new(tlsC.RealityConfig)
const x25519ScalarSize = 32 const x25519ScalarSize = 32
var publicKey [x25519ScalarSize]byte publicKey, err := base64.RawURLEncoding.DecodeString(o.PublicKey)
n, err := base64.RawURLEncoding.Decode(publicKey[:], []byte(o.PublicKey)) if err != nil || len(publicKey) != x25519ScalarSize {
if err != nil || n != x25519ScalarSize {
return nil, errors.New("invalid REALITY public key") return nil, errors.New("invalid REALITY public key")
} }
config.PublicKey, err = ecdh.X25519().NewPublicKey(publicKey[:]) config.PublicKey, err = ecdh.X25519().NewPublicKey(publicKey)
if err != nil { if err != nil {
return nil, fmt.Errorf("fail to create REALITY public key: %w", err) return nil, fmt.Errorf("fail to create REALITY public key: %w", err)
} }
n := hex.DecodedLen(len(o.ShortID))
if n > tlsC.RealityMaxShortIDLen {
return nil, errors.New("invalid REALITY short id")
}
n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID)) n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID))
if err != nil || n > tlsC.RealityMaxShortIDLen { if err != nil || n > tlsC.RealityMaxShortIDLen {
return nil, errors.New("invalid REALITY short ID") return nil, errors.New("invalid REALITY short ID")

View File

@ -50,7 +50,11 @@ func (c Config) Build() (*Builder, error) {
realityConfig.ShortIds = make(map[[8]byte]bool) realityConfig.ShortIds = make(map[[8]byte]bool)
for i, shortIDString := range c.ShortID { for i, shortIDString := range c.ShortID {
var shortID [8]byte var shortID [8]byte
decodedLen, err := hex.Decode(shortID[:], []byte(shortIDString)) decodedLen := hex.DecodedLen(len(shortIDString))
if decodedLen > 8 {
return nil, fmt.Errorf("invalid short_id[%d]: %s", i, shortIDString)
}
decodedLen, err = hex.Decode(shortID[:], []byte(shortIDString))
if err != nil { if err != nil {
return nil, fmt.Errorf("decode short_id[%d] '%s': %w", i, shortIDString, err) return nil, fmt.Errorf("decode short_id[%d] '%s': %w", i, shortIDString, err)
} }