Modern Apps git

← Modern-Apps.git

securemail: drop unencrypted opt-in, send plain when no WKD key

Commit
5f25a19d082004f38e30b9e9281d8458e96a94f9
Author
vayun-mathur <[email protected]>
When
2026-10-24 00:55 UTC (1 month ago)
Parents
f8e1d16

Diff

securemail/src/main/java/com/vayunmathur/securemail/data/MailDatabase.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/data/MailDatabase.kt b/securemail/src/main/java/com/vayunmathur/securemail/data/MailDatabase.ktindex 8301f313e7..9810a22a26 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/data/MailDatabase.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/data/MailDatabase.kt@@ -54,7 +54,6 @@ data class OutboxMessage(     val subject: String,     val body: String,     val inReplyTo: String? = null,-    val sendUnencrypted: Boolean = false,     val createdMillis: Long,     val lastError: String? = null,     val attemptCount: Int = 0,
securemail/src/main/java/com/vayunmathur/securemail/domain/mail/KeyLookupPolicy.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/domain/mail/KeyLookupPolicy.kt b/securemail/src/main/java/com/vayunmathur/securemail/domain/mail/KeyLookupPolicy.ktindex ce46aaf31d..af9718f7cc 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/domain/mail/KeyLookupPolicy.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/domain/mail/KeyLookupPolicy.kt@@ -43,10 +43,11 @@ object KeyLookupPolicy {         /** Every recipient has a usable key — send encrypted. */         data object Encrypted : SendDecision         /**-         * Some recipients lack keys. [missing] lists them; the caller must-         * obtain explicit per-message opt-in before sending unencrypted.+         * Some recipients lack keys — they receive the message unencrypted+         * (badged `plain` on receipt). No opt-in; no WKD key means no+         * encryption for those recipients.          */-        data class NeedsUnencryptedOptIn(val missing: List<String>) : SendDecision+        data class UnencryptedFallback(val missing: List<String>) : SendDecision         /** A known key changed — the user must acknowledge before sending. */         data class NeedsKeyChangeAck(val changed: List<RecipientTrust>) : SendDecision     }@@ -56,7 +57,7 @@ object KeyLookupPolicy {         if (changed.isNotEmpty()) return SendDecision.NeedsKeyChangeAck(changed)         val missing = recipients.filter { !it.hasKey }.map { it.address }         return if (missing.isEmpty()) SendDecision.Encrypted-        else SendDecision.NeedsUnencryptedOptIn(missing)+        else SendDecision.UnencryptedFallback(missing)     }      /**
securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt b/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.ktindex c0bf0b9d41..bf05e63c55 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt@@ -347,15 +347,16 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {     }      /**-     * Sends a message: encrypts per-recipient on device, publishes over the live-     * socket when up, else queues to the outbox worker. Unencrypted send requires-     * [allowUnencrypted] (explicit per-message opt-in from Compose).+     * Sends a message: encrypts per-recipient on device to every WKD key found,+     * publishes over the live socket when up, else queues to the outbox worker.+     * Recipients without a key receive the message unencrypted (badged `plain`+     * on receipt) — there is no opt-in; no WKD key simply means no encryption.+     * Changed/lost keys still abort the send for explicit acknowledgement.      */     fun send(         to: List<String>,         subject: String,         body: String,-        allowUnencrypted: Boolean,         inReplyTo: String? = null,     ) {         state = state.copy(busy = true, error = null)@@ -366,10 +367,8 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {                     when (val decision = KeyLookupPolicy.decide(trust)) {                         is KeyLookupPolicy.SendDecision.NeedsKeyChangeAck ->                             error("Key changed for ${decision.changed.first().address}. Review in Settings first.")-                        is KeyLookupPolicy.SendDecision.NeedsUnencryptedOptIn ->-                            if (!allowUnencrypted) {-                                error("No key for ${decision.missing.first()}. Enable unencrypted to send anyway.")-                            }+                        is KeyLookupPolicy.SendDecision.UnencryptedFallback ->+                            Unit // no key: send unencrypted, badged plain on receipt                         KeyLookupPolicy.SendDecision.Encrypted -> Unit                     }                     val encryptedTo = trust.filter { it.hasKey }@@ -382,26 +381,18 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {                         val blob = PgpCrypto.encrypt(mime, cached.armoredPublic)                         val ok = MailSocket.publish(t.address, blob)                         if (!ok) {-                            queueOutbox(listOf(t.address), subject, body, inReplyTo, false)+                            queueOutbox(listOf(t.address), subject, body, inReplyTo)                             queued++                         }                     }-                    // Keyless recipients: socket plaintext frame (opt-in) for own-domain,-                    // server federation for external — but only when the socket is-                    // up; otherwise the local outbox holds them for retry.+                    // Keyless recipients go out as plaintext frames for the+                    // server to federate (own-domain) or relay onward —+                    // badged `plain` wherever they land.                     for (address in plainTo) {-                        if (AddressValidator.isOwnDomain(address)) {-                            val ok = MailSocket.publish(address, mime, unencrypted = true)-                            if (!ok) {-                                queueOutbox(listOf(address), subject, body, inReplyTo, true)-                                queued++-                            }-                        } else {-                            val ok = MailSocket.publish(address, mime, unencrypted = true)-                            if (!ok) {-                                queueOutbox(listOf(address), subject, body, inReplyTo, true)-                                queued++-                            }+                        val ok = MailSocket.publish(address, mime, unencrypted = true)+                        if (!ok) {+                            queueOutbox(listOf(address), subject, body, inReplyTo)+                            queued++                         }                     }                     if (queued > 0) "Queued $queued message(s) — retry from Outbox when online." else "Sent."@@ -422,7 +413,6 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {         subject: String,         body: String,         inReplyTo: String?,-        sendUnencrypted: Boolean,     ) {         repo.outboxDao().insert(             OutboxMessage(@@ -430,7 +420,6 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {                 subject = subject,                 body = body,                 inReplyTo = inReplyTo,-                sendUnencrypted = sendUnencrypted,                 createdMillis = System.currentTimeMillis(),             ),         )@@ -438,13 +427,12 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {      /**      * Drains the local outbox straight to the server over the live socket —-     * no trust re-resolution (that already happened at send time; the entry-     * records the decision in [OutboxMessage.sendUnencrypted]).+     * no trust re-resolution (that already happened at send time).      *-     * Encrypted entries re-encrypt to the cached key; plaintext entries-     * publish as explicit-opt-in unencrypted frames for the server to-     * federate. Entries stay queued while offline; anything still failing-     * after publish records an attempt for the next retry.+     * Key-known recipients re-encrypt to the cached key; keyless recipients+     * publish as plaintext frames for the server to federate. Entries stay+     * queued while offline; anything still failing after publish records an+     * attempt for the next retry.      */     fun retryOutbox() {         viewModelScope.launch {@@ -463,10 +451,10 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {                         var entryOk = true                         for (address in to) {                             val cached = repo.keyDao().get(address)-                            if (cached != null && !entry.sendUnencrypted) {+                            if (cached != null) {                                 val blob = PgpCrypto.encrypt(mime, cached.armoredPublic)                                 if (MailSocket.publish(address, blob)) continue-                            } else if (entry.sendUnencrypted) {+                            } else {                                 if (MailSocket.publish(address, mime, unencrypted = true)) continue                             }                             entryOk = false
securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.kt b/securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.ktindex 15e34f305a..97700ab24d 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/ui/ComposeScreen.kt@@ -17,8 +17,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button-import com.vayunmathur.library.ui.Card-import com.vayunmathur.library.ui.Checkbox import com.vayunmathur.library.ui.DesktopMaxWidthContainer import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedTextField@@ -26,13 +24,14 @@ import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.appBarScrollBehavior import com.vayunmathur.library.ui.rememberMessenger import com.vayunmathur.securemail.domain.mail.AddressValidator-import com.vayunmathur.securemail.domain.mail.KeyLookupPolicy import com.vayunmathur.securemail.platform.MailUiState import kotlinx.coroutines.launch  /**- * Compose with per-recipient trust: encrypted by default, explicit opt-in for- * keyless recipients, hard stop on changed/lost keys.+ * Compose: encrypts to every recipient with a WKD key; recipients without+ * one receive the message unencrypted (badged `plain` on receipt). There is+ * no opt-in toggle — no WKD key simply means no encryption for that+ * recipient. Changed/lost keys still hard-stop the send.  */ @Composable fun ComposeScreen(@@ -40,7 +39,7 @@ fun ComposeScreen(     initialTo: String,     initialSubject: String,     initialBody: String,-    onSend: (List<String>, String, String, Boolean, String?) -> Unit,+    onSend: (List<String>, String, String, String?) -> Unit,     onBack: () -> Unit,     onConsumeMessage: () -> Unit = {}, ) {@@ -49,7 +48,6 @@ fun ComposeScreen(     var to by remember { mutableStateOf(initialTo) }     var subject by remember { mutableStateOf(initialSubject) }     var body by remember { mutableStateOf(initialBody) }-    var allowUnencrypted by remember { mutableStateOf(false) }     val scope = rememberCoroutineScope()     // Trust resolution needs the ViewModel; the parent passes a lambda that     // already gates. Here we only pre-validate syntax inline.@@ -92,30 +90,16 @@ fun ComposeScreen(                     modifier = Modifier.fillMaxWidth(),                     minLines = 8,                 )-                Card(modifier = Modifier.fillMaxWidth()) {-                    Row(-                        modifier = Modifier.padding(12.dp).fillMaxWidth(),-                        horizontalArrangement = Arrangement.spacedBy(8.dp),-                    ) {-                        Checkbox(checked = allowUnencrypted, onCheckedChange = { allowUnencrypted = it })-                        Text(-                            "Send unencrypted to recipients without a key. " +-                                "They will see a plain badge; you accept the downgrade.",-                            style = MaterialTheme.typography.bodySmall,-                            modifier = Modifier.weight(1f),-                        )-                    }-                }                 Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {                     Button(                         onClick = {                             scope.launch {-                                onSend(recipients, subject, body, allowUnencrypted, null)+                                onSend(recipients, subject, body, null)                             }                         },                         enabled = !state.busy && recipients.isNotEmpty() && invalid.isEmpty() && body.isNotBlank(),                         modifier = Modifier.weight(1f),-                    ) { Text(if (state.busy) "Sending…" else "Send encrypted") }+                    ) { Text(if (state.busy) "Sending…" else "Send") }                     Button(onClick = onBack, modifier = Modifier.weight(1f)) { Text("Cancel") }                 }             }
securemail/src/test/java/com/vayunmathur/securemail/domain/mail/MailPolicyTest.kt
diff --git a/securemail/src/test/java/com/vayunmathur/securemail/domain/mail/MailPolicyTest.kt b/securemail/src/test/java/com/vayunmathur/securemail/domain/mail/MailPolicyTest.ktindex 92d3f0c072..7a8c82b17e 100644--- a/securemail/src/test/java/com/vayunmathur/securemail/domain/mail/MailPolicyTest.kt+++ b/securemail/src/test/java/com/vayunmathur/securemail/domain/mail/MailPolicyTest.kt@@ -61,11 +61,11 @@ class KeyLookupPolicyTest {     }      @Test-    fun missingKeysNeedOptIn() {+    fun missingKeysFallBackUnencrypted() {         val decision = KeyLookupPolicy.decide(             listOf(trust("[email protected]", true), trust("[email protected]", false)),         )-        assertTrue(decision is KeyLookupPolicy.SendDecision.NeedsUnencryptedOptIn)+        assertTrue(decision is KeyLookupPolicy.SendDecision.UnencryptedFallback)         assertEquals(listOf("[email protected]"), decision.missing)     }