Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: images form iOS are blocked when restrictions are applied [WPB-10830] 🍒 🍒 #3016

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,33 @@ internal class AssetMessageHandlerImpl(
FileSharingStatus.Value.Disabled -> false
FileSharingStatus.Value.EnabledAll -> true

<<<<<<< HEAD
is FileSharingStatus.Value.EnabledSome -> validateAssetMimeTypeUseCase(
messageContent.value.name,
it.state.allowedType
)
=======
is FileSharingStatus.Value.EnabledSome -> {
// If the asset message is missing the name, but it does have full
// 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 (validateAssetMimeTypeUseCase(
fileName = messageContent.value.name,
mimeType = messageContent.value.mimeType,
allowedExtension = it.state.allowedType
)
) {
AssetRestrictionContinuationStrategy.Continue
} else {
if (messageContent.value.name.isNullOrEmpty() && messageContent.value.isAssetDataComplete) {
AssetRestrictionContinuationStrategy.RestrictIfThereIsNotOldMessageWithTheSameAssetID
} else {
AssetRestrictionContinuationStrategy.Restrict
}
}
}
>>>>>>> 6037016703 (fix: images form iOS are blocked when restrictions are applied [WPB-10830] 🍒 (#3010))
}

if (isThisAssetAllowed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.wire.kalium.logic.functional.Either
import com.wire.kalium.util.time.UNIX_FIRST_DATE
import io.mockative.Mock
import io.mockative.any
import io.mockative.classOf
import io.mockative.coEvery
import io.mockative.coVerify
import io.mockative.eq
Expand Down Expand Up @@ -251,7 +252,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 @@ -271,11 +272,28 @@ class AssetMessageHandlerTest {
})
}.wasInvoked(exactly = once)

<<<<<<< HEAD
coVerify { arrangement.messageRepository.getMessageById(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id)) }
.wasInvoked(exactly = once)

coVerify { arrangement.validateAssetMimeType(eq(COMPLETE_ASSET_CONTENT.value.name), eq(isFileSharingEnabled.allowedType)) }
.wasInvoked(exactly = once)
=======
coVerify {
arrangement.messageRepository.getMessageById(
eq(previewAssetMessage.conversationId),
eq(previewAssetMessage.id)
)
}.wasInvoked(exactly = once)

coVerify {
arrangement.validateAssetFileTypeUseCase(
fileName = eq(COMPLETE_ASSET_CONTENT.value.name),
mimeType = eq("application/zip"),
allowedExtension = eq(isFileSharingEnabled.allowedType)
)
}.wasInvoked(exactly = once)
>>>>>>> 6037016703 (fix: images form iOS are blocked when restrictions are applied [WPB-10830] 🍒 (#3010))
}

@Test
Expand All @@ -286,7 +304,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 @@ -308,8 +326,18 @@ class AssetMessageHandlerTest {
coVerify { arrangement.messageRepository.getMessageById(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id)) }
.wasInvoked(exactly = once)

<<<<<<< HEAD
coVerify { arrangement.validateAssetMimeType(eq(COMPLETE_ASSET_CONTENT.value.name), eq(isFileSharingEnabled.allowedType)) }
.wasInvoked(exactly = once)
=======
coVerify {
arrangement.validateAssetFileTypeUseCase(
fileName = eq(COMPLETE_ASSET_CONTENT.value.name),
mimeType = eq("application/zip"),
allowedExtension = eq(isFileSharingEnabled.allowedType)
)
}.wasInvoked(exactly = once)
>>>>>>> 6037016703 (fix: images form iOS are blocked when restrictions are applied [WPB-10830] 🍒 (#3010))
}

@Test
Expand All @@ -320,7 +348,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 @@ -342,10 +370,106 @@ class AssetMessageHandlerTest {
coVerify { arrangement.messageRepository.getMessageById(eq(previewAssetMessage.conversationId), eq(previewAssetMessage.id)) }
.wasNotInvoked()

<<<<<<< HEAD
coVerify { arrangement.validateAssetMimeType(any<String>(), any<List<String>>()) }
.wasNotInvoked()
}

=======
coVerify { arrangement.validateAssetFileTypeUseCase(any<String>(), any<String>(), any<List<String>>()) }
}

@Test
fun givenFileWithNullNameAndCompleteData_whenProcessingCheckAPreviousAssetWithTheSameIDIsRestricted_thenDoNotStore() = runTest {
// Given
val messageCOntant = MessageContent.Asset(
AssetContent(
sizeInBytes = 100,
name = null,
mimeType = "",
metadata = null,
remoteData = AssetContent.RemoteData(
otrKey = "otrKey".toByteArray(),
sha256 = "sha256".toByteArray(),
assetId = "some-asset-id",
assetDomain = "some-asset-domain",
assetToken = "some-asset-token",
encryptionAlgorithm = MessageEncryptionAlgorithm.AES_GCM
),
)

)
val assetMessage = COMPLETE_ASSET_MESSAGE.copy(content = messageCOntant)

val previewAssetMessage = PREVIEW_ASSET_MESSAGE.copy(
visibility = Message.Visibility.HIDDEN,
content = MessageContent.RestrictedAsset("application/zip", 500, "some-asset-name.zip.")
)

val isFileSharingEnabled = FileSharingStatus.Value.EnabledSome(listOf("txt", "png", "zip"))
val (arrangement, assetMessageHandler) = Arrangement()
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withSuccessfulStoredMessage(previewAssetMessage)
.withValidateAssetFileType(true)
.arrange()

// When
assetMessageHandler.handle(assetMessage)

// Then
coVerify { arrangement.persistMessage(any()) }
.wasNotInvoked()

coVerify {
arrangement.messageRepository.getMessageById(
eq(assetMessage.conversationId), eq(assetMessage.id)
)
}.wasInvoked(exactly = once)
}

@Test
fun givenFileWithNullNameAndCompleteData_whenProcessingCheckAPreviousAssetWithTheSameIDIsMissing_thenStoreAsRestricted() = runTest {
// Given
val messageCOntant = MessageContent.Asset(
AssetContent(
sizeInBytes = 100,
name = null,
mimeType = "",
metadata = null,
remoteData = AssetContent.RemoteData(
otrKey = "otrKey".toByteArray(),
sha256 = "sha256".toByteArray(),
assetId = "some-asset-id",
assetDomain = "some-asset-domain",
assetToken = "some-asset-token",
encryptionAlgorithm = MessageEncryptionAlgorithm.AES_GCM
),
)

)
val assetMessage = COMPLETE_ASSET_MESSAGE.copy(content = messageCOntant)

val storedMessage = assetMessage.copy(content = MessageContent.RestrictedAsset(mimeType = "", sizeInBytes = 100, name = ""))
val isFileSharingEnabled = FileSharingStatus.Value.EnabledSome(listOf("txt", "png", "zip"))
val (arrangement, assetMessageHandler) = Arrangement()
.withSuccessfulFileSharingFlag(isFileSharingEnabled)
.withSuccessfulStoredMessage(null)
.withSuccessfulPersistMessageUseCase(storedMessage)
.withValidateAssetFileType(true)
.arrange()

// When
assetMessageHandler.handle(assetMessage)

// Then
coVerify { arrangement.persistMessage(any()) }
.wasInvoked(exactly = once)

coVerify { arrangement.messageRepository.getMessageById(eq(assetMessage.conversationId), eq(assetMessage.id)) }
.wasInvoked(exactly = once)
}

>>>>>>> 6037016703 (fix: images form iOS are blocked when restrictions are applied [WPB-10830] 🍒 (#3010))
private class Arrangement {

@Mock
Expand All @@ -358,14 +482,18 @@ class AssetMessageHandlerTest {
val userConfigRepository = mock(UserConfigRepository::class)

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

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

fun withValidateAssetMime(result: Boolean) = apply {
fun withValidateAssetFileType(result: Boolean) = apply {
every {
<<<<<<< HEAD
validateAssetMimeType.invoke(any(), any())
=======
validateAssetFileTypeUseCase.invoke(any(), any(), any())
>>>>>>> 6037016703 (fix: images form iOS are blocked when restrictions are applied [WPB-10830] 🍒 (#3010))
}.returns(result)
}

Expand Down