Skip to content

Commit 487d7fa

Browse files
committed
fix: panic under some stupid input config
1 parent 4b15568 commit 487d7fa

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

adapter/outbound/reality.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
2020
config := new(tlsC.RealityConfig)
2121

2222
const x25519ScalarSize = 32
23-
var publicKey [x25519ScalarSize]byte
24-
n, err := base64.RawURLEncoding.Decode(publicKey[:], []byte(o.PublicKey))
25-
if err != nil || n != x25519ScalarSize {
23+
publicKey, err := base64.RawURLEncoding.DecodeString(o.PublicKey)
24+
if err != nil || len(publicKey) != x25519ScalarSize {
2625
return nil, errors.New("invalid REALITY public key")
2726
}
28-
config.PublicKey, err = ecdh.X25519().NewPublicKey(publicKey[:])
27+
config.PublicKey, err = ecdh.X25519().NewPublicKey(publicKey)
2928
if err != nil {
3029
return nil, fmt.Errorf("fail to create REALITY public key: %w", err)
3130
}
3231

32+
n := hex.DecodedLen(len(o.ShortID))
33+
if n > tlsC.RealityMaxShortIDLen {
34+
return nil, errors.New("invalid REALITY short id")
35+
}
3336
n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID))
3437
if err != nil || n > tlsC.RealityMaxShortIDLen {
3538
return nil, errors.New("invalid REALITY short ID")

listener/reality/reality.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ func (c Config) Build() (*Builder, error) {
5050
realityConfig.ShortIds = make(map[[8]byte]bool)
5151
for i, shortIDString := range c.ShortID {
5252
var shortID [8]byte
53-
decodedLen, err := hex.Decode(shortID[:], []byte(shortIDString))
53+
decodedLen := hex.DecodedLen(len(shortIDString))
54+
if decodedLen > 8 {
55+
return nil, fmt.Errorf("invalid short_id[%d]: %s", i, shortIDString)
56+
}
57+
decodedLen, err = hex.Decode(shortID[:], []byte(shortIDString))
5458
if err != nil {
5559
return nil, fmt.Errorf("decode short_id[%d] '%s': %w", i, shortIDString, err)
5660
}

0 commit comments

Comments
 (0)