ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded can throw, ensure it will not crash the application.

This commit is contained in:
Benoit Marty 2024-09-12 21:05:16 +02:00
parent 8056285776
commit 14a52d4a05
2 changed files with 3 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import org.matrix.rustcomponents.sdk.crypto.PkEncryption
internal object ScanEncryptorUtils {
@Throws
fun getDownloadBodyAndEncryptIfNeeded(publicServerKey: String?, mxcUrl: String, elementToDecrypt: ElementToDecrypt): DownloadBody {
// TODO, upstream refactoring changed the object model here...
// it's bad we have to recreate and use hardcoded values
@ -43,6 +44,7 @@ internal object ScanEncryptorUtils {
v = "v2"
)
return if (publicServerKey != null) {
// Note: fromBase64 can throw Exception
val pkEncryption = PkEncryption.fromBase64(key = publicServerKey)
val pkMessage = pkEncryption.encrypt(DownloadBody(encryptedInfo).toCanonicalJson())
DownloadBody(

View File

@ -42,12 +42,12 @@ internal class DefaultScanEncryptedTask @Inject constructor(
override suspend fun execute(params: ScanEncryptedTask.Params): ScanResponse {
val mxcUrl = params.mxcUrl
val dlBody = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded(params.publicServerKey, params.mxcUrl, params.encryptedInfo)
val scannerUrl = contentScannerStore.getScannerUrl()
contentScannerStore.updateStateForContent(params.mxcUrl, ScanState.IN_PROGRESS, scannerUrl)
try {
val dlBody = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded(params.publicServerKey, params.mxcUrl, params.encryptedInfo)
val api = contentScannerApiProvider.contentScannerApi ?: throw IllegalArgumentException()
val executeRequest = executeRequest<ScanResponse>(null) {
api.scanFile(dlBody)