mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
crypto: add length check to hacky megolm message index parser
This commit is contained in:
parent
0e4b074b57
commit
b226c03277
1 changed files with 5 additions and 0 deletions
|
|
@ -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])
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue