From 0131bc171a8afa7cff2b2d668207329159930a6e Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 19 Jul 2021 13:52:50 +1000 Subject: [PATCH] Warn users if they're about to send their recovery phrase to someone --- .../conversation/v2/ConversationActivityV2.kt | 26 ++++++++- .../conversation/v2/dialogs/SendSeedDialog.kt | 23 ++++++++ app/src/main/res/layout/dialog_send_seed.xml | 57 +++++++++++++++++++ app/src/main/res/values/strings.xml | 4 ++ 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/SendSeedDialog.kt create mode 100644 app/src/main/res/layout/dialog_send_seed.xml diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index d4e0b3a1dd..f57726bcb2 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -71,8 +71,10 @@ import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.concurrent.SimpleTask import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.recipients.RecipientModifiedListener +import org.session.libsignal.crypto.MnemonicCodec import org.session.libsignal.utilities.ListenableFuture import org.session.libsignal.utilities.guava.Optional +import org.session.libsignal.utilities.hexEncodedPrivateKey import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.audio.AudioRecorder @@ -94,6 +96,8 @@ import org.thoughtcrime.securesms.conversation.v2.utilities.AttachmentManager import org.thoughtcrime.securesms.conversation.v2.utilities.BaseDialog import org.thoughtcrime.securesms.conversation.v2.utilities.MentionUtilities import org.thoughtcrime.securesms.conversation.v2.utilities.ResendMessageUtilities +import org.thoughtcrime.securesms.crypto.IdentityKeyUtil +import org.thoughtcrime.securesms.crypto.MnemonicUtilities import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.DraftDatabase import org.thoughtcrime.securesms.database.DraftDatabase.Drafts @@ -153,6 +157,17 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe private val layoutManager: LinearLayoutManager get() { return conversationRecyclerView.layoutManager as LinearLayoutManager } + private val seed by lazy { + var hexEncodedSeed = IdentityKeyUtil.retrieve(this, IdentityKeyUtil.LOKI_SEED) + if (hexEncodedSeed == null) { + hexEncodedSeed = IdentityKeyUtil.getIdentityKeyPair(this).hexEncodedPrivateKey // Legacy account + } + val loadFileContents: (String) -> String = { fileName -> + MnemonicUtilities.loadFileContents(this, fileName) + } + MnemonicCodec(loadFileContents).encode(hexEncodedSeed!!, MnemonicCodec.Language.Configuration.english) + } + private val adapter by lazy { val cursor = DatabaseFactory.getMmsSmsDatabase(this).getConversation(threadID) val adapter = ConversationAdapter( @@ -889,11 +904,18 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe startActivityForResult(MediaSendActivity.buildEditorIntent(this, listOf( media ), thread, getMessageBody()), ConversationActivityV2.PICK_FROM_LIBRARY) } - private fun sendTextOnlyMessage() { + private fun sendTextOnlyMessage(hasPermissionToSendSeed: Boolean = false) { + val text = getMessageBody() + val userPublicKey = TextSecurePreferences.getLocalNumber(this) + val isNoteToSelf = (thread.isContactRecipient && thread.address.toString() == userPublicKey) + if (text.contains(seed) && !isNoteToSelf && !hasPermissionToSendSeed) { + val dialog = SendSeedDialog { sendTextOnlyMessage(true) } + return dialog.show(supportFragmentManager, "Send Seed Dialog") + } // Create the message val message = VisibleMessage() message.sentTimestamp = System.currentTimeMillis() - message.text = getMessageBody() + message.text = text val outgoingTextMessage = OutgoingTextMessage.from(message, thread) // Clear the input bar inputBar.text = "" diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/SendSeedDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/SendSeedDialog.kt new file mode 100644 index 0000000000..b215e1f65c --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/SendSeedDialog.kt @@ -0,0 +1,23 @@ +package org.thoughtcrime.securesms.conversation.v2.dialogs + +import android.view.LayoutInflater +import androidx.appcompat.app.AlertDialog +import kotlinx.android.synthetic.main.dialog_send_seed.view.* +import network.loki.messenger.R +import org.thoughtcrime.securesms.conversation.v2.utilities.BaseDialog + +/** Shown if the user is about to send their recovery phrase to someone. */ +class SendSeedDialog(private val proceed: (() -> Unit)? = null) : BaseDialog() { + + override fun setContentView(builder: AlertDialog.Builder) { + val contentView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_send_seed, null) + contentView.cancelButton.setOnClickListener { dismiss() } + contentView.sendSeedButton.setOnClickListener { send() } + builder.setView(contentView) + } + + private fun send() { + proceed?.invoke() + dismiss() + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_send_seed.xml b/app/src/main/res/layout/dialog_send_seed.xml new file mode 100644 index 0000000000..28a8855010 --- /dev/null +++ b/app/src/main/res/layout/dialog_send_seed.xml @@ -0,0 +1,57 @@ + + + + + + + + + +