Skip to content

Commit

Permalink
fix: images form iOS are blocked when restrictions are applied
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamadJaara committed Sep 11, 2024
1 parent 538bae1 commit 13fd278
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,18 @@ internal class AssetMessageHandlerImpl(
// asset data then we can not decide now if it is allowed or not
// it is safe to continue and the code later will check the original
// asset message and decide if it is allowed or not
if (
message.content.value.name.isNullOrEmpty() &&
message.content.value.isAssetDataComplete
) {
kaliumLogger.e("The asset message trying to be processed has invalid data looking locally")
AssetRestrictionContinuationStrategy.RestrictIfThereIsNotOldMessageWithTheSameAssetID
} else {
validateAssetMimeTypeUseCase(
if (validateAssetMimeTypeUseCase(
fileName = messageContent.value.name,
mimeType = messageContent.value.mimeType,
allowedExtension = it.state.allowedType
).let { validateResult ->
if (validateResult) {
AssetRestrictionContinuationStrategy.Continue
} else {
AssetRestrictionContinuationStrategy.Restrict
}
)
) {
AssetRestrictionContinuationStrategy.Continue
} else {
if (messageContent.value.name.isNullOrEmpty() && messageContent.value.isAssetDataComplete) {
AssetRestrictionContinuationStrategy.RestrictIfThereIsNotOldMessageWithTheSameAssetID
} else {
AssetRestrictionContinuationStrategy.Restrict
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.wire.kalium.logic.sync.receiver.conversation.message.hasValidData
import com.wire.kalium.logic.sync.receiver.conversation.message.hasValidRemoteData
import io.mockative.Mock
import io.mockative.any
import io.mockative.anything
import io.mockative.classOf
import io.mockative.eq
import io.mockative.given
Expand Down Expand Up @@ -261,7 +262,7 @@ class AssetMessageHandlerTest {
val isFileSharingEnabled = FileSharingStatus.Value.EnabledSome(listOf("txt", "png", "zip"))
val (arrangement, assetMessageHandler) = Arrangement()
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withValidateAssetMime(true)
.withValidateAssetFileType(true)
.withSuccessfulStoredMessage(previewAssetMessage)
.withSuccessfulPersistMessageUseCase(updateAssetMessage)
.arrange()
Expand All @@ -286,8 +287,8 @@ class AssetMessageHandlerTest {
.with(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id))
.wasInvoked(exactly = once)

verify(arrangement.validateAssetMimeType)
.suspendFunction(arrangement.validateAssetMimeType::invoke)
verify(arrangement.validateAssetFileTypeUseCase)
.suspendFunction(arrangement.validateAssetFileTypeUseCase::invoke)
.with(eq(COMPLETE_ASSET_CONTENT.value.name), eq("application/zip"), eq(isFileSharingEnabled.allowedType))
.wasInvoked(exactly = once)
}
Expand All @@ -300,7 +301,7 @@ class AssetMessageHandlerTest {
val isFileSharingEnabled = FileSharingStatus.Value.EnabledSome(listOf("txt", "png"))
val (arrangement, assetMessageHandler) = Arrangement()
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withValidateAssetMime(true)
.withValidateAssetFileType(true)
.withSuccessfulStoredMessage(previewAssetMessage)
.withSuccessfulPersistMessageUseCase(updateAssetMessage)
.arrange()
Expand All @@ -325,8 +326,8 @@ class AssetMessageHandlerTest {
.with(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id))
.wasInvoked(exactly = once)

verify(arrangement.validateAssetMimeType)
.suspendFunction(arrangement.validateAssetMimeType::invoke)
verify(arrangement.validateAssetFileTypeUseCase)
.suspendFunction(arrangement.validateAssetFileTypeUseCase::invoke)
.with(eq(COMPLETE_ASSET_CONTENT.value.name), eq("application/zip"), eq(isFileSharingEnabled.allowedType))
.wasInvoked(exactly = once)
}
Expand All @@ -339,7 +340,7 @@ class AssetMessageHandlerTest {
val isFileSharingEnabled = FileSharingStatus.Value.Disabled
val (arrangement, assetMessageHandler) = Arrangement()
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withValidateAssetMime(true)
.withValidateAssetFileType(true)
.withSuccessfulStoredMessage(previewAssetMessage)
.withSuccessfulPersistMessageUseCase(updateAssetMessage)
.arrange()
Expand All @@ -364,8 +365,8 @@ class AssetMessageHandlerTest {
.with(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id))
.wasNotInvoked()

verify(arrangement.validateAssetMimeType)
.suspendFunction(arrangement.validateAssetMimeType::invoke)
verify(arrangement.validateAssetFileTypeUseCase)
.suspendFunction(arrangement.validateAssetFileTypeUseCase::invoke)
.with(any<String>(), any<List<String>>())
.wasNotInvoked()
}
Expand Down Expand Up @@ -403,6 +404,7 @@ class AssetMessageHandlerTest {
val (arrangement, assetMessageHandler) = Arrangement()
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withSuccessfulStoredMessage(previewAssetMessage)
.withValidateAssetFileType(true)
.arrange()

// When
Expand Down Expand Up @@ -450,6 +452,7 @@ class AssetMessageHandlerTest {
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withSuccessfulStoredMessage(null)
.withSuccessfulPersistMessageUseCase(storedMessage)
.withValidateAssetFileType(true)
.arrange()

// When
Expand Down Expand Up @@ -479,15 +482,15 @@ class AssetMessageHandlerTest {
val userConfigRepository = mock(classOf<UserConfigRepository>())

@Mock
val validateAssetMimeType = mock(classOf<ValidateAssetFileTypeUseCase>())
val validateAssetFileTypeUseCase = mock(classOf<ValidateAssetFileTypeUseCase>())

private val assetMessageHandlerImpl =
AssetMessageHandlerImpl(messageRepository, persistMessage, userConfigRepository, validateAssetMimeType)
AssetMessageHandlerImpl(messageRepository, persistMessage, userConfigRepository, validateAssetFileTypeUseCase)

fun withValidateAssetMime(result: Boolean) = apply {
given(validateAssetMimeType)
.function(validateAssetMimeType::invoke)
.whenInvokedWith(any(), any())
fun withValidateAssetFileType(result: Boolean) = apply {
given(validateAssetFileTypeUseCase)
.function(validateAssetFileTypeUseCase::invoke)
.whenInvokedWith(anything(), any(), any())
.thenReturn(result)
}

Expand Down

0 comments on commit 13fd278

Please sign in to comment.