diff --git a/ANDROID_SECURITY_PLAN.md b/ANDROID_SECURITY_PLAN.md index a1f9e63..b42c324 100644 --- a/ANDROID_SECURITY_PLAN.md +++ b/ANDROID_SECURITY_PLAN.md @@ -16,9 +16,9 @@ Tracking legend: `[x]` implemented for the stated item; `[~]` partially implemen OpenPGP.js 6.3.2 is bundled for Android testing. The test implementation stores passphrase-protected keys and public contacts in the existing Keystore-encrypted vault; supports key generation/import/export, contact trust controls, PGP/MIME encrypt/sign, and on-demand PGP/MIME or inline-encrypted-message decryption. Encrypted sends are converted to ciphertext before native sending or offline queueing; decrypted content is held in renderer memory only. Native prompts cover trust elevation, key replacement, and plaintext sending when keys exist or encryption was requested. Key-server lookup is disabled, and PGP/MIME messages are capped at 25 MiB. This work was implemented to enable testing before the independent review gate was satisfied; it is **not approved for sensitive or production use**. -**Validation evidence:** DAV URL, attachment-name, notification privacy, and Android OpenPGP trust/plaintext policy decisions have Android unit tests. The app lock was exercised on an emulator with a device PIN, including background lock and unlock. OpenPGP key generation/storage and native trust/plaintext confirmation dialogs were exercised on an emulator; trust cancellation left trust unchanged, trust confirmation persisted, and plaintext cancellation returned `PLAINTEXT_CANCELLED`. Phone and landscape layouts were checked for horizontal overflow. Current validation: 152 passing Node tests, 18 Android unit tests, and a successful Android debug build. This does not constitute live-provider mail testing or GnuPG/Thunderbird interoperability testing on Android. +**Validation evidence:** DAV URL, attachment-name, notification privacy, Android OpenPGP policy decisions, and the confirmation gate's accept/cancel callback ordering have Android unit tests. The app lock was exercised on an emulator with a device PIN, including background lock and unlock. OpenPGP key generation/storage and native trust/plaintext confirmation dialogs were exercised on an emulator; trust cancellation left trust unchanged, trust confirmation persisted, and plaintext cancellation returned `PLAINTEXT_CANCELLED`. Phone and landscape layouts were checked for horizontal overflow. Current validation: Android debug build and 20 Android unit tests pass; the Node suite has 151 passes and one version-consistency failure because the working tree's `package-lock.json` is locally set to 0.5.9 while `package.json` remains 0.5.8. That lockfile change was preserved. This does not constitute automated testing of the actual Android dialog UI, live-provider mail testing, or GnuPG/Thunderbird interoperability testing on Android. -**Release blockers:** independent human cryptography/security review; automated native confirmation and bypass tests; full signed/inline and hostile-input parity; Android/GnuPG/Thunderbird interoperability; Keystore lifecycle/recovery and biometric design; invalid-certificate integration tests; production-signed APK/manifest review; app-lock recovery polish; and compatibility review/remediation of the three moderate `npm audit --omit=dev` advisories in `ip-address` and the `nodemailer`/`mailparser` dependency chain. +**Release blockers:** independent human cryptography/security review; instrumented tests of actual native confirmation dialogs and policy-bypass attempts; full signed/inline and hostile-input parity; Android/GnuPG/Thunderbird interoperability; Keystore lifecycle/recovery and biometric design; invalid-certificate integration tests; production-signed APK/manifest review; app-lock recovery polish; and compatibility review/remediation of the three moderate `npm audit --omit=dev` advisories in `ip-address` and the `nodemailer`/`mailparser` dependency chain. ## Goals and limits @@ -64,7 +64,7 @@ These are implementation observations, not proof that all paths are safe. In par - [ ] Add optional biometric/device-credential release of PGP key access and test Keystore invalidation/recovery. Background sync does not use PGP private keys. - [~] The Android UI/bridge supports key generation, paste-based import, export, deletion, revocation export, contacts, fingerprints, and trust. Native prompts now guard trust elevation and replacement; automated cancel/confirm regression tests remain open. - [~] PGP/MIME encryption/signing, PGP/MIME and inline-encrypted-message decryption, attachments, encrypted Sent copies, and ciphertext-before-queueing are implemented. Signed-only/clear-signed/inline signature parity, full hostile-input/tampering suite, and Android interoperability remain open. -- [~] Native prompts guard trust elevation, key replacement, and plaintext sending when a key is available or encryption was requested; the native send path checks PGP/MIME structure. Emulator smoke tests cover trust accept/cancel and plaintext cancel; policy-level unit tests cover prompt/no-prompt decisions. Automated dialog-interaction, bypass, stale-key, and queue-policy tests remain open. +- [~] Native prompts guard trust elevation, key replacement, and plaintext sending when a key is available or encryption was requested; the native send path checks PGP/MIME structure. Emulator smoke tests cover trust accept/cancel and plaintext cancel; policy and confirmation-gate unit tests cover prompt decisions and one-shot accept/cancel handling. Instrumented tests of the actual dialog UI, bypass attempts, stale-key cases, and queue policy remain open. - [x] Key-server lookup and publication are disabled in the initial Android test implementation. **Exit criteria:** parity tests for desktop message formats and trust rules; Android-specific tests for cancel/confirm paths, tampering, malformed/oversized messages, key replacement, offline queueing, process death, and passphrase expiry; independent human review before release. diff --git a/TODO.md b/TODO.md index 81a8506..800d80b 100644 --- a/TODO.md +++ b/TODO.md @@ -34,7 +34,7 @@ - [x] Experimental Android test path: Keystore-encrypted vault key storage, key/contact management, PGP/MIME encryption/signing, and on-demand decryption. - [x] Exercise native trust-change and plaintext-send confirmations on an emulator, including cancellation. - [x] Add Android unit tests for trust-elevation, key-replacement, and plaintext-send confirmation decisions. - - [ ] Android release gate: automated native-dialog interaction and bypass tests, full parity/interoperability tests, independent human cryptography/security review, and signed release review. + - [ ] Android release gate: instrumented native-dialog UI and bypass tests, full parity/interoperability tests, independent human cryptography/security review, and signed release review. - [ ] Android follow-up: biometric unlock, full vault lifecycle/recovery, signed/inline verification parity, and dependency advisory remediation. - [x] Phase 5 (automated): large attachments, multiple recipients, corrupted/tampered payloads, inline PGP and PGP/MIME tests. - [x] Phase 5 (GnuPG): automated interoperability tests with GnuPG 2.5 (both directions: PGP/MIME, inline, clear-signed, detached signatures). diff --git a/android/app/src/main/java/studio/daybreak/mail/AndroidPgpConfirmationGate.kt b/android/app/src/main/java/studio/daybreak/mail/AndroidPgpConfirmationGate.kt new file mode 100644 index 0000000..4fe7e1c --- /dev/null +++ b/android/app/src/main/java/studio/daybreak/mail/AndroidPgpConfirmationGate.kt @@ -0,0 +1,39 @@ +package studio.daybreak.mail + +import java.util.concurrent.atomic.AtomicBoolean + +/** Makes native confirmations fail closed and guarantees each plugin call gets one decision. */ +internal class AndroidPgpConfirmationGate( + private val showPrompt: (Prompt) -> Unit, + private val dispatch: ((() -> Unit) -> Unit) +) { + data class Prompt( + val title: String, + val message: String, + val confirmLabel: String, + val cancelLabel: String, + val confirm: () -> Unit, + val cancel: () -> Unit + ) + + fun request( + title: String, + message: String, + confirmLabel: String, + cancelLabel: String = "Cancel", + onConfirmed: () -> Unit, + onCancelled: () -> Unit + ) { + val decided = AtomicBoolean(false) + showPrompt( + Prompt( + title = title, + message = message, + confirmLabel = confirmLabel, + cancelLabel = cancelLabel, + confirm = { if (decided.compareAndSet(false, true)) dispatch(onConfirmed) }, + cancel = { if (decided.compareAndSet(false, true)) dispatch(onCancelled) } + ) + ) + } +} diff --git a/android/app/src/main/java/studio/daybreak/mail/EpMailPlugin.kt b/android/app/src/main/java/studio/daybreak/mail/EpMailPlugin.kt index c22fc98..443e0e1 100644 --- a/android/app/src/main/java/studio/daybreak/mail/EpMailPlugin.kt +++ b/android/app/src/main/java/studio/daybreak/mail/EpMailPlugin.kt @@ -126,25 +126,20 @@ class EpMailPlugin : Plugin() { AndroidAccountVault.savePgpStore(next) call.resolve(JSObject().put("ok", true)) } else { - val host = activity - host.runOnUiThread { - AlertDialog.Builder(host) - .setTitle("Confirm OpenPGP key changes") - .setMessage(warning) - .setNegativeButton("Cancel") { _, _ -> call.reject("OpenPGP key change cancelled.") } - .setPositiveButton("Confirm changes") { _, _ -> - bridge.execute { - try { - AndroidAccountVault.savePgpStore(next) - call.resolve(JSObject().put("ok", true)) - } catch (error: Exception) { - call.reject(error.message ?: "Could not save the Android OpenPGP key store.") - } - } + showPgpConfirmation( + title = "Confirm OpenPGP key changes", + message = warning, + confirmLabel = "Confirm changes", + onConfirmed = { + try { + AndroidAccountVault.savePgpStore(next) + call.resolve(JSObject().put("ok", true)) + } catch (error: Exception) { + call.reject(error.message ?: "Could not save the Android OpenPGP key store.") } - .setOnCancelListener { call.reject("OpenPGP key change cancelled.") } - .show() - } + }, + onCancelled = { call.reject("OpenPGP key change cancelled.") } + ) } } catch (error: Exception) { call.reject(error.message ?: "Could not save the Android OpenPGP key store.") @@ -169,6 +164,31 @@ class EpMailPlugin : Plugin() { private fun jsonStrings(values: JSONArray?): List = values?.let { array -> (0 until array.length()).mapNotNull { array.optString(it).takeIf(String::isNotBlank) } }.orEmpty() + private fun showPgpConfirmation( + title: String, + message: String, + confirmLabel: String, + onConfirmed: () -> Unit, + onCancelled: () -> Unit + ) { + val gate = AndroidPgpConfirmationGate( + showPrompt = { prompt -> + val host = activity + host.runOnUiThread { + AlertDialog.Builder(host) + .setTitle(prompt.title) + .setMessage(prompt.message) + .setNegativeButton(prompt.cancelLabel) { _, _ -> prompt.cancel() } + .setPositiveButton(prompt.confirmLabel) { _, _ -> prompt.confirm() } + .setOnCancelListener { prompt.cancel() } + .show() + } + }, + dispatch = { action -> bridge.execute(action) } + ) + gate.request(title, message, confirmLabel, onConfirmed = onConfirmed, onCancelled = onCancelled) + } + @PluginMethod fun pgpGetMessageSource(call: PluginCall) { try { @@ -435,20 +455,15 @@ class EpMailPlugin : Plugin() { sendPrepared(call, input) return } - val host = activity - host.runOnUiThread { - AlertDialog.Builder(host) - .setTitle("Send without OpenPGP encryption?") - .setMessage(warning) - .setNegativeButton("Cancel") { _, _ -> - call.resolve(JSObject().put("failed", true).put("code", "PLAINTEXT_CANCELLED").put("message", "Plaintext send cancelled.")) - } - .setPositiveButton("Send plaintext") { _, _ -> bridge.execute { sendPrepared(call, input) } } - .setOnCancelListener { - call.resolve(JSObject().put("failed", true).put("code", "PLAINTEXT_CANCELLED").put("message", "Plaintext send cancelled.")) - } - .show() - } + showPgpConfirmation( + title = "Send without OpenPGP encryption?", + message = warning, + confirmLabel = "Send plaintext", + onConfirmed = { sendPrepared(call, input) }, + onCancelled = { + call.resolve(JSObject().put("failed", true).put("code", "PLAINTEXT_CANCELLED").put("message", "Plaintext send cancelled.")) + } + ) } private fun sendPrepared(call: PluginCall, input: JSONObject) { diff --git a/android/app/src/test/java/studio/daybreak/mail/AndroidPgpConfirmationGateTest.kt b/android/app/src/test/java/studio/daybreak/mail/AndroidPgpConfirmationGateTest.kt new file mode 100644 index 0000000..bc59568 --- /dev/null +++ b/android/app/src/test/java/studio/daybreak/mail/AndroidPgpConfirmationGateTest.kt @@ -0,0 +1,66 @@ +package studio.daybreak.mail + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Test + +class AndroidPgpConfirmationGateTest { + @Test + fun cancelDoesNotRunProtectedActionAndCannotBeOverriddenLater() { + var prompt: AndroidPgpConfirmationGate.Prompt? = null + var protectedActionCount = 0 + var cancelCount = 0 + val gate = AndroidPgpConfirmationGate( + showPrompt = { prompt = it }, + dispatch = { action -> action() } + ) + + gate.request( + title = "Confirm key change", + message = "Check the fingerprint", + confirmLabel = "Confirm", + onConfirmed = { protectedActionCount += 1 }, + onCancelled = { cancelCount += 1 } + ) + + assertEquals(0, protectedActionCount) + assertEquals(0, cancelCount) + assertNotNull(prompt) + prompt!!.cancel() + prompt!!.confirm() + prompt!!.cancel() + + assertEquals(0, protectedActionCount) + assertEquals(1, cancelCount) + } + + @Test + fun confirmRunsProtectedActionExactlyOnce() { + var prompt: AndroidPgpConfirmationGate.Prompt? = null + var protectedActionCount = 0 + var cancelCount = 0 + val gate = AndroidPgpConfirmationGate( + showPrompt = { prompt = it }, + dispatch = { action -> action() } + ) + + gate.request( + title = "Send plaintext?", + message = "The message will not be encrypted", + confirmLabel = "Send plaintext", + onConfirmed = { protectedActionCount += 1 }, + onCancelled = { cancelCount += 1 } + ) + + assertNotNull(prompt) + assertEquals("Send plaintext", prompt!!.confirmLabel) + prompt!!.confirm() + prompt!!.confirm() + prompt!!.cancel() + + assertEquals(1, protectedActionCount) + assertEquals(0, cancelCount) + assertTrue(prompt!!.message.contains("not be encrypted")) + } +} diff --git a/package-lock.json b/package-lock.json index 286186e..554dd11 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ep-mail", - "version": "0.5.8", + "version": "0.5.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ep-mail", - "version": "0.5.8", + "version": "0.5.9", "dependencies": { "@capacitor/android": "^8.5.2", "@capacitor/core": "^8.5.2", diff --git a/package.json b/package.json index 6d1364d..35546cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ep-mail", - "version": "0.5.8", + "version": "0.5.9", "private": true, "description": "Eternity Project mail client", "homepage": "https://git.eternityproject.fi/thuttu/ep-mail-client", diff --git a/tests/android-ui-support.test.cjs b/tests/android-ui-support.test.cjs index 1007a1a..64690a1 100644 --- a/tests/android-ui-support.test.cjs +++ b/tests/android-ui-support.test.cjs @@ -12,6 +12,7 @@ const mainActivity = fs.readFileSync(path.join(root, 'android', 'app', 'src', 'm const plugin = fs.readFileSync(path.join(root, 'android', 'app', 'src', 'main', 'java', 'studio', 'daybreak', 'mail', 'EpMailPlugin.kt'), 'utf8'); const androidMailService = fs.readFileSync(path.join(root, 'android', 'app', 'src', 'main', 'java', 'studio', 'daybreak', 'mail', 'AndroidMailService.kt'), 'utf8'); const androidPgpPolicy = fs.readFileSync(path.join(root, 'android', 'app', 'src', 'main', 'java', 'studio', 'daybreak', 'mail', 'AndroidPgpPolicy.kt'), 'utf8'); +const androidPgpConfirmationGate = fs.readFileSync(path.join(root, 'android', 'app', 'src', 'main', 'java', 'studio', 'daybreak', 'mail', 'AndroidPgpConfirmationGate.kt'), 'utf8'); const productivity = fs.readFileSync(path.join(root, 'productivity.js'), 'utf8'); const androidBridge = fs.readFileSync(path.join(root, 'scripts', 'android-bridge.js'), 'utf8'); const androidPgp = fs.readFileSync(path.join(root, 'scripts', 'android-pgp.js'), 'utf8'); @@ -77,10 +78,11 @@ test('Android OpenPGP uses the encrypted vault and queues only built MIME data', assert.match(plugin, /pgpStoreChangeWarning/); assert.match(plugin, /plaintextSendWarning/); assert.match(plugin, /Send plaintext/); - assert.match(plugin, /\.setNegativeButton\("Cancel"\) \{ _, _ -> call\.reject\("OpenPGP key change cancelled\."\) \}/); - assert.match(plugin, /\.setPositiveButton\("Confirm changes"\)[\s\S]*bridge\.execute \{[\s\S]*AndroidAccountVault\.savePgpStore\(next\)/); - assert.match(plugin, /\.setNegativeButton\("Cancel"\)[\s\S]*PLAINTEXT_CANCELLED/); - assert.match(plugin, /\.setPositiveButton\("Send plaintext"\)[\s\S]*sendPrepared\(call, input\)/); + assert.match(plugin, /showPgpConfirmation\(/); + assert.match(plugin, /onCancelled = \{ call\.reject\("OpenPGP key change cancelled\."\) \}/); + assert.match(plugin, /onConfirmed = \{ sendPrepared\(call, input\) \}/); + assert.match(plugin, /PLAINTEXT_CANCELLED/); + assert.match(androidPgpConfirmationGate, /compareAndSet\(false, true\)/); assert.match(androidPgpPolicy, /fun storeChangeWarning/); assert.match(androidPgpPolicy, /fun plaintextSendWarning/); assert.match(androidPgp, /curve25519Legacy/);