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

View file

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