From 52ccceb73222d00df3f2ecf5835801520eff3296 Mon Sep 17 00:00:00 2001 From: Incido Developer Date: Mon, 5 Oct 2026 19:43:42 +1300 Subject: [PATCH] =?UTF-8?q?feat(phase-1/m2.4a):=20scripted=20encrypted=20b?= =?UTF-8?q?ackup=20legs=20=E2=80=94=20the=20app=20round-trips=20ICDBKP1=20?= =?UTF-8?q?envelopes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 1 M2 task 4, desktop half (tools/DESKTOP_PHASE_PLAN.md §4). New --backup-export-enc + --backup-passphrase flags drive the exact calls the RequirePassphrase/Passphrase dialogs make, headlessly. Verified across two launches on fresh libraries: run A: import plain(27) → reimport skipped=27 → exported-enc=27 magic ok run B: preview encrypted=true → imported=27 → wrong-pw preview rejected → reimport imported=0 skipped=27 Gotcha found (and honoured): EncryptedBackupCodec zeroes the passphrase CharArray in BOTH encrypt and decrypt — the array is single-use fuel. The dialogs already comply (fresh toCharArray() per call); the scripted block re-derives it via newPw() for the same reason. Device half (4b: phone re-imports a desktop-made envelope + sees the dedupe snackbar) follows on Waydroid with the M2 wrap-up. --- .../main/kotlin/org/incido/desktop/Main.kt | 81 ++++++++++++++----- tools/DESKTOP_FEASIBILITY.md | 25 ++++++ 2 files changed, 88 insertions(+), 18 deletions(-) diff --git a/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt b/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt index a780439..fea9801 100644 --- a/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt +++ b/desktopApp/src/main/kotlin/org/incido/desktop/Main.kt @@ -44,11 +44,12 @@ import androidx.compose.ui.window.Window import androidx.compose.ui.window.application import androidx.compose.ui.window.rememberWindowState import java.io.File -import java.io.FileInputStream +import java.io.ByteArrayInputStream import java.io.FileOutputStream import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.incido.compose.data.backup.BackupBridge +import org.incido.compose.data.backup.EncryptedBackupCodec import org.incido.domain.model.Note import org.incido.domain.repository.NoteRepository @@ -67,7 +68,10 @@ import org.incido.domain.repository.NoteRepository * * Args: `--data-dir ` opens another library (e.g. a database pulled off * a phone, to prove schema compatibility); `--exit-after ` auto-closes - * for scripted smoke runs. + * for scripted smoke runs; `--backup-import ` / `--backup-export ` + * drive the Import/Export menu's exact bridge calls headlessly; + * `--backup-export-enc ` + `--backup-passphrase ` add the + * encrypted-envelope legs (M2 task 4). */ fun main(args: Array) { val dataDir = args.argAfter("--data-dir") @@ -81,6 +85,9 @@ fun main(args: Array) { // Import/Export menu makes, without clicking through native dialogs. val backupImport = args.argAfter("--backup-import") val backupExport = args.argAfter("--backup-export") + // M2 task 4: encrypted-envelope legs (the RequirePassphrase dialog's calls). + val backupExportEnc = args.argAfter("--backup-export-enc") + val backupPassphrase = args.argAfter("--backup-passphrase") println("=== Incido (desktop) — Phase 1 M1b ===") println("JVM : ${System.getProperty("java.vendor")} ${System.getProperty("java.version")}") @@ -100,6 +107,8 @@ fun main(args: Array) { selfTest = selfTest, backupImport = backupImport, backupExport = backupExport, + backupExportEnc = backupExportEnc, + backupPassphrase = backupPassphrase, onExit = ::exitApplication, ) } @@ -116,6 +125,8 @@ private fun NotesWindow( selfTest: Boolean, backupImport: String?, backupExport: String?, + backupExportEnc: String?, + backupPassphrase: String?, onExit: () -> Unit, ) { val notes by remember(repository) { repository.observeAll() } @@ -219,29 +230,63 @@ private fun NotesWindow( println("SELFTEST wrote note id=${saved.id}") } } - if (backupImport != null) { + if (backupImport != null || backupExportEnc != null) { LaunchedEffect(Unit) { delay(800) // mirror the self-test pacing; wait past first frame - val file = File(backupImport) - val preview = BackupBridge.preview(FileInputStream(file)) - println( - "BACKUP preview notes=${preview?.noteCount} " + - "attachments=${preview?.attachmentCount} app=${preview?.appVersion}", - ) - val first = BackupBridge.import(repository, FileInputStream(file)) - println( - "BACKUP imported=${first.imported} skipped=${first.skipped} " + - "(same calls as the Import… menu)", - ) + // The codec zeroes the CharArray it consumes (encrypt AND + // decrypt both call passphrase.fill), so every call needs a + // FRESH array — exactly what the dialogs do by re-deriving + // stage.passphrase?.toCharArray() per call. + fun newPw(): CharArray? = backupPassphrase?.toCharArray() + if (backupImport != null) { + val file = File(backupImport) + val bytes = file.readBytes() + val encrypted = EncryptedBackupCodec.isEncrypted(bytes) + // preview() takes the passphrase exactly like the + // RequirePassphrase dialog does, and returns null when the + // passphrase is wrong — the dialog's own reject gate. + val preview = BackupBridge.preview(ByteArrayInputStream(bytes), newPw()) + println( + "BACKUP preview notes=${preview?.noteCount} " + + "attachments=${preview?.attachmentCount} app=${preview?.appVersion} " + + "encrypted=$encrypted", + ) + if (preview != null) { + val first = BackupBridge.import(repository, ByteArrayInputStream(bytes), newPw()) + println( + "BACKUP imported=${first.imported} skipped=${first.skipped} " + + "(same calls as the Import… menu)", + ) + if (encrypted && backupPassphrase != null) { + val wrong = BackupBridge.preview( + ByteArrayInputStream(bytes), + "definitely-the-wrong-pass".toCharArray(), + ) + println("BACKUP wrong-pw preview=${if (wrong == null) "null (rejected)" else "LEAKED"}") + } + // M2 task 3: importing the same file again must add nothing — + // the dedupe path the confirm dialog's checkbox controls. + val again = BackupBridge.import(repository, ByteArrayInputStream(bytes), newPw()) + println("BACKUP reimport imported=${again.imported} skipped=${again.skipped} (dedupe ON)") + } + } if (backupExport != null) { val out = File(backupExport) val exported = BackupBridge.exportPlain(repository, FileOutputStream(out), DESKTOP_APP_VERSION) println("BACKUP exported=$exported notes to $out") } - // M2 task 3: importing the same file again must add nothing — - // the dedupe path the confirm dialog's checkbox controls. - val again = BackupBridge.import(repository, FileInputStream(file)) - println("BACKUP reimport imported=${again.imported} skipped=${again.skipped} (dedupe ON)") + if (backupExportEnc != null) { + val pass = newPw() ?: error("--backup-export-enc needs --backup-passphrase") + val out = File(backupExportEnc) + val n = BackupBridge.exportEncrypted(repository, FileOutputStream(out), pass, DESKTOP_APP_VERSION) + val magicOk = out.inputStream().use { ins -> + ins.readNBytes(EncryptedBackupCodec.MAGIC.size).contentEquals(EncryptedBackupCodec.MAGIC) + } + println( + "BACKUP exported-enc=$n notes to $out " + + "(magic=${if (magicOk) "ICDBKP1 ok" else "BAD"})", + ) + } } } if (exitAfterSeconds != null) { diff --git a/tools/DESKTOP_FEASIBILITY.md b/tools/DESKTOP_FEASIBILITY.md index 8323c58..b9b3241 100644 --- a/tools/DESKTOP_FEASIBILITY.md +++ b/tools/DESKTOP_FEASIBILITY.md @@ -401,6 +401,31 @@ Full suite green: `backupSmoke` (9 steps incl. dedupe), `assembleDebug`, `:core:data-jvm:smoke`, scripted desktop run (clean exit), `:desktopApp:run --self-test`. +**M2 task 4 (desktop leg) — encrypted round-trip via the app: DONE.** +New scripted flags `--backup-export-enc ` + `--backup-passphrase +` drive the passphrase dialogs' exact call chain headlessly: +`exportEncrypted` writes a real ICDBKP1 envelope, and importing it back +exercises `preview(stream, pw)` → `import(stream, pw)` → wrong-passphrase +preview → deduped re-import, in fresh libraries across two launches: + +``` +run A: plain import 27 → reimport skipped=27 → exported-enc=27 (magic=ICDBKP1 ok) +run B: preview encrypted=true → imported=27 → wrong-pw preview=null (rejected) + → reimport imported=0 skipped=27 +``` + +Caught while writing it: `EncryptedBackupCodec` zeroes the passphrase +`CharArray` it consumes — **both in `encrypt` AND `decrypt`** — so every +call needs a fresh array. The UI already complies (dialogs re-derive +`toCharArray()` per call); the scripted block initially reused one array +and died on the second decrypt with `AEADBadTagException`. Worth +remembering for the phone device pass and any future caller: treat the +passphrase array as single-use fuel. The envelope desktop produces is +byte-format-identical to the phone's because both sides now compile the +same `:core:backup` codec — the remaining proof for task 4 is the +on-device leg (Waydroid: phone re-imports a desktop-made encrypted file ++ dedupe snackbar), tracked as M2 task 4b. +