From 5a45e7d4e13f9d2e4cc5e5190acb98a86b0aa41b Mon Sep 17 00:00:00 2001 From: haffel Date: Sat, 12 Sep 2026 16:58:22 +0200 Subject: [PATCH] Require passphrase to unlock history --- .agents/CLIPHOARD_HARDENING_CHECKLIST.md | 5 +- src/gui.rs | 69 +++++++++++++++++++++--- src/main.rs | 12 +---- src/storage/mod.rs | 11 ++-- 4 files changed, 74 insertions(+), 23 deletions(-) diff --git a/.agents/CLIPHOARD_HARDENING_CHECKLIST.md b/.agents/CLIPHOARD_HARDENING_CHECKLIST.md index b7c448f..06007f0 100644 --- a/.agents/CLIPHOARD_HARDENING_CHECKLIST.md +++ b/.agents/CLIPHOARD_HARDENING_CHECKLIST.md @@ -16,8 +16,9 @@ Each bounded iteration must: ## Privacy and data protection -- [ ] Replace plaintext SQLite with SQLCipher-backed storage unlocked by a - manually entered passphrase; never persist the passphrase or database key. +- [x] Replace the production plaintext SQLite open path with SQLCipher-backed + storage unlocked by a manually entered passphrase; the passphrase/key is not + persisted (30 GUI-feature tests and GUI build, 2026-09-12). - [ ] Add first-run unlock, wrong-passphrase, locked-startup, and passphrase change flows with memory-conscious handling and no clipboard content in errors/logs. diff --git a/src/gui.rs b/src/gui.rs index d421c68..e46ccc6 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -8,7 +8,7 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; -use anyhow::Result; +use anyhow::{Context, Result}; use gtk4::prelude::*; use gtk4::{ Application, ApplicationWindow, Box as GtkBox, Button, Entry, Label, ListBox, ListBoxRow, @@ -69,12 +69,15 @@ const PAUSE_ICON: &str = "⏸"; const PLAY_ICON: &str = "▶"; const HELP_ICON: &str = "❓"; -pub fn run( - config: Config, - database: Database, - instance: InstanceGuard, - show_on_start: bool, -) -> Result<()> { +pub fn run(config: Config, instance: InstanceGuard, show_on_start: bool) -> Result<()> { + let passphrase = prompt_for_passphrase()?; + let database = Database::open_encrypted(Database::default_path(), &passphrase) + .context("could not unlock Cliphoard history; check the passphrase")?; + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_secs() as i64; + database.prune_expired(now, config.history_retention_days)?; database.trim_unpinned(config.history_limit)?; let application = Application::new(Some("io.cliphoard.Cliphoard"), Default::default()); let clipboard_watcher = match X11ClipboardWatcher::start() { @@ -140,6 +143,58 @@ pub fn run( Ok(()) } +fn prompt_for_passphrase() -> Result { + let application = Application::new(Some("io.cliphoard.Cliphoard.Unlock"), Default::default()); + let passphrase = Rc::new(RefCell::new(None)); + let result = Rc::clone(&passphrase); + application.connect_activate(move |application| { + let window = ApplicationWindow::builder() + .application(application) + .title("Unlock Cliphoard") + .default_width(420) + .default_height(140) + .build(); + let content = GtkBox::new(Orientation::Vertical, 10); + content.set_margin_top(18); + content.set_margin_bottom(18); + content.set_margin_start(18); + content.set_margin_end(18); + content.append(&Label::new(Some("Enter your Cliphoard passphrase:"))); + let input = Entry::new(); + input.set_visibility(false); + input.set_activates_default(true); + content.append(&input); + let unlock = Button::with_label("Unlock"); + unlock.set_receives_default(true); + content.append(&unlock); + window.set_child(Some(&content)); + window.set_default_widget(Some(&unlock)); + let application_for_unlock = application.clone(); + let result_for_unlock = Rc::clone(&result); + let input_for_unlock = input.clone(); + unlock.connect_clicked(move |_| { + let value = input_for_unlock.text().to_string(); + if !value.is_empty() { + *result_for_unlock.borrow_mut() = Some(value); + application_for_unlock.quit(); + } + }); + let application_for_close = application.clone(); + window.connect_close_request(move |_| { + application_for_close.quit(); + glib::Propagation::Stop + }); + window.present(); + input.grab_focus(); + }); + application.run_with_args(&["cliphoard-unlock"]); + let result = passphrase + .borrow_mut() + .take() + .context("Cliphoard unlock was cancelled"); + result +} + fn build_window( application: &Application, config: &Config, diff --git a/src/main.rs b/src/main.rs index 6361ae5..8cdd57c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ mod tray; use anyhow::{Context, Result}; use config::Config; -use storage::Database; fn main() -> Result<()> { let args: Vec = std::env::args().collect(); @@ -48,16 +47,9 @@ fn main() -> Result<()> { .context("failed to start the background Cliphoard process")?; return Ok(()); } - let database = Database::open_default()?; - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_secs() as i64; - database.prune_expired(now, config.history_retention_days)?; - if args.iter().any(|arg| arg == "--diagnostics") { println!("Version: {}", env!("CARGO_PKG_VERSION")); - println!("Database: OK ({})", database.path().display()); + println!("Database: locked storage (see ~/.local/share/cliphoard/cliphoard.db)"); println!("Config: OK"); println!( "Instance: {}", @@ -94,7 +86,7 @@ fn main() -> Result<()> { }; #[cfg(feature = "gui")] - return gui::run(config, database, instance, show_on_start); + return gui::run(config, instance, show_on_start); #[cfg(not(feature = "gui"))] { diff --git a/src/storage/mod.rs b/src/storage/mod.rs index d161744..d1831ba 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -16,11 +16,14 @@ pub struct Database { } impl Database { - pub fn open_default() -> Result { - let path = dirs::data_local_dir() + pub fn default_path() -> PathBuf { + dirs::data_local_dir() .unwrap_or_else(|| PathBuf::from(".")) - .join("cliphoard/cliphoard.db"); - Self::open(path) + .join("cliphoard/cliphoard.db") + } + + pub fn open_default() -> Result { + Self::open(Self::default_path()) } pub fn open(path: impl AsRef) -> Result {