Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: store.bin encryption — use security CLI on macOS, make opt-in

Louis Beaumontaee1cd2b5487
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index f71cbfbc0..656ca77d9 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -9727,7 +9727,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.3.62"
+version = "2.3.64"
dependencies = [
"anyhow",
"arboard",
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/secrets.rs b/apps/screenpipe-app-tauri/src-tauri/src/secrets.rs
index 49d5fba51..bf5f9ddc6 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/secrets.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/secrets.rs
@@ -4,12 +4,16 @@
//! OS keychain integration for storing the store.bin encryption key.
//!
-//! Uses macOS Keychain, Windows Credential Manager, or Linux secret-service
-//! via the `keyring` crate. Falls back gracefully when unavailable.
+//! On macOS: uses the `security` CLI to read/write the login keychain.
+//! The `keyring` crate's Security framework API is subject to per-app ACL
+//! that breaks across app updates (new binary = access denied). The CLI
+//! bypasses this because it runs as a separate process with its own access.
+//!
+//! On Windows/Linux: uses the `keyring` crate as before.
use base64::{engine::general_purpose::STANDARD as B64, Engine};
use std::sync::OnceLock;
-use tracing::warn;
+use tracing::{debug, info, warn};
const SERVICE: &str = "com.screenpipe.app";
const KEY_NAME: &str = "store-encryption-key";
@@ -20,38 +24,54 @@ static AVAILABLE: OnceLock<bool> = OnceLock::new();
/// Check if the OS keychain is available (cached).
pub fn is_keychain_available() -> bool {
*AVAILABLE.get_or_init(|| {
- let probe = format!("__screenpipe_probe_{}", std::process::id());
- let entry = match keyring::Entry::new(SERVICE, &probe) {
- Ok(e) => e,
- Err(_) => {
- warn!("keychain: entry creation failed — keychain unavailable, store.bin will not be encrypted");
+ #[cfg(target_os = "macos")]
+ {
+ // On macOS, just check that the security CLI exists
+ std::process::Command::new("security")
+ .arg("help")
+ .stdout(std::process::Stdio::null())
+ .stderr(std::process::Stdio::null())
+ .status()
+ .is_ok()
+ }
+ #[cfg(not(target_os = "macos"))]
+ {
+ let probe = format!("__screenpipe_probe_{}", std::process::id());
+ let entry = match keyring::Entry::new(SERVICE, &probe) {
+ Ok(e) => e,
+ Err(_) => {
+ warn!("keychain: unavailable, store.bin will not be encrypted");
+ return false;
+ }
+ };
+ if entry.set_password("probe").is_err() {
+ warn!("keychain: write failed, store.bin will not be encrypted");
return false;
}
- };
- // Try a write/read/delete cycle
- if entry.set_password("probe").is_err() {
- warn!("keychain: write failed — keychain unavailable, store.bin will not be encrypted");
- return false;
+ let _ = entry.delete_credential();
+ true
}
- let _ = entry.delete_credential();
- true
})
}
-/// Retrieve the encryption key from the keychain. Returns None if unavailable or not set.
+/// Retrieve the encryption key from the keychain.
pub fn get_key() -> Option<[u8; 32]> {
if !is_keychain_available() {
return None;
}
- let entry = keyring::Entry::new(SERVICE, KEY_NAME).ok()?;
- let b64 = entry.get_password().ok()?;
+
+ let b64 = get_password_from_keychain()?;
let bytes = B64.decode(&b64).ok()?;
if bytes.len() != 32 {
- warn!("keychain: stored key has wrong length ({}), ignoring", bytes.len());
+ warn!(
+ "keychain: stored key has wrong length ({}), ignoring",
+ bytes.len()
+ );
return None;
}
let mut key = [0u8; 32];
key.copy_from_slice(&bytes);
+ debug!("keychain: retrieved existing store.bin encryption key");
Some(key)
}
@@ -66,18 +86,112 @@ pub fn get_or_create_key() -> Option<[u8; 32]> {
// Generate a new random key
let key = screenpipe_vault::crypto::generate_master_key();
let b64 = B64.encode(key.as_ref());
- let entry = keyring::Entry::new(SERVICE, KEY_NAME).ok()?;
- if let Err(e) = entry.set_password(&b64) {
- warn!("keychain: failed to store encryption key: {}", e);
+
+ if !set_password_in_keychain(&b64) {
+ warn!("keychain: failed to store encryption key");
return None;
}
- tracing::info!("keychain: generated and stored new store.bin encryption key");
+ info!("keychain: generated and stored new store.bin encryption key");
Some(*key)
}
/// Delete the encryption key from the keychain (for testing/reset).
#[allow(dead_code)]
pub fn delete_key() -> Result<(), String> {
- let entry = keyring::Entry::new(SERVICE, KEY_NAME).map_err(|e| e.to_string())?;
- entry.delete_credential().map_err(|e| e.to_string())
+ #[cfg(target_os = "macos")]
+ {
+ let status = std::process::Command::new("security")
+ .args(["delete-generic-password", "-s", SERVICE, "-a", KEY_NAME])
+ .stdout(std::process::Stdio::null())
+ .stderr(std::process::Stdio::null())
+ .status()
+ .map_err(|e| e.to_string())?;
+ if status.success() {
+ Ok(())
+ } else {
+ Err("keychain: delete failed".to_string())
+ }
+ }
+ #[cfg(not(target_os = "macos"))]
+ {
+ let entry = keyring::Entry::new(SERVICE, KEY_NAME).map_err(|e| e.to_string())?;
+ entry.delete_credential().map_err(|e| e.to_string())
+ }
+}
+
+// ── Platform-specific keychain access ──────────────────────────────────
+
+#[cfg(target_os = "macos")]
+fn get_password_from_keychain() -> Option<String> {
+ let output = std::process::Command::new("security")
+ .args([
+ "find-generic-password",
+ "-s",
+ SERVICE,
+ "-a",
+ KEY_NAME,
+ "-w", // print password only
+ ])
+ .output()
+ .ok()?;
+
+ if !output.status.success() {
+ debug!("keychain: key not found (security CLI returned non-zero)");
+ return None;
+ }
+
+ let password = String::from_utf8(output.stdout).ok()?.trim().to_string();
+ if password.is_empty() {
+ return None;
+ }
+ Some(password)
+}
+
+#[cfg(target_os = "macos")]
+fn set_password_in_keychain(password: &str) -> bool {
+ // Delete existing entry first (add-generic-password fails if it exists)
+ let _ = std::process::Command::new("security")
+ .args(["delete-generic-password", "-s", SERVICE, "-a", KEY_NAME])
+ .stdout(std::process::Stdio::null())
+ .stderr(std::process::Stdio::null())
+ .status();
+
+ let status = std::process::Command::new("security")
+ .args([
+ "add-generic-password",
+ "-s",
+ SERVICE,
+ "-a",
+ KEY_NAME,
+ "-w",
+ password,
+ "-T",
+ "", // allow access from any application (no per-app ACL)
+ ])
+ .stdout(std::process::Stdio::null())
+ .stderr(std::process::Stdio::null())
+ .status();
+
+ match status {
+ Ok(s) => s.success(),
+ Err(e) => {
+ warn!("keychain: security add-generic-password failed: {}", e);
+ false
+ }
+ }
+}
+
+#[cfg(not(target_os = "macos"))]
+fn get_password_from_keychain() -> Option<String> {
+ let entry = keyring::Entry::new(SERVICE, KEY_NAME).ok()?;
+ entry.get_password().ok()
+}
+
+#[cfg(not(target_os = "macos"))]
+fn set_password_in_keychain(password: &str) -> bool {
+ let entry = match keyring::Entry::new(SERVICE, KEY_NAME) {
+ Ok(e) => e,
+ Err(_) => return false,
+ };
+ entry.set_password(password).is_ok()
}
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/store.rs b/apps/screenpipe-app-tauri/src-tauri/src/store.rs
index aca83c42d..2fe54e15c 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/store.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/store.rs
@@ -25,30 +25,52 @@ fn decrypt_store_file(path: &Path) {
let key = match secrets::get_key() {
Some(k) => k,
None => {
- tracing::warn!("store.bin is encrypted but keychain key unavailable — resetting to defaults");
- // Remove the encrypted file so the plugin creates fresh defaults
- let _ = std::fs::remove_file(path);
+ tracing::warn!(
+ "store.bin is encrypted but keychain key unavailable — \
+ writing empty JSON so the plugin can create fresh defaults. \
+ The encrypted backup is saved as store.bin.encrypted.bak"
+ );
+ // Preserve the encrypted file as backup before overwriting
+ let backup = path.with_extension("bin.encrypted.bak");
+ let _ = std::fs::copy(path, &backup);
+ let _ = std::fs::write(path, b"{}");
return;
}
};
match screenpipe_vault::crypto::decrypt_small(&data[8..], &key) {
Ok(plaintext) => {
- // Atomic write: temp + rename
let tmp = path.with_extension("bin.dec.tmp");
if std::fs::write(&tmp, &plaintext).is_ok() {
let _ = std::fs::rename(&tmp, path);
}
}
Err(e) => {
- tracing::error!("failed to decrypt store.bin: {} — resetting to defaults", e);
- let _ = std::fs::remove_file(path);
+ tracing::error!(
+ "failed to decrypt store.bin: {} — saving backup as store.bin.encrypted.bak",
+ e
+ );
+ let backup = path.with_extension("bin.encrypted.bak");
+ let _ = std::fs::copy(path, &backup);
+ let _ = std::fs::write(path, b"{}");
}
}
}
-/// Encrypt store.bin in place if keychain key is available.
-/// No-op if already encrypted or keychain is unavailable.
+/// Encrypt store.bin in place if keychain key is available AND encryption is opted-in.
+///
+/// DISABLED BY DEFAULT — the macOS keychain doesn't reliably persist keys across
+/// app updates (code signing identity changes), causing settings loss on every update.
+/// The 0o600 file permissions are sufficient protection for now.
+///
+/// To opt in: create ~/.screenpipe/.encrypt-store or set SCREENPIPE_ENCRYPT_STORE=1.
fn encrypt_store_file(path: &Path) {
+ // Check opt-in flag
+ let opted_in = std::env::var("SCREENPIPE_ENCRYPT_STORE").map(|v| v == "1").unwrap_or(false)
+ || path.parent().map(|p| p.join(".encrypt-store").exists()).unwrap_or(false);
+ if !opted_in {
+ return;
+ }
+
let data = match std::fs::read(path) {
Ok(d) => d,
Err(_) => return,
@@ -65,7 +87,6 @@ fn encrypt_store_file(path: &Path) {
let mut out = Vec::with_capacity(8 + ciphertext.len());
out.extend_from_slice(STORE_MAGIC);
out.extend(ciphertext);
- // Atomic write: temp + rename
let tmp = path.with_extension("bin.enc.tmp");
if std::fs::write(&tmp, &out).is_ok() {
let _ = std::fs::rename(&tmp, path);
diff --git a/crates/screenpipe-a11y/src/tree/windows.rs b/crates/screenpipe-a11y/src/tree/windows.rs
index f45bf84c3..32ca3eebb 100644
--- a/crates/screenpipe-a11y/src/tree/windows.rs
+++ b/crates/screenpipe-a11y/src/tree/windows.rs
@@ -490,12 +490,11 @@ fn extract_text_from_tree(
// add-login route). Scan 2 levels of children for any text that
// matches an ignored pattern — extension UIs always render their
// brand name visibly somewhere near the top of the tree.
- let is_extension_popup = node
- .value
- .as_deref()
- .map_or(false, |v| v.starts_with("chrome-extension://")
+ let is_extension_popup = node.value.as_deref().map_or(false, |v| {
+ v.starts_with("chrome-extension://")
|| v.starts_with("moz-extension://")
- || v.starts_with("ms-browser-extension://"));
+ || v.starts_with("ms-browser-extension://")
+ });
if is_extension_popup
&& extension_subtree_matches_ignored(node, ignored_windows_lower)
{
@@ -592,10 +591,7 @@ fn extract_text_from_tree(
/// route). In that case the top-level Document name check misses it, but the
/// extension's own UI text always contains the brand name ("Bitwarden",
/// "1Password", etc.) a few levels in.
-fn extension_subtree_matches_ignored(
- node: &AccessibilityNode,
- ignored_lower: &[String],
-) -> bool {
+fn extension_subtree_matches_ignored(node: &AccessibilityNode, ignored_lower: &[String]) -> bool {
let matches = |val: &str| {
let lower = val.to_lowercase();
ignored_lower.iter().any(|ig| lower.contains(ig.as_str()))