5543: Closes https://github.com/mozilla-mobile/android-components/issues/5541: Safer onMessageReceived error handling r=jonalmeida a=grigoryk



Co-authored-by: Grisha Kruglov <gkruglov@mozilla.com>
This commit is contained in:
MozLando
2020-01-15 13:34:22 +00:00
6 changed files with 58 additions and 12 deletions

View File

@@ -42,6 +42,7 @@ abstract class AbstractFirebasePushService(
PushProcessor.requireInstance.onNewToken(newToken)
}
@SuppressWarnings("TooGenericExceptionCaught")
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
remoteMessage?.let {
// This is not an AutoPush message we can handle.
@@ -49,19 +50,32 @@ abstract class AbstractFirebasePushService(
return
}
try {
val message = EncryptedPushMessage(
val message = try {
EncryptedPushMessage(
channelId = it.data.getValue(MESSAGE_KEY_CHANNEL_ID),
body = it.data.getValue(MESSAGE_KEY_BODY),
encoding = it.data.getValue(MESSAGE_KEY_ENCODING),
salt = it.data[MESSAGE_KEY_SALT],
cryptoKey = it.data[MESSAGE_KEY_CRYPTO_KEY]
)
PushProcessor.requireInstance.onMessageReceived(message)
} catch (e: NoSuchElementException) {
PushProcessor.requireInstance.onError(
PushError.MalformedMessage("parsing encrypted message failed: $e")
)
return
}
// In case of any errors, let the PushProcessor handle this exception. Instead of crashing
// here, just drop the message on the floor. This is fine, since we don't really need to
// "recover" from a bad incoming message.
// PushProcessor will submit relevant issues via a CrashReporter as appropriate.
try {
PushProcessor.requireInstance.onMessageReceived(message)
} catch (e: IllegalStateException) {
// Re-throw 'requireInstance' exceptions.
throw(e)
} catch (e: Exception) {
PushProcessor.requireInstance.onError(PushError.Rust(e))
}
}
}