crypto: add length check to hacky megolm message index parser

This commit is contained in:
Tulir Asokan 2026-01-17 00:55:16 +02:00
commit b226c03277

View file

@ -91,11 +91,16 @@ func IsShareError(err error) bool {
}
func ParseMegolmMessageIndex(ciphertext []byte) (uint, error) {
if len(ciphertext) == 0 {
return 0, fmt.Errorf("empty ciphertext")
}
decoded := make([]byte, base64.RawStdEncoding.DecodedLen(len(ciphertext)))
var err error
_, err = base64.RawStdEncoding.Decode(decoded, ciphertext)
if err != nil {
return 0, err
} else if len(decoded) < 2+binary.MaxVarintLen64 {
return 0, fmt.Errorf("decoded ciphertext too short: %d bytes", len(decoded))
} else if decoded[0] != 3 || decoded[1] != 8 {
return 0, fmt.Errorf("unexpected initial bytes %d and %d", decoded[0], decoded[1])
}