Fix image decryption race condition

This commit is contained in:
Tulir Asokan 2023-07-16 01:45:19 +03:00
parent f22d4f6ac8
commit 50c2d45316
2 changed files with 7 additions and 14 deletions

View file

@ -31,7 +31,7 @@ type AuthData struct {
JWK *crypto.JWK `json:"jwk,omitempty"`
}
type Proxy func(*http.Request) (*url.URL, error)
type EventHandler func(evt interface{})
type EventHandler func(evt any)
type Client struct {
Logger zerolog.Logger
rpc *RPC
@ -39,7 +39,6 @@ type Client struct {
evHandler EventHandler
sessionHandler *SessionHandler
imageCryptor *crypto.ImageCryptor
authData *AuthData
proxy Proxy
@ -60,7 +59,6 @@ func NewClient(authData *AuthData, logger zerolog.Logger) *Client {
cli := &Client{
authData: authData,
Logger: logger,
imageCryptor: &crypto.ImageCryptor{},
sessionHandler: sessionHandler,
http: &http.Client{},
}
@ -230,8 +228,11 @@ func (c *Client) DownloadMedia(mediaID string, key []byte) ([]byte, error) {
return nil, err3
}
c.Logger.Debug().Any("key", key).Any("encryptedLength", len(encryptedBuffImg)).Msg("Attempting to decrypt image")
c.imageCryptor.UpdateDecryptionKey(key)
decryptedImageBytes, decryptionErr := c.imageCryptor.DecryptData(encryptedBuffImg)
cryptor, err := crypto.NewImageCryptor(key)
if err != nil {
return nil, err
}
decryptedImageBytes, decryptionErr := cryptor.DecryptData(encryptedBuffImg)
if decryptionErr != nil {
return nil, decryptionErr
}

View file

@ -19,14 +19,6 @@ func NewImageCryptor(key []byte) (*ImageCryptor, error) {
return &ImageCryptor{key: key}, nil
}
func (ic *ImageCryptor) GetKey() []byte {
return ic.key
}
func (ic *ImageCryptor) UpdateDecryptionKey(key []byte) {
ic.key = key
}
func (ic *ImageCryptor) Encrypt(imageBytes []byte, aad []byte) ([]byte, error) {
block, err := aes.NewCipher(ic.key)
if err != nil {